Paypal Integration in php

function PaymentDetails()
{
global $wpdb;
$current_user = wp_get_current_user();
$postvalues = (object)$_POST;
// Set request-specific fields.
$paymentType = urlencode(‘Authorization’); // or ‘Sale’
$firstName = trim($postvalues->cc_first_name); //card holder first name
$lastName = trim($postvalues->cc_last_name); //card holder last name
$creditCardType = trim($postvalues->cc_type); //card type
$creditCardNumber = trim($postvalues->cc_number); //card number
$padDateMonth = trim($postvalues->expiry_month);
$month = trim($postvalues->billingperoid);
$expDateYear = trim($postvalues->expiry_year);
$amount = trim($postvalues->amount);
$arr1 = str_split($amount);
$arr2 = (array_filter($arr1, “is_numeric”));
$comma_separated = implode(“”, $arr2);
$lastamount = (int)$comma_separated;
$saveammount = $amount;
$cvv2Number = trim($postvalues->cvv_number);
$currencyID = urlencode(‘USD’);// or other currency (‘GBP’, ‘EUR’, ‘JPY’, ‘CAD’, ‘AUD’)
// Add request-specific fields to the request string.

$nvpStr = “&PAYMENTACTION=$paymentType&AMT=$lastamount&CREDITCARDTYPE=$creditCardType&ACCT=$creditCardNumber”.
“&EXPDATE=$padDateMonth$expDateYear&CVV2=$cvv2Number&FIRSTNAME=$firstName&LASTNAME=$lastName”.
“&STREET=$address1&STATE=$state&ZIP=$zip&COUNTRYCODE=$country&CURRENCYCODE=$currencyID”.
“&PROFILESTARTDATE=”.date(“Y-m-d”).”T”.date(“H:i:s”).”Z&DESC=siteDescription&BILLINGPERIOD=Month&BILLINGFREQUENCY=”.$month.”&MAXFAILEDPAYMENTS=1″;

// Execute the API operation; see the PPHttpPost function below.
$httpParsedResponseAr = PHttpPost(‘CreateRecurringPaymentsProfile’, $nvpStr);
//OR
$httpParsedResponseAr = PPHttpPost(‘doDirectPaymentsProfile’, $nvpStr);

if(“SUCCESS” == strtoupper($httpParsedResponseAr[“ACK”]) || “SUCCESSWITHWARNING” == strtoupper($httpParsedResponseAr[“ACK”]))
{
$_SESSION[‘message’] = (object)array(‘message’=>”Your payment has been done successfully.”,’type’=>’success’);
}
else
{
$_SESSION[‘message’] = (object)array(‘message’=>”There is an error while processing your payment.Please try again with correct credentials.”,’type’=> ‘error’);
}
}
}

function PPHttpPost($methodName_,$nvpStr_)
{
$environment = ‘sandbox’; // or ‘beta-sandbox’ or ‘live’
$API_UserName = urlencode(‘abc.testing.in’);//my_api_username
$API_Password = urlencode(‘12345678910’); //my_api_password
$API_Signature = urlencode(‘asdfasdfasdfasdf-R5qoFj3Vwqa0nC5uDRvq’); //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);
//curl_setopt($ch, CURLOPT_PROXY, “255.168.1.9:9858”);
// 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;
}

Leave a Reply