从 cURL 请求时的 Paypal Adaptivepayments 问题

Posted

技术标签:

【中文标题】从 cURL 请求时的 Paypal Adaptivepayments 问题【英文标题】:Paypal Adaptivepayments Issue when requesting from cURL 【发布时间】:2015-10-31 04:45:29 【问题描述】:

我在 php 中创建一个简单的 web 应用程序,它使用 paypal 的 adaptivePayments/Pay Api,我使用 POSTMAN 进行了一些测试,一切正常,我发出初始请求,我得到了我的 payKey,也有了这个 payKey,我可以使用 /AdaptivePayments/PaymentDetails Api 检查交易状态,问题是当我尝试通过 php 代码发出请求时:

    $endpoint = 'https://svcs.sandbox.paypal.com/AdaptivePayments/Pay';

    $payload['actionType']                       = "PAY";
    $payload['clientDetails']['applicationId']   = "APP-80W284485P519543T";
    $payload['clientDetails']['ipAddress']       = "xxx.xxx.xxx.xxx";
    $payload['currencyCode']                     = "USD";
    $payload['feesPayer']                        = "EACHRECEIVER";
    $payload['memo']                             = "Transaction";
    $payload['receiverList']['receiver']         = $receivers;
    $payload['requestEnvelope']['errorLanguage'] = "en_US";
    $payload['returnUrl']                        = "URL";
    $payload['cancelUrl']                        = "URL";

    $json = json_encode($payload);

    $ch = curl_init($endpoint);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'X-PAYPAL-SECURITY-USERID: ACCOUNT',
        'X-PAYPAL-SECURITY-PASSWORD: PASSWORD',
        'X-PAYPAL-SECURITY-SIGNATURE: SIGNATURE',
        'X-PAYPAL-REQUEST-DATA-FORMAT: JSON',
        'X-PAYPAL-RESPONSE-DATA-FORMAT: JSON',
        'X-PAYPAL-APPLICATION-ID: APP-80W284485P519543T',
        'Content-Type: application/json',
        'Content-Length: ' . strlen($json)
    ));

    $result = curl_exec($ch);

$receivers 是一个 PHP 数组,其中包含有关电子邮件和付款金额的信息,如下所示:

Array
(
    [0] => Array
        (
            [amount] => 19.8
            [email] => client2-ubs@gmail.com
            [primary] => 
        )


    [1] => Array
        (
            [amount] => 20.7
            [email] => client1-ubs@gmail.com
            [primary] => 
        )


    [2] => Array
        (
            [amount] => 45
            [email] => store-ubs@gmail.com
            [primary] => 1
        )
    )

我没有收到任何错误,我什至得到了用于创建 html 按钮的 payKey,以便用户可以为他的商品付款,问题是当我检查付款状态 /AdaptivePayments/PaymentDetails 时,我得到:


    "responseEnvelope": 
        "timestamp": "2015-08-06T23:59:23.075-07:00",
        "ack": "Success",
        "correlationId": "bab22ca0bd887",
        "build": "17603431"
    ,
    "cancelUrl": "https://23410a33.ngrok.com/universal-bank-of-souls/",
    "currencyCode": "USD",
    "paymentInfoList": null,
    "returnUrl": "https://23410a33.ngrok.com/universal-bank-of-souls/",
    "status": "CREATED",
    "payKey": "AP-8ML17897XK803351A",
    "actionType": "PAY",
    "feesPayer": "EACHRECEIVER",
    "sender": 
        "useCredentials": "false"
    

由于某种奇怪的原因,paypalInfoList 为空,因此用户在单击 paypal 按钮时会看到错误(交易错误)。

正如我之前所说,如果我使用 POSTMAN 进行交易,然后检查状态,我会得到正确的响应:


    "responseEnvelope": 
        "timestamp": "2015-08-07T00:14:08.510-07:00",
        "ack": "Success",
        "correlationId": "b9049ba0d4cf1",
        "build": "17603431"
    ,
    "cancelUrl": "URL",
    "currencyCode": "USD",
    "memo": "Bank of souls payment",
    "paymentInfoList": 
        "paymentInfo": [
            
                "receiver": 
                    "amount": "123.30",
                    "email": "client2-ubs@gmail.com",
                    "primary": "false",
                    "paymentType": "SERVICE",
                    "accountId": "SLKM4ZQ5FMSHG"
                ,
                "pendingRefund": "false"
            ,
            
                "receiver": 
                    "amount": "510.30",
                    "email": "client1-ubs@gmail.com",
                    "primary": "false",
                    "paymentType": "SERVICE",
                    "accountId": "8PUDR7LSRS4MJ"
                ,
                "pendingRefund": "false"
            ,
            
                "receiver": 
                    "amount": "704.00",
                    "email": "store-ubs@gmail.com",
                    "primary": "true",
                    "paymentType": "SERVICE",
                    "accountId": "E29BAQX7C7P3N"
                ,
                "pendingRefund": "false"
            
        ]
    ,
    "returnUrl": "URL",
    "status": "CREATED",
    "payKey": "AP-9MU4806743660523S",
    "actionType": "PAY",
    "feesPayer": "EACHRECEIVER",
    "reverseAllParallelPaymentsOnError": "false",
    "sender": 
        "useCredentials": "false"
    

(paymentInfoList实际上包含一个json对象与货物,但这是使用POSTMAN手动请求)

这可能是 PHP 问题吗?一些调试 curl 连接的想法会对我有很大帮助。

PD:我一整天都在调试这个,我什至打印了 $json 变量(我得到了 json 格式)并直接在邮递员及其作品中复制/粘贴,但由于某种原因,使用 curl 的相同请求不是发送 $receivers 信息。

【问题讨论】:

看起来交易根本没有完成。这就是为什么它仍然显示已创建。获得 PayKey 后,您必须将用户重定向到 PayPal,以便他们可以登录并完成付款(除非您使用的是预先批准密钥,但我看不到您是。)您是否正在执行重定向并实际完成付款? 【参考方案1】:

已解决,我正在使用两个不同的 api 凭据发出请求(创建订单和请求订单信息),而且,当接收到与原始不同的 api 凭据时,paypal 似乎隐藏了订单商品,但仍显示状态订单的(创建、完成等)

【讨论】:

以上是关于从 cURL 请求时的 Paypal Adaptivepayments 问题的主要内容,如果未能解决你的问题,请参考以下文章

如何从php中的paypal curl响应获取付款参考号

不从 PayPal 上的 curl 请求接收令牌

将 paypal curl 获取访问令牌请求转换为 guzzle

将 Paypal cURL 命令转换为 Axios 请求

PayPal REST API 送货地址不适用于 cURL 请求

Paypal API 订阅 curl 沙箱目录产品 权限不足授权失败