Skip to content

Instantly share code, notes, and snippets.

@anthonysbrown
Created December 14, 2016 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonysbrown/7bb7438b1be48d8cc8e2cedb9dcd9897 to your computer and use it in GitHub Desktop.
Save anthonysbrown/7bb7438b1be48d8cc8e2cedb9dcd9897 to your computer and use it in GitHub Desktop.
<?php
// Include required library files.
global $wpdb,$trans_id,$last_postid,$payable_amount;
require_once(TEMPLATIC_PAYPAL_EXPRESS_CHECKOUT_DIR .'/includes/config.php');
require_once(TEMPLATIC_PAYPAL_EXPRESS_CHECKOUT_DIR .'/includes/paypal.class.php');
$post_title = get_post($last_postid);
$post_title = $post_title->post_title;
// Create PayPal object.
$PayPalConfig = array('Sandbox' => $sandbox, 'APIUsername' => $api_username, 'APIPassword' => $api_password, 'APISignature' => $api_signature);
$PayPal = new PayPal($PayPalConfig);
if($_REQUEST['page'] == 'upgradenow'){
$suburl ="&upgrade=pkg";
}
if($_REQUEST['page'] == 'upgradenow'){
$price_package_id=$_SESSION['upgrade_post']['package_select'];
}
else{
$price_package_id=get_post_meta($last_postid,'package_select',true);
}
$package_amount=get_post_meta($price_package_id,'package_amount',true);
$validity=get_post_meta($price_package_id,'validity',true);
$validity_per=get_post_meta($price_package_id,'validity_per',true);
$recurring=get_post_meta($price_package_id,'recurring',true);
$billing_num=get_post_meta($price_package_id,'billing_num',true);
$billing_per=get_post_meta($price_package_id,'billing_per',true);
$billing_cycle=get_post_meta($price_package_id,'billing_cycle',true);
$first_free_trail_period=get_post_meta($price_package_id,'first_free_trail_period',true);
if($recurring==1){
$c=$billing_num;
if($billing_per=='M'){
$rec_type=sprintf('%d Month', $c);
$cycle= 'Month';
}elseif($billing_per=='D'){
$rec_type=sprintf('%d Week', $c/7);
$cycle= 'Week';
}else{
$rec_type=sprintf('%d Year', $c);
$cycle= 'Year';
}
$c_recurrence=$rec_type;
//$c_duration='FOREVER';
$c_duration=$billing_cycle.' '.$cycle;
}
$returnUrl = site_url("/")."?ptype=return&pmethod=paypal_express_checkout&pid=".$last_postid."&trans_id=".$trans_id.$suburl;
$cancel_return = site_url("/")."?ptype=cancel&pmethod=paypal_express_checkout&pid=".$last_postid."&trans_id=".$trans_id;
// Prepare request arrays
$DPFields = array(
'paymentaction' => 'Sale', // How you want to obtain payment. "Authorization" indidicates the payment is a basic auth subject to settlement with Auth & Capture. "Sale" indicates that this is a final sale for which you are requesting payment. Default is Sale.
'ipaddress' => $_SERVER['REMOTE_ADDR'], // Required. IP address of the payer's browser.
'returnfmfdetails' => ''
);
$CCDetails = array(
'creditcardtype' => $CardType, // Required. Type of credit card. Visa, MasterCard, Discover, Amex, Maestro, Solo. If Maestro or Solo, the currency code must be GBP. In addition, either start date or issue number must be specified.
'acct' => $CardNumber, // Required. Credit card number. No spaces or punctuation.
'expdate' => $expDateMonth, // Required. Credit card expiration date. Format is MMYYYY
'cvv2' => $cvv2Number, // Requirements determined by your PayPal account settings. Security digits for credit card.
'startdate' => '', // Month and year that Maestro or Solo card was issued. MMYYYY
'issuenumber' => '' // Issue number of Maestro or Solo card. Two numeric digits max.
);
$PayerInfo = array(
'email' => $email, // Email address of payer.
'firstname' => $firstName, // Required. Payer's first name.
'lastname' => $lastName // Required. Payer's last name.
);
$BillingAddress = array(
'street' => $ad1, // Required. First street address.
'street2' => $ad2, // Second street address.
'city' => $city, // Required. Name of City.
'state' => $state, // Required. Name of State or Province.
'countrycode' => 'US', // Required. Country code.
'zip' => $zip, // Required. Postal code of payer.
'phonenum' => '' // Phone Number of payer. 20 char max.
);
$ShippingAddress = array(
'shiptoname' => $firstName, // Required if shipping is included. Person's name associated with this address. 32 char max.
'shiptostreet' => $ad1, // Required if shipping is included. First street address. 100 char max.
'shiptostreet2' => $ad2, // Second street address. 100 char max.
'shiptocity' => $city, // Required if shipping is included. Name of city. 40 char max.
'shiptostate' => $state, // Required if shipping is included. Name of state or province. 40 char max.
'shiptozip' => $zip, // Required if shipping is included. Postal code of shipping address. 20 char max.
'shiptocountry' => 'US', // Required if shipping is included. Country code of shipping address. 2 char max.
'shiptophonenum' => '' // Phone number for shipping address. 20 char max.
);
$PaymentDetails = array(
'amt' => $amount, // Required. Total amount of order, including shipping, handling, and tax.
'currencycode' => $currencyID, // Required. Three-letter currency code. Default is USD.
'itemamt' => '', // Required if you include itemized cart details. (L_AMTn, etc.) Subtotal of items not including S&H, or tax.
'shippingamt' => '', // Total shipping costs for the order. If you specify shippingamt, you must also specify itemamt.
'insuranceamt' => '', // Total shipping insurance costs for this order.
'shipdiscamt' => '', // Shipping discount for the order, specified as a negative number.
'handlingamt' => '', // Total handling costs for the order. If you specify handlingamt, you must also specify itemamt.
'taxamt' => '', // Required if you specify itemized cart tax details. Sum of tax for all items on the order. Total sales tax.
'desc' => '', // Description of the order the customer is purchasing. 127 char max.
'custom' => '', // Free-form field for your own use. 256 char max.
'invnum' => '', // Your own invoice or tracking number
'notifyurl' => '', // URL for receiving Instant Payment Notifications. This overrides what your profile is set to use.
'recurring' => 'Y',
'ReturnUrl'=> apply_filters('tmpl_returnUrl',$returnUrl), // set return url
'CANCELURL'=> apply_filters('tmpl_cancel_return',$cancel_return) // set Cancel url
);
if(@$recurring == 1)
{
$recurring = array(
'L_BILLINGTYPE0'=>'RecurringPayments',
'L_BILLINGAGREEMENTDESCRIPTION0'=>$post_title
);
$PaymentDetails = array_merge($PaymentDetails,$recurring);
}
// For order items you populate a nested array with multiple $Item arrays. Normally you'll be looping through cart items to populate the $Item
// array and then push it into the $OrderItems array at the end of each loop for an entire collection of all items in $OrderItems.
$OrderItems = array();
$Item = array(
'l_name' => $post_title, // Item Name. 127 char max.
'l_desc' => '', // Item description. 127 char max.
'l_amt' => $payable_amount, // Cost of individual item.
'l_number' => '', // Item Number. 127 char max.
'l_qty' => '', // Item quantity. Must be any positive integer.
'l_taxamt' => '', // Item's sales tax amount.
'l_ebayitemnumber' => '', // eBay auction number of item.
'l_ebayitemauctiontxnid' => '', // eBay transaction ID of purchased item.
'l_ebayitemorderid' => '' // eBay order ID for the item.
);
array_push($OrderItems, $Item);
$Secure3D = array(
'authstatus3d' => '',
'mpivendor3ds' => '',
'cavv' => '',
'eci3ds' => '',
'xid' => ''
);
$PayPalRequestData = array(
'DPFields' => $DPFields,
'CCDetails' => $CCDetails,
'PayerInfo' => $PayerInfo,
'BillingAddress' => $BillingAddress,
'ShippingAddress' => $ShippingAddress,
'PaymentDetails' => $PaymentDetails,
'OrderItems' => $OrderItems
);
// Pass data into class for processing with PayPal and load the response array into $PayPalResult
$PayPalResult = $PayPal->SetExpressCheckout($PayPalRequestData);
$PayPalResult['REQUESTDATA']['RETURNURL'] = apply_filters('tmpl_returnUrl',$PayPalResult['REQUESTDATA']['ReturnURL']);
$PayPalResult['REQUESTDATA']['ReturnURL'] = apply_filters('tmpl_returnUrl',$PayPalResult['REQUESTDATA']['ReturnURL']);
$PayPalResult['REQUESTDATA']['CANCELURL'] = apply_filters('tmpl_cancel_return',$PayPalResult['REQUESTDATA']['CancelURL']);
$PayPalResult['REQUESTDATA']['CancelURL'] = apply_filters('tmpl_cancel_return',$PayPalResult['REQUESTDATA']['CancelURL']);
$ack = @strtoupper($PayPalResult["ACK"]);
$failureMessage = @$PayPalResult['L_LONGMESSAGE0'];
if(isset($ack) && $ack=='FAILURE'){
$_SESSION['paypal_errors'] = $failureMessage;
$cancel_return = apply_filters('tmpl_cancel_return',get_permalink($_REQUEST['submit_page_id'])."/?paypalerror=yes");
echo '<script type="text/javascript">location.href="'.$cancel_return.'";</script>';exit;
}
elseif(isset($PayPalResult['TOKEN']) && $PayPalResult['TOKEN'] != '' && isset($PayPalResult['ACK']) && $PayPalResult['ACK'] == 'Success')
{
$redirect_url = $PayPalResult['REDIRECTURL'];
$returnUrl = $redirect_url;
echo '<script type="text/javascript">location.href="'.$returnUrl.'";</script>';exit;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment