What is meant by createOrder: in JavaScript - paypal

I'm trying to create a PayPal button, but don't understand what createorder: mean in this javascript code
createOrder: function(data, actions) {
Can you give me any reference about this?

With PayPal Checkout, the createOrder JavaScript function is called when a button is clicked.
The purpose of the function is to create a v2/checkout/orders object -- for the payer to then approve. The order object contains all details about the transaction taking place, including the amount.
After receiving approval from a payer, an order must be captured for any transaction to occur.
That PayPal Checkout guide should have all the information you need to get started... but since you asked for a reference, see the JavaScript SDK reference for complete detail.

Related

Retrieve paypal subscription data after approval

Afternoon
I am a bit of a PayPal integration newbie and having been working on a project where customers use a subscription button to purchase goods. Everything works well however I need to retrieve subscription data after approval for further use. Initially I need the email address so we can send a customised automated email thanking the customer for their purchase but going forward would like the majority of the data so it can be placed in an SQL database.
The onApprove: function(data, actions) seems to be the place to enter code but not overly sure where to start. The subscriptionID seems to be pulled and used within the default code, but when I try and pull another piece of data nothing happens.
onApprove: function(data, actions) { //alert('You have successfully created subscription ' + data.subscriptionID); alert('Here is the email address ' + data.subscriber.email_address); }
Is my reference to the email address wrong or does paypal only give you access to the subscriptionID which can then be passed to another PHP page and I can use do a REST API request for the subscription data??
Many thanks chris
console.log(data) in the onApprove will show you the data that is available on the client side.
To log information to a server, you should not depend on approval notifications from the client, since they may fail for any number of reasons. Instead, (using the correct clientid) subscribe to webhooks related to subscriptions, particularly the PAYMENT.SALE.COMPLETED event so you receive an update every time a subscription is successfully renewed.
For reconciliation (so you know what payment is for what), you can pass your own arbitrary custom_id when the subscription is first created. This will be returned in the webhook.

PayPal Subscription API, Smart Payment Button and custom variables

I am trying to set up a recurring payment with PayPal Subscription REST API. I'm following the Basic Integration in PayPal Developer Portal, using as suggested the Smart Payment Button. Besides, I'm using a WebHook to "capture" events related to subscription.
As the tutoria suggest, I created a Product and a Plan.
Now, I rendered the Smart Payment Button in a page (using the example), and when clicked it triggers the PayPal authorization flow. When the user complete the subscription process, the BILLING.SUBSCRIPTION.CREATED event is triggered. Later, also the PAYMENT.SALE.COMPLETED and BILLING.SUBSCRIPTION.ACTIVATED are triggered, and I receive data in my WebHook. Now, I have troubles in identifying which user activated the subscription. My idea is to pass a custom variable (with something allows me to identify the user) and retrive it later when the WebHook post me data, but I can't undertand how to do it. Anyway, is this the best way to do it? Or there is another solution?
I solved this using "custom_id" in the smart button:
return actions.subscription.create({
'plan_id': 'PLAN_ID',
'custom_id': 'CUSTOM_ID'
});
Then, I receive custom_id from webhooks or querying the subscription.

How to add custom user id in subscriptions paypal api

I need integrate paypal subscriptions to my project.
I use new paypal subscriptions api https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions and need to know how i can identify payer and my own db user.
Here is scenario in my mind:
I create my own db user with signup form.
I set my own user id to paypal api call
After user confirm subscription paypal return me subscriptions id, then i call paypal api to get details of returned subscription and see my own db user id
But looks like paypal cant provide it.
here is my smart checkout button code
paypal.Buttons({
style: {
layout: 'horizontal',
size: 'small',
color: 'blue',
label: 'pay',
height: 55,
tagline: 'false'
},
createSubscription: function(data, actions) {
return actions.subscription.create({
'plan_id': 'P-5GS67390M7258253CLUHXAHQ',
'metadata' : {
'user_id' : 'myuserid'
}
});
},
onApprove: function(data, actions) {
console.log(data);
alert('You have successfully created subscription ' + data.subscriptionID);
}
}).render('#paypal-button-container');
</script>
I found only one way how i can do it
User insert all data to signup form and then click on paypal button
After success paypal payment on
onApprove: function(data, actions) {
$('#myform').append('<input name="subscriptions_id" value="data.subscriptionID"')
$('#myform').submit()
alert('You have successfully created subscription ' + data.subscriptionID);
}
What is the best way to add subscriptions id for mysql db user?
The solution in the question is one possible way. The problem with it is that if the client goes down before sending the data to the server, the association between user_id and subscription_id is lost.
The way around it is to send the user_id to paypal as custom_id. That custom_id is then a part of the subscription data, retained on the payapl server, shown in the paypal billing UI for the subscriptiion details. I do not yet know it for sure, but strongly suspect it is also sent to the webhook.
see PayPal API documentation for creating a subscription
So ideally combine both. Have a webhook to associate the custom_id (your user_id) with the subscription. Have the API call (form post) like you do. If either way fails, you have the fallback.
UPDATE 2021-09-02: I myself use that approach in our web billing solution. One thing I noticed, statistically, it is a very rare event that the web client goes down before getting a response back. I know it because webhook calls come from hours to days after the event. After several hundreds of subscriptions that I analyzed, I have seen not a single case where the webhook came to patch up a missing transaction.
I do not know why it is so. It certainly exceeded my expectations.
My guesses are 1. PayPal server replies fast 2. my PWA is written well enough to warn user to wait a bit not close the browser window until the confirmation screen.
Still I recommend to implement webhook. It would be needed for all the other cases where user cancels subscription directly on the paypal, or such other events.
The solution in the question is correct, the subscription-id should be stored and associated with the internal user-id at creation time, which for subscriptions occurs right after approval (in the onApprove function)
The fetch API can be used instead of a form post to communicate this subscription-id to the server, along with the user-id to associate it with -- similar to the examples in https://developer.paypal.com/demo/checkout/#/pattern/server

Braintree - Callback When Payment Method Changes

Is there a callback in the Braintree drop-in for when the user changes the payment method? I see there is onPaymentMethodReceived but it's called when the form is submitted. I need a callback when the payment method changes.
Basically, what I need is a way to know when the user selects "PayPal" as the payment method.
Full disclosure: I'm a developer at Braintree. If you have any further questions, feel free to contact support.
Right now we don't have a callback for the Drop-in that is called when the payment method is updated. If your main usage of the Drop-in is to be SAQ A compliant, it is worth it to check out Hosted Fields. Hosted Fields let you custom build your own payment method forms, with the actual form input handled on Braintree servers. This also allows you to customize your integration more than the Drop-in, which would let you build your own payment method picker, and implement any callbacks necessary to detect a change in payment method selection.

How to integrate with Paypal suggested solution: "Add payment buttons to your website"

I hope to integrate Paypal using "Add payment buttons to your website" https://developer.paypal.com/webapps/developer/docs/integration/web/ since my site only needs one time purchase so this solution is perfect.
But it requires an IPN implemented as an async solution at the merchant side to receive the transaction result. While the button redirects user to paypal.com to finish the payment process. But after that is done. User is not automatically redirected back to merchant site.
I found on the forum that I can add an "data-return" attribute in the script element so when user is done paying there is an additional link that points to the merchant site. Now since IPN is asynchronous, once user views the "data-return" page, IPN may not have been called yet. My questions are:
1. So what's the suggested solution of synchronizing between return page and IPN?
2. When testing, I found a lot of query strings are appended to the "data-return" URL, looks related to transaction result. Can I use them to replace IPN so I have a SYNCHRONOUS way of verifying the transaction result?
Thanks!
Use Payment Data Transfer (PDT, for short). https://developer.paypal.com/webapps/developer/docs/classic/paypal-payments-standard/integration-guide/paymentdatatransfer/