paypalpaypal-sandboxpaypal-rest-sdkpaypal-buttons

JS client for PayPal Smart Buttons /v2 API: how to set payment_method and override brand_name?


Can't figure out how and where to set 1.payment_method to ' IMMEDIATE_PAYMENT_REQUIRED', override 2.brand_name (Seller's default name on PayPal account) and 3.To getan invoice_id(order_id) onApprove.

I have the following JavaScript client /v2 API PayPal code that works well except those 3 new variables I want to set/get.

paypal.Buttons({
                    style: {
                        color: 'gold',
                        shape: 'pill',
                        height: 40
                    },
                    commit: true,
                    createOrder: function (data, actions) {
                        return actions.order.create({

                            purchase_units: [{
                                    amount: {
                                        value: 20.00
                                    },
                                    description: 'My company',
                                    invoice_id: '234567890',
                                    soft_descriptor: 'mysite.com'
                                }]
                        });
                    },
                    onApprove: function (data, actions) {
                        return actions.order.capture().then(function (details) {
                            var name = details.payer.name.given_name;
                            alert('Transaction completed by ' + name);
                        });
                    }
                }).render('#paypal-button-container');

Solution

  • In the createOrder, payment_method -> payee_preferred : https://developer.paypal.com/docs/api/orders/v2/#definition-payment_method

    application_context -> brand_name : https://developer.paypal.com/docs/api/orders/v2/#definition-order_application_context

    Since you are already setting invoice_id in createOrder, in onApprove have you done console.log(details) and looked in all the subobjects?

    Note that invoice_id must be a unique value for every successful transaction, so this shouldn't be a hardcoded string.

    .....

    Example:

                    createOrder: function (data, actions) {
                        return actions.order.create({
    
                            purchase_units: [{
                                    amount: {
                                        value: 20.00
                                    },
                                    description: 'My company',
                                    invoice_id: '234567890',
                                    soft_descriptor: 'mysite.com'
                                }],
                            application_context: {
                                brand_name: "MyBusiness",
                            },
                            payment_method: {
                                payee_preferred: "IMMEDIATE_PAYMENT_REQUIRED",
                            }
                        });
                    },