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

标签 php api rest paypal payment

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

生成请求的 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"
    }
]

非常感谢任何建议!

最佳答案

原来解决方案是添加一个 header ,Content-Type: application/json。出于某种原因,php 在 token 请求中猜测了正确的内容类型(大概是因为 Accepts header )但是当传递 json 数组时,将内容类型更改为 application/xml。现在一切都好!

关于php - PayPal Rest API for Payments 在沙箱中返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15651716/

相关文章:

php - 使用 PHP 和 curl 的标题顺序

php - 将脚本从 mysql* 重写为 PDO

api - 如何安全地让我的用户使用刷新 token 登录?

JavaScript 数组遍历手动工作,但循环不

android - GSON 通用 : failed to deserialized json object

php - CodeIgniter Performance 多次加载 View 与 View 循环

PHP 如果日期早于 X 天

api - "Google Maps API Keyboard Shortcuts"弹出窗口在 Laravel Blade 中的 "Enter"上打开

java - 将用户主体从 REST 传播到 EJB 层

php - 使用 PHP 发送/接收 PUT 请求,在解析请求正文时遇到问题?