'PayPal API v2 - replace payer results that other fields to be added are ignored

I need the payer to be given an order id. According to PayPal API documentation this should be no problem. But as soon as I send the "replace" operation also the "add" fields are ignored.

public function buildRequestBodyUpdate($order, $payer)
{
    return [
        [
            'op' => 'add',
            'path' => "/purchase_units/@reference_id=='default'/custom_id",
            'value' => "ORDER-{$order->id}"
        ],
        [
            'op' => 'add',
            'path' => "/purchase_units/@reference_id=='default'/description",
            'value' => "ORDER-{$order->id}"
        ],
        [
            'op' => 'replace',
            'path' => '/payer',
            'value' => "{$payer->given_name} {$payer->surname} ({$order->id})"
        ]
    ];
}
public function updatePaypalOrder($paypalId, $order)
{
    $request = new OrdersPatchRequest($paypalId);
    $getOrder = $this->client->execute(new OrdersGetRequest($paypalId));
    $request->body = $this->buildRequestBodyUpdate($order, $getOrder->result->payer->name);

    try {
        return $this->client->execute($request);
    } catch (HttpException $ex) {
        $this->_logData($ex);
        return false;
    }
}

called at CartsController

$paypalId = $this->getRequest()->getSession()->read('shopping_cart.paypal_id');

$params = $this->getRequest()->getQueryParams();
$this->Paypal->updatePaypalOrder($paypalId, $order);
$result = $this->Paypal->capturePayment($params);

If i remove the "replace" block it works for the "add" operation. So how to get the "payer" updated, too?



Solution 1:[1]

The syntax of your replace operation seems wrong. But moreover, why are you attempting to put an Order ID in the payer name field? And is this a PayPal order ID or your own ID?

  • If it is a PayPal order ID, as seems to be the case, then: no reason to keep or use this information for any purpose, these order IDs are only used during the approval process and meaningless after a transaction is succesfully captured. Do not store nor keep the PayPal Order ID, except for debug purposes; it has no accounting value. (Store a successful capture response's ID instead, and do so in your own database)

  • If it is your own unique ID (never used for a previously completed transaction) -> pass it in the purchase_unit's invoice_id field. The invoice_id field is visible as part of the PayPal transaction, and enforcing uniqueness for completed payments prevents accidental duplicate payments for the same thing.

  • If it is some other non-unique ID of yours -> use custom_id. This field will be visible to you as part of the transaction, but not visible to the payer.

With one of those changes, there should be no need for a replace operation to "update" the payer name; it is already set to what it needs to be.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1