Here is the working code for PayPal's Recurring Payments API (adding tags for the benefit of readers):
<?php
$environment = 'sandbox'; // or 'beta-sandbox' or 'live'
/**
* 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
*/
/*copied (and modified)from DoDirectPaymentReceipt.php*/
require_once 'test/php_nvp_samples/CallerService.php';
session_start();
/**
* Get required parameters from the web form for the request
*/
//$paymentType =urlencode( $_POST['paymentType']);
$firstName =urlencode("xxxx");
$lastName =urlencode("xxxx");
$email = urlencode("xxxx@xxxx.com");
$creditCardType =urlencode( "Visa");
$creditCardNumber = urlencode("4477432722346252"); // dummy test number
$expDateMonth =urlencode("1");
// Month must be padded with leading zero
$padDateMonth = str_pad($expDateMonth, 2, '0', STR_PAD_LEFT);
$expDateYear =urlencode("2010");
$cvv2Number = urlencode("962");
$address1 = urlencode("1 Main Street");
$address2 = urlencode("");
$city = urlencode("xxxx");
$state =urlencode( "xx");
$zip = urlencode("11111");
$amount = urlencode("1.00");
$currencyCode="USD";
$anonymous = urlencode($_POST['anonymous']);// not paypal
/* Construct the request string that will be sent to PayPal.
The variable $nvpstr contains all the variables and is a
name value pair string with & as a delimiter */
$nvpStr="&AMT=$amount&CREDITCARDTYPE=$creditCardType&ACCT=$creditCardNumber&EXPDATE=". $padDateMonth.$expDateYear."&CVV2=$cvv2Number&FIRSTNAME=$firstName&LASTNAME=$lastName&STREET=$address1&CITY=$city&STATE=$state".
"&ZIP=$zip&COUNTRYCODE=US&CURRENCYCODE=$currencyCode";
/**/
function PPHttpPost($methodName_, $nvpStr_) {
global $environment;
require_once 'test/php_nvp_samples/constants.php';
$API_UserName=API_USERNAME;
$API_Password=API_PASSWORD;
$API_Signature=API_SIGNATURE;
$API_Endpoint =API_ENDPOINT;
$version=VERSION;
if("sandbox" === $environment || "beta-sandbox" === $environment) {
$API_Endpoint = "https://api-3t.$environment.paypal.com/nvp";
}
// setting the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $API_Endpoint);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// turning off the server and peer verification(TrustManager Concept).
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);
// NVPRequest for submitting to server
$nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$API_Password&USER=$API_UserName&SIGNATURE=$API_Signature$nvpStr_";
// setting the nvpreq as POST FIELD to curl
curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
// getting response from server
$httpResponse = curl_exec($ch);
if(!$httpResponse) {
exit("$methodName_ failed: ".curl_error($ch).'('.curl_errno($ch).')');
}
// Extract the RefundTransaction 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;
}
$profileStartDate = urlencode("2009-5-7T0:0:0Z");
$billingPeriod = urlencode("Month"); // or "Day", "Week", "SemiMonth", "Year"
$billingFreq = urlencode("1"); // combination of this and billingPeriod must be at most a year
$method = urlencode("CreateRecurringPaymentsProfile");
$paymentAction = urlencode("Sale");
$initAmt = 50;
$failedInitAmtAction = urlencode("ContinueOnFailure");
$desc = urlencode("Recurring $50");
$autoBillAmt = urlencode("AddToNextBilling");
if ($anonymous == 1) {
$profileReference = urlencode("Anonymous");
} else {
$profileReference = urlencode("-");
}
$nvpStr .= "&BILLINGPERIOD=$billingPeriod&BILLINGFREQUENCY=$billingFreq&PROFILESTARTDATE=$profileStartDate&METHOD=$method&PAYMENTACTION=$paymentAction&INITAMT=$initAmt&FAILEDINITAMTACTION=$failedInitAmtAction&DESC=$desc&AUTOBILLAMT=$autoBillAmt&PROFILEREFERENCE=$profileReference";
$httpParsedResponseAr = PPHttpPost('CreateRecurringPaymentsProfile', $nvpStr);
//$resArray=hash_call("CreateRecurringPaymentsProfile",$nvpStr);// testing
if("Success" == $httpParsedResponseAr["ACK"]) {
exit('CreateRecurringPaymentsProfile Completed Successfully: '.print_r($httpParsedResponseAr, true));
} else {
exit('CreateRecurringPaymentsProfile failed: ' . print_r($httpParsedResponseAr, true));
}
?>