Domantas Šlaičiūnas
Domantas Šlaičiūnas

Reputation: 369

PayPal Orders API schema

I am setting up a payment integration with PayPal Checkout Buttons and Paypal Orders API V2. Payments are going throw, but now I want to include items in my Order request, but I getting: enter image description here

As PayPal docs say I have to pass array (contains the item object) Link to Orders API purchase_unit object Link to Orders API item object. But I'm still getting this error.
This is my request:

public function createOrder($value, $order, $products, $currency = 'EUR')
    {
      return $this->makeRequest(
        'POST',
        '/v2/checkout/orders',
        [],
        [
          'intent' => 'CAPTURE',
          'payer' => [
            'name' => [
              'given_name' => $order->billing_firstname,
              'surname' => $order->billing_lastname
            ],
            'email_address' => $order->email_address,
            'address' => [
              'address_line_1' => $order->billing_street_address,
              'admin_area_2' => $order->billing_city,
              'postal_code' => $order->billing_postcode,
              'country_code' => $order->billing_country_code
            ],
          ],
          'purchase_units' => [
            0 => [
              'amount' => [
                'currency_code' => $currency,
                'value' => $value,
              ],
              'description' => 'Order: ' . $order->order_serial_number,

              'items' => [
                0 => [
                  'name' => 'Item1',
                  'unit_amount' => 100,
                  'quantity' => 1
                ],
              ],
            ],
          ],
          'application_context' => [
              'brand_name' => config('app.name'),
              'shipping_preference' => 'NO_SHIPPING',
              'user_action' => 'PAY_NOW',
              'return_url' => route('approval.paypal', $order->id_order),
              'cancel_url' => route('payment.cancelled', $order->id_order),
          ]
        ],
        [],
        $isJsonRequest = true
      );
    }

I'm using: Laravel and Guzzle/HTTP to perform payment requests.

As I say, payments are going throw, but when I try to add Items to Order, I'm getting this error.

Upvotes: 0

Views: 852

Answers (1)

Preston PHX
Preston PHX

Reputation: 30359

You haven't posted the full (rest of the) error response that details the problem, so there may be another issue we can't see, but one thing that's missing is the amount.breakdown.item_total subtotal, which is required when passing an items array with amounts: https://developer.paypal.com/docs/api/orders/v2/#definition-amount_breakdown

A simple example in the API's JSON format, which you can adapt to PHP array syntax:

// based on example array from https://developer.paypal.com/docs/checkout/reference/server-integration/set-up-transaction/
  "purchase_units": [{
      "amount": {
        "value": "17.49",
        "currency_code": "USD",
        "breakdown": {
          "item_total": {
            "currency_code": "USD",
            "value": "17.49"
          },
        }
      },
      "items": [
        {
          "unit_amount": {
            "currency_code": "USD",
            "value": "17.49"
          },
          "quantity": "1",
          "name": "item 1",
        },
      ],
    }
  ]

Upvotes: 1

Related Questions