PayPal v2 API Patching failed because of JSON pointer error

I am creating a customer PayPal Class. The current flow is the user orders, clicks paypal, and approves the price. Once approved, we collect the delivery address from PayPal, and get a shipping price. We then update the order with the delivery price and updated Vat and Total Order Value.

This is the error I keep getting:

{“name”:”UNPROCESSABLE_ENTITY”,”details”:[{“field”:”path”,”value”:”/purchase_units/@reference_id==”default”/amount”,”location”:”body”,”issue”:”INVALID_JSON_POINTER_FORMAT”,”description”:”Path should be a valid JSON Pointer https://tools.ietf.org/html/rfc6901 that references a location within the request where the operation is performed.”}],”message”:”The requested action could not be performed, semantically incorrect, or failed business validation.”,”debug_id”:”c*******d”,”links”:[{“href”:”https://developer.paypal.com/docs/api/orders/v2/#error-INVALID_JSON_POINTER_FORMAT”,”rel”:”information_link”,”method”:”GET”}]}

This is the template for the order:

$order = [
            'intent' => 'CAPTURE',
            'application_context' => [
                'return_url' => BASE_URL . 'shoppingcart/view_papa_cart.php?pp=ret', // Replace with your return URL
                'cancel_url' => BASE_URL . 'shoppingcart/view_cart_update.php?pp=can', // Replace with your cancel URL
            ],
            'purchase_units' => [
                [
                    'description' => 'Order', // Update as needed
                    'amount' => [
                        'currency_code' => $_COOKIE['PriceNameCookie'], // Update based on your currency
                        'value' => '0', // The total will be calculated in the loop below
                        'breakdown' => [
                            'item_total' => [
                                'currency_code' => $_COOKIE['PriceNameCookie'], // Update based on your currency
                                'value' => '0', // The total will be calculated in the loop below
                            ],
                            'shipping' => [
                                'currency_code' => $_COOKIE['PriceNameCookie'],
                                'value' => '0' // Initial value for shipping
                            ],
                            'tax_total' => [
                                'currency_code' => $_COOKIE['PriceNameCookie'],
                                'value' => '0' // Initial value for tax (VAT)
                            ]
                        ],
                    ],
                    'items' => [],
                ]
            ],
        ]; 

This is the patch code I want to update:

$patchData = [
            [
                'op' => 'replace',
                'path' => '/purchase_units/@reference_id=="default"/amount',
                'value' =>  [
                    'value' => number_format($newTotal, 2, '.', '')
                ],
                'breakdown' => [
                    'item_total' => [
                        'currency_code' => $_COOKIE['PriceNameCookie'],
                        'value' => number_format($itemTotal, 2, '.', '')
                    ],
                    'shipping' => [
                        'currency_code' => $_COOKIE['PriceNameCookie'],
                        'value' => number_format($deliveryPrice, 2, '.', '')
                    ],
                    'tax_total' => [
                        'currency_code' => $_COOKIE['PriceNameCookie'],
                        'value' => number_format($vatAmount, 2, '.', '')
                    ]
                ]
            ]
        ]; 

Based on the error, it is this part which is causing the issue:

'op' => 'replace',
                'path' => '/purchase_units/@reference_id=="default"/amount',
                'value' =>  [
                    'value' => number_format($newTotal, 2, '.', '')
                ],

I have spend some time trying to figure it out but I can not get the code working properly. I did have a lot of errors but now it’s just this one. Could someone please help and point me in the right direction.

Okay, so the issue was down to how you create the patch json object. This is how it should look:

 $patchData = [
            [
                'op' => 'replace',
                'path' => '/purchase_units/@reference_id=='default'/amount',
                'value' => [
                    'currency_code' => $_COOKIE['PriceNameCookie'],
                    'value' => number_format($newTotal, 2, '.', ''),
                    'breakdown' => [
                        'item_total' => [
                            'currency_code' => $_COOKIE['PriceNameCookie'],
                            'value' => number_format($itemTotal, 2, '.', '')
                        ],
                        'shipping' => [
                            'currency_code' => $_COOKIE['PriceNameCookie'],
                            'value' => number_format($deliveryPrice, 2, '.', '')
                        ],
                        'tax_total' => [
                            'currency_code' => $_COOKIE['PriceNameCookie'],
                            'value' => number_format($vatAmount, 2, '.', '')
                        ]
                    ]
                ]
            ]
        ];

But although this is a fix, if you change the price, you need the user to again re approve the new price. In this case you are going around in circles.

But that is a different problem altogether. I wanted to clear this part up as I spent a long time on it, and the instructions on PayPal are vague as it only demonstrates how to change an address.

I hope this helps someone out there 🙂

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật