测试第三方购物车(Paypal)

时间:2015-10-10 12:36:14

标签: php curl paypal paypal-sandbox

我正在尝试测试第三方购物车api并且总是得到空字符串。

这是我试图测试的代码:

$ch = curl_init();
    $params = array();
    $params["cmd"]= "_cart";
    $url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
    $params["upload"] = 1;
    $params["business"] = "my business email";
    $params["item_name_1"] = "Item Name 1";
    $params["amount_1"]  = "1.00";
    $params["shipping_1"] = "1.75";
    $params["item_name_2"] = "Item Name 2";
    $params["amount_2"] = "2.00";
    $params["shipping_2"] ="2.50";
    $params["currency_code"] = "USD";
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($params));
    $response = curl_exec($ch);
    $curl_errno = curl_errno($ch);
    $curl_error = curl_error($ch);
    curl_close($ch);

我得到$response = "";

我有一些问题:

  1. 知道我做错了吗?
  2. 我应该得到哪些回复?
  3. 在这种情况下我应该使用令牌api(https://api.sandbox.paypal.com/v1/oauth2/token)来获取密钥吗?

1 个答案:

答案 0 :(得分:0)

首先,你必须使用http_build_query方法发送到paypal服务器而不是像数组一样 - 请按照我的例子:

$padata['L_BILLINGAGREEMENTDESCRIPTION0']   = 'product description';
$padata['L_PAYMENTREQUEST_0_DESC0']         = $padata['L_BILLINGAGREEMENTDESCRIPTION0'] . ' price - 0,01$';
$padata['PAYMENTREQUEST_0_NOTIFYURL']       = 'http://yourdomain.com/paypal/ipn';
$padata['PAYMENTREQUEST_0_DESC']            = 'product name'; //enter code here
$padata['CANCELURL']                        = ''http://yourdomain.com/paypal/cancelurl';
$padata['RETURNURL']                        = ''http://yourdomain.com/paypal/returnurl';
$padata['PAYMENTREQUEST_0_CURRENCYCODE']    = 'USD';
$padata['PAYMENTREQUEST_0_PAYMENTACTION']   = 'SALE';
$padata['PAYMENTREQUEST_0_ITEMAMT']         = $product->price;
$padata['PAYMENTREQUEST_0_AMT']             = $product->price;
$padata['L_BILLINGTYPE0']                   = 'RecurringPayments';
$padata['L_PAYMENTREQUEST_0_NAME0']         = 'product name';
$padata['L_PAYMENTREQUEST_0_NUMBER0']       = 223; // 'product ID '
$padata['L_PAYMENTREQUEST_0_QTY0']          = '1';
$padata['L_PAYMENTREQUEST_0_AMT0']          = $product->price;

$paypal_data            = http_build_query($padata);
$httpParsedResponseAr   = $this->PPHttpPost('SetExpressCheckout', $paypal_data);

我使用分开的方法将数据发送到paypal api:

private function PPHttpPost( $methodName_, $nvpStr_ ) {

    $api_username   = 'yourapiemail@paypal.com';
    $api_password   = '111111';
    $api_signature  = 'ASDdsasdaASDasdaDASDFzMjEzStQBLyke6Pi4vQhAy3JWRsdOpQybmHjpzasdsad.fT';
    $api_endpoint   = "https://api-3t.paypal.com/nvp";
    $version        = '124.0';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $api_endpoint);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);

    $nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$api_password&USER=$api_username&SIGNATURE=$api_signature&$nvpStr_";

    curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
    $httpResponse = curl_exec($ch);

    if(!$httpResponse) {
        exit("$methodName_ failed: ".curl_error($ch).'('.curl_errno($ch).')');
    }

    // Extract the response details.
    $httpResponseAr = explode("&", $httpResponse);

    $httpParsedResponseAr = array();
    foreach ($httpResponseAr as $i => $value) {
        $tmpAr = explode("=", $value);
        if(sizeof($tmpAr) > 1) {
            $httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1];
        }
    }

    if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr)) {
        exit("Invalid HTTP Response for POST request($nvpreq) to $api_endpoint.");
    }

    return $httpParsedResponseAr;
}