Чтобы успешно опубликовать свой код, необходимо установить некоторые дополнительные параметры.Также я убираю некоторые дополнительные ошибки, которые вы найдете, если сравнить два примера.Основные вещи:
$returnURL
и $cancelURL
должны быть реальными URL и без метода urlencode()
$version
должны быть установлены в urlencode('124.0')
; - Параметры для
SetExpressCheckout
должны быть установлены, как в примере. Я рекомендую тестировать вживую, просто установите цену в $0.01
Следуйте примеруниже:
<br/>/** SetExpressCheckout NVP example; last modified 08MAY23.
*
* Initiate an Express Checkout transaction.
*/
$environment = 'live'; // or 'beta-sandbox' or 'live'<br/>
/**
* Send HTTP POST Request
*
* @param string The API method name
* @param string The POST Message fields in &name=value pair format
* @return array Parsed HTTP Response body
*/
<br/>
function PPHttpPost($methodName_, $nvpStr_) {<br/>
global $environment;<br/>
<br/>
// Set up your API credentials, PayPal end point, and API version.<br/>
$API_UserName = urlencode('super_secret_username');<br/>
$API_Password = urlencode('super_secret_password');<br/>
$API_Signature = urlencode('super_secret_signature');<br/>
$API_Endpoint = "https://api-3t.paypal.com/nvp";<br/>
if("sandbox" === $environment || "beta-sandbox" === $environment) {<br/>
$API_Endpoint = "https://api-3t.$environment.paypal.com/nvp";<br/>
}<br/>
$version = urlencode('124.0');<br/>
<br/>
// Set the curl parameters.<br/>
$ch = curl_init();<br/>
curl_setopt($ch, CURLOPT_URL, $API_Endpoint);<br/>
curl_setopt($ch, CURLOPT_VERBOSE, 1);<br/>
<br/>
// Turn off the server and peer verification (TrustManager Concept).<br/>
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);<br/>
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);<br/>
<br/>
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br/>
curl_setopt($ch, CURLOPT_POST, 1);<br/>
<br/>
// Set the API operation, version, and API signature in the request.<br/>
$nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$API_Password&USER=$API_UserName&SIGNATURE=$API_Signature&$nvpStr_";<br/>
<br/>
// Set the request as a POST FIELD for curl.<br/>
curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);<br/>
<br/>
// Get response from the server.<br/>
$httpResponse = curl_exec($ch);<br/>
if(!$httpResponse) {<br/>
exit("$methodName_ failed: ".curl_error($ch).'('.curl_errno($ch).')');<br/>
}<br/>
<br/>
// Extract the response details.<br/>
$httpResponseAr = explode("&", $httpResponse);<br/>
<br/>
$httpParsedResponseAr = array();<br/>
foreach ($httpResponseAr as $i => $value) {<br/>
$tmpAr = explode("=", $value);<br/>
if(sizeof($tmpAr) > 1) {<br/>
$httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1];<br/>
}<br/>
}<br/>
<br/>
if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr)) {<br/>
exit("Invalid HTTP Response for POST request($nvpreq) to $API_Endpoint.");<br/>
}<br/>
<br/>
return $httpParsedResponseAr;<br/>
}<br/>
// Set request-specific fields.
$paymentAmount = urlencode('105.87');
$currencyID = urlencode('USD'); // or other currency code ('GBP', 'EUR', 'JPY', 'CAD', 'AUD')
$paymentType = urlencode('Authorization'); // or 'Sale' or 'Order'
<br/>
$returnURL = "http://localhost/paypal/new/success.php"; // Please provide a real URL !!!!!<br/>
$cancelURL = 'http://localhost/paypal/new/cencel.php'; // Please provide a real URL !!!!!<br/>
//Parameters for SetExpressCheckout, which will be sent to PayPal <br/>
$paypal_data = array(
'RETURNURL' => $returnURL,<br/>
'CANCELURL' => $cancelURL,<br/>
'PAYMENTREQUEST_0_CURRENCYCODE' => $$currencyID,<br/>
'PAYMENTREQUEST_0_PAYMENTACTION'=> 'SALE',<br/>
'PAYMENTREQUEST_0_DESC' => 'Hosted Saas Tier 1 and Community Management Services',<br/>
'PAYMENTACTION' => $paymentType,<br/>
'L_BILLINGTYPE0' => 'RecurringPayments',<br/>
'L_BILLINGAGREEMENTDESCRIPTION0'=> 'Description of Community Management Services',<br/>
'L_PAYMENTREQUEST_0_NAME0' => 'Community Management Services 8 hours for $0.01',<br/>
'L_PAYMENTREQUEST_0_NUMBER0' => '010101',<br/>
'L_PAYMENTREQUEST_0_QTY0' => '1',<br/>
'L_PAYMENTREQUEST_0_AMT0' => $paymentAmount,<br/>
<br/>
/**
* If you want second recurring payment in the same session
* 'L_BILLINGTYPE1' => 'RecurringPayments',
* 'L_BILLINGAGREEMENTDESCRIPTION1'=> 'Description of Hosted Saas Tier 1',
* 'L_PAYMENTREQUEST_0_NAME1' => 'Hosted Saas Tier 1',
* 'L_PAYMENTREQUEST_0_NUMBER1' => '212121',
* 'L_PAYMENTREQUEST_0_QTY1' => '1',
* 'L_PAYMENTREQUEST_0_AMT1' => '0.02',
*/<br/>
'PAYMENTREQUEST_0_ITEMAMT' => $paymentAmount,
'PAYMENTREQUEST_0_AMT' => $paymentAmount
);
$nvpStr = http_build_query($paypal_data);<br/>
// Execute the API operation; see the PPHttpPost function above.<br/>
$httpParsedResponseAr = PPHttpPost('SetExpressCheckout', $nvpStr);<br/>
if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"])) {<br/>
// Redirect to paypal.com.<br/>
$token = urldecode($httpParsedResponseAr["TOKEN"]);<br/>
$payPalURL = "https://www.paypal.com/webscr&cmd=_express-checkout&token=$token";<br/>
if("sandbox" === $environment || "beta-sandbox" === $environment) {<br/>
$payPalURL = "https://www.$environment.paypal.com/webscr&cmd=_express-checkout&token=$token";<br/>
}<br/>
header("Location: $payPalURL");<br/>
exit;<br/>
} else {<br/>
exit('SetExpressCheckout failed: ' . print_r($httpParsedResponseAr, true));<br/>
}<br/>