3

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

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

}

任何建议都非常感谢!

4

1 回答 1

5

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

于 2013-03-28T00:24:03.383 回答