Home > OS >  Passing both user ID and product ID to Paypal Button
Passing both user ID and product ID to Paypal Button

Time:10-22

I am using @paypal/react-paypal-js to display the PayPal button and create an order. I am specifically interested in processing credit card payments. Once my Django backend receives the payment capture completion event from PayPal, it needs to store information about a particular user owning a particular product. Hence, the notification has to contain both the user ID (i.e. email) and the product ID.

This is how I am trying to pass these two pieces of information in my frontend:

createOrder={(data, actions) => {
    return actions.order.create({
        purchase_units: [
            {
                amount: {
                    value: course.price,
                    currency_code: course.currency,
                },
                custom_id: course.title,
            },
        ],
        payer: {
            email_address: userEmail,
        }
    });
}}

Here is my onApprove:

onApprove={(data, actions) => onSuccess(data, actions)}

where onSuccess is implemented as follows:

const onSuccess= (data, actions) => {
    return actions.order.capture().then(
        function (details) {
            toast.success('Payment completed. Thank you, '   
                          details.payer.name.given_name,
                          {duration: 6000})
        }
    );
}

The user email address is automatically filled into the form. However, the webhook_event event in the server does not contain this information. That is, there is no payer field inside resource in webhook_event.

I also tried adding payee: {email_address: userEmail} into purchase_units, but then there was no option to pay with credit card after pressing on the PayPal button. That option got replaced by the option to create a PayPal account.

How can I pass the two pieces of information so as to get them in webhook_event? (I would like to avoid the ugly solution of concatenating both pieces into custom_id)

CodePudding user response:

I also tried adding payee: {email_address: userEmail} into purchase_units, but then there was no option to pay with credit card after pressing on the PayPal button. That option got replaced by the option to create a PayPal account.

payee sets which account receives the PayPal payment. The black Debit or Credit Card button will generally be available for guest payments if the recipient PayPal account exists and is confirmed.

How can I pass the two pieces of information so as to get them in webhook_event?

If you absolutely must store two pieces information as part of a custom value (usually only useful for subscriptions, although even for subscriptions there are better solutions that involve using your own database), you could encode a JSON object with json.dumps({'email': somevalue, 'product': somevalue} and store that json string value in custom_id -- then decode it later on receipt with json.loads.

It's unusual to need to do anything like this, however, especially for one-time Order payments. You should implement order creation and capture from your own server routes and store the necessary information on your own database immediately on successful capture, before returning the capture result to the button JavaScript. See the information for a server integration in Set up standard payments.

Webhooks are not necessary at all in such a case, much less retrieving item information from a webhook. You however can and should include item information as part of the PayPal transaction for informational purposes, via an items array. This will also show to the payer in the upper right cart dropdown during checkout, and be visible in the completed paypal.com transaction details.

  • Related