Online Book Reader

Home Category

PayPal APIs_ Up and Running_ A Developer's Guide - Michael Balderas [25]

By Root 154 0
format

* @return array Parsed HTTP Response body

*/

function PPHttpPost($methodName_, $nvpStr_) {

global $environment;

// Set up your API credentials, PayPal end point, and API version.

$API_UserName = urlencode('my_api_username');

$API_Password = urlencode('my_api_password');

$API_Signature = urlencode('my_api_signature');

$API_Endpoint = "https://api-3t.paypal.com/nvp";

if("sandbox" === $environment || "beta-sandbox" === $environment) {

$API_Endpoint = "https://api-3t.$environment.paypal.com/nvp";

}

$version = urlencode('51.0');

// Set the curl parameters.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $API_Endpoint);

curl_setopt($ch, CURLOPT_VERBOSE, 1);

// Turn 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);

// Set the API operation, version, and API signature in the request.

$nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$API_Password&USER=$API_UserName".

"&SIGNATURE=$API_Signature$nvpStr_";

// Set the request as a POST FIELD for curl.

curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);

// Get response from the server.

$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;

}

// Set request-specific fields.

$paymentType = urlencode('Authorization'); // or 'Sale'

$firstName = urlencode('customer_first_name');

$lastName = urlencode('customer_last_name');

$creditCardType = urlencode('customer_credit_card_type');

$creditCardNumber = urlencode('customer_credit_card_number');

$expDateMonth = 'cc_expiration_month';

// Month must be padded with leading zero

$padDateMonth = urlencode(str_pad($expDateMonth, 2, '0', STR_PAD_LEFT));

$expDateYear = urlencode('cc_expiration_year');

$cvv2Number = urlencode('cc_cvv2_number');

$address1 = urlencode('customer_address1');

$address2 = urlencode('customer_address2');

$city = urlencode('customer_city');

$state = urlencode('customer_state');

$zip = urlencode('customer_zip');

$country = urlencode('customer_country'); // US or other valid country code

$amount = urlencode('example_payment_amuont');

$currencyID = urlencode('USD'); // or other currency ('GBP', 'EUR', 'JPY', 'CAD', 'AUD')

// Add request-specific fields to the request string.

$nvpStr ="&PAYMENTACTION=$paymentType&AMT=$amount&CREDITCARDTYPE=$creditCardType".

"&ACCT=$creditCardNumber&EXPDATE=$padDateMonth$expDateYear&CVV2=$cvv2Number".

"&FIRSTNAME=$firstName&LASTNAME=$lastName&STREET=$address1&CITY=$city".

"&STATE=$state&ZIP=$zip&COUNTRYCODE=$country&CURRENCYCODE=$currencyID";

// Execute the API operation; see the PPHttpPost function above.

$httpParsedResponseAr = PPHttpPost('DoDirectPayment', $nvpStr);

if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"])||"SUCCESSWITHWARNING"==

strtoupper($httpParsedResponseAr["ACK"])) {

exit('Direct Payment Completed Successfully: '.print_r($httpParsedResponseAr, true));

} else {

exit('DoDirectPayment failed: ' . print_r($httpParsedResponseAr, true));

}

?>

Chapter 4. PayPal Adaptive Payments


Overview of Adaptive Payments


PayPal’s Adaptive Payments allow you to send money in a variety of scenarios. Using Adaptive Payments, you can build a “send money” application for a social networking site or build a robust payment system. Through the Adaptive Payments API, you can build an application that can handle payments, payment preapprovals, and refunds. You can also

Return Main Page Previous Page Next Page

®Online Book Reader