在使用Paypal REST API创建付款时设置付款ID时遇到问题

时间:2017-03-24 14:14:59

标签: php rest paypal

您好我是PHP和Paypal的新手,我正在尝试使用Paypal REST API实现快速结账,首先我生成身份验证令牌,到目前为止一直很好,然后我创建了付款和响应是1,但带有响应的JSON在页面上,这是我的代码,我不知道是什么问题。

编辑****我发现问题是我没有调用 $ this-> setDefaultRequest(); 所以CURLOPT_RETURNTRANSFER没有设置为true ,我做了那个改变,工作得很好。

此致

class ExpressCheckout {

private $request = null;

function getConnectionToken(){
        $this->request = curl_init();
        $userName = PPL_REST_API_CLIENT_ID;
        $password = PPL_REST_API_SECRET;
        $paypalmode = (PPL_MODE=='sandbox') ? '.sandbox' : '';
        $url = 'https://api'.$paypalmode.'.paypal.com/v1/oauth2/token';
        $data = 'grant_type=client_credentials';

        $this->setDefaultRequest();
        curl_setopt($this->request, CURLOPT_USERPWD, "$userName:$password");
        curl_setopt($this->request, CURLOPT_POSTFIELDS, $data);

        $response = $this->sendHttp($url);
        return $response->access_token;
    }

    private function setDefaultRequest(){
        curl_setopt($this->request, CURLOPT_VERBOSE, 1);
        curl_setopt($this->request, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($this->request, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($this->request, CURLOPT_TIMEOUT, 45);
        curl_setopt($this->request, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->request, CURLOPT_POST, 1);
    }

    private function sendHttp($url){
        curl_setopt($this->request, CURLOPT_URL, $url);
        $responseJson = json_decode(curl_exec($this->request));
        curl_close ($this->request); 
        return $responseJson;
    }

    function createPayment(){

        $token = $this->getConnectionToken();

        $this->request = curl_init();
        $paypalmode = (PPL_MODE=='sandbox') ? '.sandbox' : '';

        $data = '{
          "intent": "sale",
          "redirect_urls": {
            "return_url": "http://dummy_url/your_redirect_url.html",
            "cancel_url": "http://dummy_url/your_cancel_url.html"
          },
          "payer": {
            "payment_method":"paypal"
          },
          "transactions": [
            {
              "amount":{
                "total":"7.47",
                "currency":"USD"
              }
            }
          ]
        }';
        $url = 'https://api'.$paypalmode.'.paypal.com/v1/payments/payment';
        //EDIT**** set the default curl options
        $this->setDefaultRequest();
        curl_setopt($this->request, CURLOPT_HTTPHEADER, array("Authorization: Bearer $token",
                                                   'Content-Type: application/json')); 
        curl_setopt($this->request, CURLOPT_POSTFIELDS, $data);
        curl_setopt($this->request, CURLOPT_URL, $url);
        $response = curl_exec($this->request);
        curl_close ($this->request); 
        return $response;
    }
}

我正在调用该类的页面是

<?php
    require('../class_lib.php');
    echo '<br>*************<br>';

    echo '<br> create Payment Response : ' . $expressCheckOut->createPayment() . '<br>';

?>

我得到的是这个

*************
{"id":"PAY-9C095019L90508941LDKSKKI","intent":"sale","state":"created","payer":{"payment_method":"paypal"},"transactions":[{"amount":{"total":"7.47","currency":"USD"},"related_resources":[]}],"create_time":"2017-03-24T13:54:49Z","links":[{"href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-9C095019L90508941LDKSKKI","rel":"self","method":"GET"},{"href":"https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-54K94229V6070113S","rel":"approval_url","method":"REDIRECT"},{"href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-9C095019L90508941LDKSKKI/execute","rel":"execute","method":"POST"}]}
create Payment Response : 1

编辑****现在我收到了这个:     *************

create Payment Response : {"id":"PAY-54658290313587457LDKS62I","intent":"sale","state":"created","payer":{"payment_method":"paypal"},"transactions":[{"amount":{"total":"7.47","currency":"USD"},"related_resources":[]}],"create_time":"2017-03-24T14:38:32Z","links":[{"href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-54658290313587457LDKS62I","rel":"self","method":"GET"},{"href":"https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-9KA31292T8253811P","rel":"approval_url","method":"REDIRECT"},{"href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-54658290313587457LDKS62I/execute","rel":"execute","method":"POST"}]}

0 个答案:

没有答案