PayPal Rest API for Payments 在沙箱中返回 NULL

Posted

技术标签:

【中文标题】PayPal Rest API for Payments 在沙箱中返回 NULL【英文标题】:PayPal Rest API for Payments returns NULL in the sandbox 【发布时间】:2013-03-17 02:42:39 【问题描述】:

我有一个 PayPal 沙盒帐户。我可以在 php 上使用 curl 通过 api 检索令牌,但是处理测试卡只会返回 null。有人看到代码有问题吗?这是 PayPal 沙盒的已知问题吗?下面 sn-p 中的客户端是虚构的,但是,如前所述,使用我的真实凭据我可以成功调用以检索令牌。

生成请求的PHP:

 <?php

    class psPayPal 

    private $tokenUrl = 'https://api.sandbox.paypal.com/v1/oauth2/token';
    private $paymentUrl = 'https://api.sandbox.paypal.com/v1/payments/payment';
    private $client = 'dlkjasdfkja;skdjfaksdjfkajsdkfjaejkjefkekjakdjfaksjdfadjf;ja';
    private $secret = 'klj;akjdfjeieiadfaldkjfelkajsdfkjaejeiejisadif;alkdsfj;kjjie';
    private $token;
    private $tokenHandle;
    private $paymentHandle;

    public function __construct()
    
        $this->tokenHandle = curl_init($this->tokenUrl);
        $this->buildTokenRequest();
    

    public function buildTokenRequest()
    
        $header = array(
            'Accept: application/json',
            'Accept-Language: en_US'
        );

        $user = $this->client . ':' . $this->secret;

        $data = 'grant_type=client_credentials';

        curl_setopt($this->tokenHandle, CURLOPT_HTTPHEADER, $header);
        curl_setopt($this->tokenHandle, CURLOPT_USERPWD, $user);
        curl_setopt($this->tokenHandle, CURLOPT_POSTFIELDS, $data);
        curl_setopt($this->tokenHandle, CURLOPT_RETURNTRANSFER, true);

        $this->commitTokenRequest();
    

    public function commitTokenRequest()
    
        $response = json_decode(curl_exec($this->tokenHandle));

        $this->token = $response->access_token;

        curl_close($this->tokenHandle);
    

    public function buildPaymentRequest($cost = '', $description = '')
    
        $this->paymentHandle = curl_init($this->paymentUrl);

        $header = array(
            'Accept: application/json',
            'Accept-Language: en_US',
            'Authorization:Bearer ' . $this->token
        );

        $data = array(
            'intent' => 'sale',
            'payer' => array(
                'payment_method' => 'credit_card',
                'funding_instruments' => array(
                        array(
                            'credit_card' => array(
                                'number' => '5500005555555559',
                                'type' => 'mastercard',
                                'expire_year'=> '2018',
                                'cvv2'=> '111',
                                'first_name'=> 'Joe',
                                'last_name' => 'Shopper'
                            )
                        )
                )
            ),
            'transactions' => array(
                    array(
                        'amount' => array(
                            'total' => '39.54',
                            'currency' => 'USD'
                        ),
                        'description' => 'This is my descrription for hats'
                    )
            )
        );
        $d = json_encode($data);

        curl_setopt($this->paymentHandle, CURLOPT_HTTPHEADER, $header);
        curl_setopt($this->paymentHandle, CURLOPT_POSTFIELDS, $d);
        curl_setopt($this->paymentHandle, CURLOPT_RETURNTRANSFER, true);

        $this->commitPaymentRequest();
    

    public function commitPaymentRequest()
    
        $response = json_decode(curl_exec($this->paymentHandle));

        var_dump($response);

        curl_close($this->paymentHandle);
    



?>

我尝试了多种选择,但我相信上面的代码是完整的。我还验证了我使用 JSON Lint 发送的 JSON。 JSON 如下:


"intent": "sale",
"payer": 
    "payment_method": "credit_card",
    "funding_instruments": [
        
            "credit_card": 
                "number": "5500005555555559",
                "type": "mastercard",
                "expire_year": "2018",
                "cvv2": "111",
                "first_name": "Joe",
                "last_name": "Shopper"
            
        
    ]
,
"transactions": [
    
        "amount": 
            "total": "39.54",
            "currency": "USD"
        ,
        "description": "This is my descrription for hats"
    
]

任何建议都非常感谢!

【问题讨论】:

你可以试试PayPal SDKgithub.com/paypal/rest-api-sdk-php和create payment sample 【参考方案1】:

原来的解决方案是添加一个标头,Content-Type: application/json。出于某种原因,php 在令牌请求中猜到了正确的 Content-type(可能是因为 Accepts 标头),但是当传递 json-array 时,将 content-type 更改为 application/xml。现在一切都好!

【讨论】:

以上是关于PayPal Rest API for Payments 在沙箱中返回 NULL的主要内容,如果未能解决你的问题,请参考以下文章

使用 Paypal REST API 时需要 API 证书

PayPal REST 令牌请求:api.paypal.com OK,api-m 失败

使用 Paypal REST API 创建付款

使用 paypal/rest-api-sdk-php 的 laravel paypal 集成错误

Paypal 快速结账与 PayPal REST API 的集成

了解 PayPal 的 REST API