php - Test third party shopping cart (Paypal) -
i trying test third party shopping cart api , empty string result.
this code trying test:
$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);
i $response = "";
i have questions:
- any idea doing wrong?
- which response should get?
- should token api (https://api.sandbox.paypal.com/v1/oauth2/token) in case secret key?
first must post paypal server not array query using http_build_query method - follow example:
$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);
i use separated method sending 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 response details. $httpresponsear = explode("&", $httpresponse); $httpparsedresponsear = array(); foreach ($httpresponsear $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 post request($nvpreq) $api_endpoint."); } return $httpparsedresponsear; }
Comments
Post a Comment