Search code examples
javascriptreactjspaypalpaypal-sandbox

Is there any way to provide a shipping address to @paypal/react-paypal-js createOrder prop


<PayPalScriptProvider options={{ "client-id": "", currency: "MXN" }}>
   <PayPalButtons
       createOrder={createOrder}
       onApprove={(data, actions) => onApprove(data, actions)}
       onError={error => console.log(error)}/>
</PayPalScriptProvider>

I'm calling a createOrder function like this :

const createOrder = (data, actions) => {
        return actions.order.create({
            purchase_units: [
                {
                    amount: {
                        value: 100.00
                    },
                    description: "asdasda",
                }
            ],
            intent: "CAPTURE",
        }
        )
    }

I want to send a pre-set shipping address.

What I've tried:

Sending it in purchase_units array like this:


purchase_units: [
   {
       amount: {
          value: 100.00
       },
       shipping: {
         shipping_detail: {
           address : {
                //here goes the address
           }
         }
       }
  }
],

I've also tried sending it into payee object but it didn't work.

So, is there any way to do this?


Solution

  • i have solved it just adding this:

    const createOrder = (data, actions) => {
    return actions.order.create({
      application_context: {
        shipping_preferences: 'SET_PROVIDED_ADDRESS', //Just add this and it will take the address
      },
      purchase_units: [
        {
          amount: { value: parseInt(`${total}.0`) },
          description: `Cuadroy ${cantidad} cuadros 20x20 ${marco}`,
          shipping: {
            address: {
              address_line_1: calle,
              address_line_2: colonia,
              admin_area_2: municipio,
              admin_area_1: estado,
              postal_code: cp,
              country_code: country,
            },
          },
        },
      ],
      intent: 'CAPTURE',
    });
    

    All i need was adding application context object and set shipping preferences to SET_PROVIDED_ADDRESS and thats it.

    ALSO i have changed it shipping_detail to shipping like the other user said.