简体   繁体   English

在执行付款之前,使用orderId更新PayPal Express Checkout付款

[英]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. 因此,我在网上进行了研究,但是在PayPal提供的用于快速结帐的类中找不到关于如何使用Patch方法的很好的解释。

My checkout process work in 3Steps. 我的结帐流程分3步进行。

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) 步骤1一切开始的页面是一个简单的“购物车查看器”页面,其中有一个按钮“使用PayPal结帐”当我们单击该按钮时,有一个javascript发布请求发送到我的服务器,要求它用PayPal Link答复PayPal Link (客户将在此处登录并批准交易)

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 步骤2在客户批准并使用PayPal PaymentId返回我的网站后,我获取此令牌并收集客户信息,以在表单中自动填写他们具有的PayPal PaymentId确认地址。

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 Step3客户选择送货方式并仅单击“ PayNow”,然后我将为其创建一个orderId,并将包含运费和OrderId的更新发送至PayPal

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 有人可以向我指出正确的方向,或者只是一个正确的文档,显示用于setOp()方法的路径

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. 为了找到所需的路径,您将需要查看JSON的create_payment方法,然后从中找出您需要添加的内容。 In my case I needed invoice_number so I needed /transactions/0/invoice_number We can see it here 就我而言,我需要invoice_number所以我需要/transactions/0/invoice_number我们可以在这里看到 在此处输入图片说明

So since I didnt had the invoice number in the transaction. 因此,由于我没有交易中的发票编号。 I needed to patch op add 我需要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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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