简体   繁体   中英

Update PayPal Express Checkout Payment with orderId before executing payment

So I made my research online, but I can't find any great explanation on how to use Patch method from the classes PayPal give for express checkout.

My checkout process work in 3Steps.

Step1 The page where everything begin is a simple "Cart Viewer page", Where there is a button "Checkout With PayPal" When we click on that button there is a javascript post request sent to my server asking for it to reply back with a PayPal Link (Where customer will login and approve the transaction)

Step2 After customer approved and come back to my website with a PayPal PaymentId , I fetch this Token and gather customer information to autofill the form with the paypal confirm address they have on file

Step3 Customer select shipping method and click "PayNow" only then I will create a orderId to his name and send an Update to PayPal with shipping cost and OrderId

So here is some code

Step3 Code

//$PaymentId = PayPal Payement Id Customer Returned with from paypal
//PayerId = PayPal PayerId Customer returned with from paypal
//$SubTotal,$ShippingCost,$Tax,$Total,$NewOrderNumber = Are setted with appropriate values.

$PP_Payment = PayPal\Api\Payment::get($PaymentId,$PayPalAPIContext);
$PP_Execution = new PayPal\API\PaymentExecution();
$PP_Execution -> setPayerId($PayerId);

$PP_Transaction = new \PayPal\Api\Transaction();
$PP_Amount = new \PayPal\Api\Amount();
$PP_Details = new \PayPal\Api\Details();

$PP_Details -> setSubtotal($SubTotal);
$PP_Details -> setShipping($ShippingCost);
$PP_Details -> setTax($Tax);

$PP_Amount -> setCurrency('CAD');
$PP_Amount -> setTotal($Total);
$PP_Amount -> setDetails($PP_Details);

$PP_Transaction -> setInvoiceNumber($NewOrderNumber);
$PP_Transaction -> setAmount($PP_Amount);
$PP_Transaction -> setDescription('Order #'.$NewOrderNumber);

$PP_Execution -> addTransaction($PP_Transaction);
$PP_Payment -> setIntent('sale');

$PP_Response = $PP_Payment -> execute($PP_Execution,$PayPalAPIContext);
try{
    if($PP_Response -> getState() == 'approved')
    {
        //Stuff to do when transaction did go tru!
    }
}
catch(\PayPal\Exception\PayPalConnectionException $e)
{
    //Error Handeling Code Here!
}

So even though I set these two

$PP_Transaction -> setInvoiceNumber($NewOrderNumber);
$PP_Transaction -> setAmount($PP_Amount);

It does not apply to trasaction on paypal.

So I made some research on how to do that, and found out I need to use

$PP_Patch = new \PayPal\Api\Patch();

But there is no documentation on what to use to update invoice number and shipping So I tried to improvise with the following code

$PP_Patch = new \PayPal\Api\Patch();
$PP_Patch -> setOp('add')
    ->setPath('/transactions/0')
    ->setValue(json_decode('{
    "invoice_number":'.$NewOrderNumber.'
    }'));

$PP_Payment -> update($PP_Patch,$PayPalAPIContext);

So does anyone has a litle bit of experience with this ? can someone point me out the right dirrection, Or simply a proper documentation that show path to use for the setOp() method

Okay, So there is no documentation at all, for patch method.

To find out the path you need, You will need to look at the create_payment method JSON and from there figure out what you need to add. In my case I needed invoice_number so I needed /transactions/0/invoice_number We can see it here 在此处输入图片说明

So since I didnt had the invoice number in the transaction. I needed to patch op add

$PP_Patch = new \PayPal\Api\Patch();
$PP_Patch -> setOp('add');
$PP_Patch -> setPath('/transactions/0/invoice_number');
$PP_Patch -> setValue($NewOrderNumber);
$PP_PathReq = new \PayPal\Api\PatchRequest();
$PP_PathReq -> setPatches(array($PP_Patch));
$PP_Payment -> update($PP_PathReq,$PayPalAPIContext);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM