-
-
Save anonymous/9200b4540bf196540b58 to your computer and use it in GitHub Desktop.
eway payment gateway for wpmu membership plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Addon Name: eWAY payment plugin | |
Author: Mike Mills (Incsub) | |
Author URI: http://premium.wpmudev.org | |
Gateway ID: eway | |
*/ | |
class eway extends M_Gateway { | |
var $gateway = 'eway'; | |
var $title = 'eWAY'; | |
function api_call($url, $fields) { | |
global $mp; | |
$param_list = array(); | |
foreach ($fields as $k => $v) { | |
$param_list[] = "{$k}=".rawurlencode($v); | |
} | |
$url .= '?' . implode('&', $param_list); | |
//build args | |
$args['user-agent'] = "Membership/3.0.0: http://premium.wpmudev.org/project/membership | eWAY Payment plugin/1.1"; | |
$args['sslverify'] = false; | |
$args['timeout'] = 60; | |
//use built in WP http class to work with most server setups | |
$response = wp_remote_get($url, $args); | |
if (is_wp_error($response) || wp_remote_retrieve_response_code($response) != 200) { | |
$mp->cart_checkout_error( __('There was a problem connecting to eWay. Please try again.', 'mp') ); | |
return false; | |
} else { | |
return $response['body']; | |
} | |
} | |
function eWAY() { | |
parent::M_Gateway(); | |
add_action('M_gateways_settings_' . $this->gateway, array(&$this, 'mysettings')); | |
// If I want to override the transactions output - then I can use this action | |
//add_action('M_gateways_transactions_' . $this->gateway, array(&$this, 'mytransactions')); | |
if($this->is_active()) { | |
// Subscription form gateway | |
add_action('membership_purchase_button', array(&$this, 'display_subscribe_button'), 1, 3); | |
add_action('membership_token_redirect', array(&$this, 'display_subscribe_button'), 1, 3); | |
// Payment return | |
add_action('membership_handle_payment_return_' . $this->gateway, array(&$this, 'handle_eway_return')); | |
add_filter('membership_gateway_exp_window', array(&$this,'eway_expiration_window')); | |
add_action('membership_mark_for_expire', array(&$this,'remove_recurring_line_item'), null, 2); | |
} | |
} | |
function eway_expiration_window($time) { | |
//payment will sometimes send notifications up to 24 hours after a subscription expires, so we need to adjust the window. | |
return "+ 24 hours"; | |
} | |
function remove_recurring_line_item($sub_id, $user_id) { | |
$invoice_id = $this->db->get_var( $this->db->prepare( "SELECT transaction_paypal_ID FROM {$this->subscription_transaction} WHERE transaction_subscription_ID = %s AND transaction_user_ID = %s AND transaction_gateway = %s LIMIT 1", $sub_id, $user_id, $this->gateway ) ); | |
if(empty($invoice_id) || !$invoice_id) { | |
// Don't really know what else to do if we can't find the Invoice ID besides echo an error. | |
echo '<div class="alert alert-error">'.__('Invoice ID could not be determined','membership').'</div>'; | |
} else { | |
$args = array(); | |
$args['headers'] = array( | |
'Authorization' => 'Basic '.base64_encode(get_option( $this->gateway . "_eway_username" )), | |
'Accept' => 'application/json', | |
); | |
$args['user-agent'] = "Membership/3.0.0: http://premium.wpmudev.org/project/membership | eWAY Payment plugin/1.1"; | |
$args['body'] = array('invoice_id' => $invoice_id); | |
$args['sslverify'] = false; | |
$args['timeout'] = 30; | |
$endpoint = "https://au.ewaygateway.com/Request"; | |
$response = wp_remote_post($endpoint, $args); | |
if (is_wp_error($response) || (wp_remote_retrieve_response_code($response) != 200 && wp_remote_retrieve_response_code($response) != 400)) { | |
print '<div class="alert alert-error">'.__('There was a problem connecting to eWAY. Please try again.', 'membership').'</div>'; | |
} else { | |
$response_obj = json_decode($response['body']); | |
$lineitem_id = false; | |
$product_id = false; | |
$subscription = new M_Subscription($sub_id); | |
if($subscription) { | |
$product_id = $this->get_product_id($subscription, $subscription->get_pricingarray()); | |
} | |
if ($response_obj->response_code == "OK" && $product_id != false) { | |
//$product_id = $response_obj->assigned_product_id; | |
foreach($response_obj->sale->invoices as $invoice) { | |
foreach($invoice->lineitems as $lineitem) { | |
if($lineitem->product_id == $product_id) | |
$lineitem_id = $lineitem->lineitem_id; | |
} | |
} | |
} | |
if($lineitem_id == false) { | |
print '<div class="alert alert-error">'.__('There was a problem finding your transaction in eWAY. Please contact an administrator.', 'membership').'</div>'; | |
} else { | |
$args = array(); | |
$args['headers'] = array( | |
'Authorization' => 'Basic '.base64_encode(get_option( $this->gateway . "_eway_username" )), | |
'Accept' => 'application/json', | |
); | |
$args['user-agent'] = "Membership/3.0.0: http://premium.wpmudev.org/project/membership | eWAY Payment plugin/1.1"; | |
$args['body'] = array('lineitem_id' => $lineitem_id); | |
$args['sslverify'] = false; | |
$args['timeout'] = 30; | |
$endpoint = "https://api.ewaypayments.com/CreateAccessCode.json"; | |
$stop_response = wp_remote_post($endpoint, $args); | |
if (is_wp_error($stop_response) || (wp_remote_retrieve_response_code($stop_response) != 200 && wp_remote_retrieve_response_code($stop_response) != 400)) { | |
print '<div class="alert alert-error">'.__('There was a problem connecting to eWAY. Please try again.', 'membership').'</div>'; | |
} else { | |
$stop_response_obj = json_decode($stop_response['body']); | |
if ($response_obj->response_code !== "OK" ) { | |
print '<div class="alert alert-error">'.__('An unknown error prevented eWAY from canceling your payment. Please contact an administrator or cancel your payment from the eWAY website.', 'membership').'</div>'; | |
} else { | |
print '<div class="alert alert-success">'.__('Your recurring payment has been canceled successfully', 'membership').'</div>'; | |
} | |
} | |
} | |
} | |
} | |
} | |
function mysettings() { | |
global $M_options, $M_membership_url; | |
?> | |
<table class="form-table"> | |
<tbody> | |
<tr valign="top"> | |
<th scope="row" colspan="2"><div class="updated below-h2"><p><?php _e('In order for recurring payments to log properly you must enable your global instant notifications url pointing to '.home_url('paymentreturn/' . esc_attr($this->gateway)).' ', 'membership') ?></p></div></th> | |
</tr> | |
<tr valign="top"> | |
<th scope="row"><?php _e('eWAY Username', 'membership') ?></th> | |
<td><input type="text" name="eway_username" value="<?php esc_attr_e(get_option( $this->gateway . "_eway_username" )); ?>" /> | |
</td> | |
</tr> | |
<tr valign="top"> | |
<th scope="row"><?php _e('eWAY Customer ID', 'membership') ?></th> | |
<td><input type="text" name="eway_sid" value="<?php esc_attr_e(get_option( $this->gateway . "_eway_sid" )); ?>" /> | |
</td> | |
</tr> | |
<tr valign="top"> | |
<th scope="row"><?php _e('eWAY Currency', 'membership') ?></th> | |
<td><?php | |
if(empty($M_options['paymentcurrency'])) { | |
$M_options['paymentcurrency'] = 'AUD'; | |
} | |
echo esc_html($M_options['paymentcurrency']); ?></td> | |
</tr> | |
<tr valign="top"> | |
<th scope="row"><?php _e('eWAY Mode', 'membership') ?></th> | |
<td><select name="eway_status"> | |
<option value="live" <?php if (get_option( $this->gateway . "_eway_status" ) == 'live') echo 'selected="selected"'; ?>><?php _e('Live Site', 'membership') ?></option> | |
<option value="test" <?php if (get_option( $this->gateway . "_eway_status" ) == 'test') echo 'selected="selected"'; ?>><?php _e('Test Mode (Sandbox)', 'membership') ?></option> | |
</select> | |
<br /> | |
</td> | |
</tr> | |
<tr valign="top"> | |
<th scope="row"><?php _e('eWAY Language', 'membership') ?></th> | |
<td> | |
<select name="eway_lang"> | |
<?php $lang = get_option($this->gateway.'_eway_lang'); ?> | |
<option value="en" <?php selected($lang,'en'); ?>><?php _e('English','membership') ?></option> | |
<option value="zh" <?php selected($lang,'zh'); ?>><?php _e('Chinese','membership') ?></option> | |
<option value="da" <?php selected($lang,'da'); ?>><?php _e('Danish','membership') ?></option> | |
<option value="fr" <?php selected($lang,'fr'); ?>><?php _e('French','membership') ?></option> | |
<option value="gr" <?php selected($lang,'gr'); ?>><?php _e('German','membership') ?></option> | |
<option value="el" <?php selected($lang,'el'); ?>><?php _e('Greek','membership') ?></option> | |
<option value="it" <?php selected($lang,'it'); ?>><?php _e('Italian','membership') ?></option> | |
<option value="jp" <?php selected($lang,'jp'); ?>><?php _e('Japanese','membership') ?></option> | |
<option value="no" <?php selected($lang,'no'); ?>><?php _e('Norwegian','membership') ?></option> | |
<option value="pt" <?php selected($lang,'pt'); ?>><?php _e('Portuguese','membership') ?></option> | |
<option value="sl" <?php selected($lang,'sl'); ?>><?php _e('Slovenian','membership') ?></option> | |
<option value="es_ib" <?php selected($lang,'es_ib'); ?>><?php _e('Spanish','membership') ?></option> | |
<option value="es_la" <?php selected($lang,'es_la'); ?>><?php _e('Spanish (Latin America)','membership') ?></option> | |
<option value="sv" <?php selected($lang,'sv'); ?>><?php _e('Swedish','membership') ?></option> | |
</select> | |
<br /> | |
</td> | |
</tr> | |
<tr valign="top"> | |
<th scope="row"><?php _e('Skip Order Review Page', 'membership') ?></th> | |
<td><select name="eway_skip_landing"> | |
<?php $skip = get_option($this->gateway.'_eway_skip_landing'); ?> | |
<option value="1" <?php selected($skip,'1'); ?>><?php _e('Yes','membership') ?></option> | |
<option value="0" <?php selected($skip,'0'); ?>><?php _e('No','membership') ?></option> | |
</select> | |
<br /> | |
</td> | |
</tr> | |
<tr valign="top"> | |
<th scope="row"><?php _e('Checkout Style', 'membership') ?></th> | |
<td><select name="eway_checkout_type"> | |
<?php $checkout_type = get_option($this->gateway.'_eway_checkout_type'); ?> | |
<option value="multi" <?php selected($checkout_type,'multi'); ?>><?php _e('Multi Page Checkout','membership') ?></option> | |
<option value="single" <?php selected($checkout_type,'single'); ?>><?php _e('Single Page Checkout','membership') ?></option> | |
</select> | |
<br /> | |
</td> | |
</tr> | |
<tr valign="top"> | |
<th scope="row"><?php _e('Subscription button', 'membership') ?></th> | |
<?php | |
$button = get_option( $this->gateway . "_eway_button", $M_membership_url.'membershipincludes/images/eway-logo.png'); | |
?> | |
<td><input type="text" name="eway_button" value="<?php esc_attr_e($button); ?>" style='width: 40em;' /> | |
<br /> | |
</td> | |
</tr> | |
<tr> | |
<th scope="row"><?php _e('Email Customer (on success)', 'membership') ?></th> | |
<td><select name="email_customer"> | |
<?php | |
$sel_mode = get_option( $this->gateway . "_email_customer", "yes" ); | |
$modes = array( | |
'yes' => __('Yes', 'membership'), | |
'no' => __('No', 'membership') | |
); | |
foreach ($modes as $key => $value) { | |
echo '<option value="' . esc_attr($key) . '"'; | |
if($key == $sel_mode) echo 'selected="selected"'; | |
echo '>' . esc_html($value) . '</option>' . "\n"; | |
} | |
?> | |
</select> | |
</td> | |
</tr> | |
<tr> | |
<th scope="row"><?php _e('Customer Receipt Email Header', 'membership') ?></th> | |
<td><input type="text" name="header_email_receipt" value="<?php esc_attr_e(get_option( $this->gateway . "_header_email_receipt", __("Thanks for your payment!", "membership"))); ?>" /></td> | |
</tr> | |
<tr> | |
<th scope="row"><?php _e('Customer Receipt Email Footer', 'membership') ?></th> | |
<td><input type="text" name="footer_email_receipt" value="<?php esc_attr_e(get_option( $this->gateway . "_footer_email_receipt", "" )); ?>" /></td> | |
</tr> | |
</tbody> | |
</table> | |
<?php | |
} | |
function update() { | |
if(isset($_POST['eway_sid'])) { | |
update_option( $this->gateway . "_eway_username", $_POST[ 'eway_username' ] ); | |
update_option( $this->gateway . "_eway_sid", $_POST[ 'eway_sid' ] ); | |
update_option( $this->gateway . "_currency", $_POST[ 'currency' ] ); | |
update_option( $this->gateway . "_eway_status", $_POST[ 'eway_status' ] ); | |
update_option( $this->gateway . "_eway_button", $_POST[ 'eway_button' ] ); | |
update_option( $this->gateway . "_eway_lang", $_POST[ 'eway_lang' ] ); | |
update_option( $this->gateway . "_eway_skip_landing", $_POST[ 'eway_skip_landing' ] ); | |
update_option( $this->gateway . "_eway_checkout_type", $_POST[ 'eway_checkout_type' ] ); | |
} | |
// default action is to return true | |
return true; | |
} | |
function build_custom($user_id, $sub_id, $amount) { | |
$custom = ''; | |
$custom = time() . ':' . $user_id . ':' . $sub_id . ':'; | |
$key = md5('MEMBERSHIP' . $amount); | |
$custom .= $key; | |
return $custom; | |
} | |
/** | |
* Get product ID | |
* | |
* Search for the product, on failure to find it add it and return the | |
* new product id. If found return the product id. | |
* | |
* @param Object $subscription Subscription | |
* @param Array $pricing Pricing plan | |
* @return Integer $product_id | |
*/ | |
function get_product_id($subscription, $pricing) { | |
$product_id = 0; | |
$args = array(); | |
$args['headers'] = array( | |
'Authorization' => 'Basic '.base64_encode(get_option( $this->gateway . "_eway_username" )), | |
'Accept' => 'application/json', | |
); | |
$args['user-agent'] = "Membership/3.0.0: http://premium.wpmudev.org/project/membership | eWAY Payment plugin/1.1"; | |
$args['body'] = array('product_id' => $subscription->sub_id()); | |
$args['sslverify'] = false; | |
$args['timeout'] = 10; | |
$endpoint = "https://au.ewaygateway.com/Request"; | |
$response = wp_remote_post($endpoint."list_products", $args); | |
if (is_wp_error($response) || (wp_remote_retrieve_response_code($response) != 200 && wp_remote_retrieve_response_code($response) != 400)) { | |
print __('There was a problem connecting to eWAY. Please try again. REF:'.wp_remote_retrieve_response_code($response), 'membership'); | |
} else { | |
$response_obj = json_decode($response['body']); | |
$found = false; | |
if ($response_obj->products && is_array($response_obj->products) && count($response_obj->products) > 0) { | |
foreach ($response_obj->products as $product) { | |
if ($subscription->sub_id() == $product->vendor_product_id) { | |
$found = true; | |
break; | |
} | |
} | |
} | |
if (!$found) { | |
if (count($pricing) < 2) { | |
$bargs = array( | |
'name' => $subscription->sub_name(), | |
'description' => $subscription->sub_description(), | |
'vendor_product_id' => $subscription->sub_id(), | |
'approved_url' => trailingslashit(get_option('home')) . 'paymentreturn/' . esc_attr($this->gateway), | |
'tangible' => 0, | |
'price' => number_format($pricing[0]['amount'], 2), | |
); | |
if (isset($pricing[0]['type']) && $pricing[0]['type'] == 'serial') { | |
$bargs['recurring'] = 1; | |
$bargs['recurrence'] = $this->convert_duration($pricing[0]['period'], $pricing[0]['unit']); | |
$bargs['duration'] = 'Forever'; | |
} | |
$body = array(); | |
foreach ($bargs as $bkey => $bval) { | |
$body[$bkey] = $bval; | |
} | |
$args['body'] = $body; | |
$response = wp_remote_post($endpoint."create_product", $args); | |
if (is_wp_error($response) || (wp_remote_retrieve_response_code($response) != 200 && wp_remote_retrieve_response_code($response) != 400)) { | |
print __('There was a problem connecting to eWAY. Please try again.', 'membership'); | |
} else { | |
$response_obj = json_decode($response['body']); | |
if ($response_obj->response_code == "OK") { | |
$product_id = $response_obj->assigned_product_id; | |
} | |
} | |
} | |
} else { | |
$product_id = $product->assigned_product_id; | |
} | |
} | |
return $product_id; | |
} | |
function convert_duration($length, $unit) { | |
if (($length%7) == 0 && $unit == 'd') { | |
return intval($length/7) . " Week"; | |
} | |
if ($unit == 'w') { | |
return "{$length} Week"; | |
} | |
if ($unit == 'm') { | |
return "{$length} Month"; | |
} | |
if ($unit == 'y') { | |
return "{$length} Year"; | |
} | |
} | |
function single_sub_button($pricing, $subscription, $user_id, $repeat = false) { | |
global $M_options, $M_membership_url; | |
if(empty($M_options['paymentcurrency'])) { | |
$M_options['paymentcurrency'] = 'USD'; | |
} | |
// Fetch product_id | |
//$product_id = $this->get_product_id($subscription, $pricing); | |
$form = ''; | |
if (get_option( $this->gateway . "_eway_status" ) == 'live') { | |
$form .= '<form action="https://api.ewaypayments.com/CreateAccessCode.json" method="post">'; | |
} else { | |
$form .= '<form action="https://api.sandbox.ewaypayments.com/CreateAccessCode.json" method="post">'; | |
} | |
if (get_option( $this->gateway . "_eway_status" ) != 'live') { | |
// $form .= '<input type="hidden" name="demo" value="Y">'; | |
} | |
$form .= '<input type="hidden" name="ewayCustomerID" value="' . esc_attr(get_option( $this->gateway . "_eway_sid" )) . '">'; | |
$form .= '<input type="hidden" name="mode" value="eWAY" />'; | |
$form .= '<input type="hidden" name="li_1_type" value="product" />'; | |
$form .= '<input type="hidden" name="li_1_name" value="'.$subscription->sub_name().'" />'; | |
$form .= '<input type="hidden" name="li_1_price" value="'.number_format($pricing[0]['amount'], 2).'" />'; | |
$form .= '<input type="hidden" name="li_1_tangible" value="N" />'; | |
$form .= '<input type="hidden" name="li_1_product_id" value="'.$subscription->id.'" />'; | |
$form .= '<input type="hidden" name="li_1_description" value="'.$subscription->sub_description().'" />'; | |
if($repeat) { | |
$form .= '<input type="hidden" name="li_1_recurrence" value="'.$this->convert_duration($pricing[0]['period'], $pricing[0]['unit']).'" />'; | |
$form .= '<input type="hidden" name="li_1_duration" value="Forever" />'; | |
} | |
$form .= '<input type="hidden" name="merchant_order_id" value="'.$subscription->id.':'.$user_id.'" />'; | |
$form .= '<input type="hidden" name="skip_landing" value="'.esc_attr(get_option($this->gateway.'_eway_skip_landing')).'" />'; | |
$form .= '<input type="hidden" name="lang" value="'.esc_attr(get_option( $this->gateway . "_eway_lang" )).'" />'; | |
$form .= '<input type="hidden" name="user_id" value="'.$user_id.'">'; | |
$user_data = get_userdata($user_id); | |
if($user_data) | |
$form .= '<input type="hidden" name="email" value="'.$user_data->data->user_email.'" />'; | |
$form .= '<input type="hidden" name="currency" value="'.$M_options['paymentcurrency'].'">'; | |
$form .= '<input type="hidden" name="return_url" value="'.trailingslashit(get_option('home')) . 'paymentreturn/' . esc_attr($this->gateway).'" />'; | |
$form .= '<input type="hidden" name="return_method" value="1" />'; | |
//$form .= '<input type="hidden" name="return_url" value="'.M_get_registrationcompleted_permalink().'" />'; | |
$button = get_option( $this->gateway . "_eway_button", $M_membership_url . 'membershipincludes/images/eway-logo.png' ); | |
// $button = get_option( $this->gateway . "_subscribe_button" ); | |
$form .= '<input type="image" name="submit" border="0" src="' . $button . '" alt="Pay via eWAY">'; | |
$form .= '</form>'; | |
return $form; | |
} | |
function complex_sub_button($pricing, $subscription, $user_id) { | |
global $M_options, $M_membership_url; | |
if(empty($M_options['paymentcurrency'])) { | |
$M_options['paymentcurrency'] = 'AUD'; | |
} | |
// Fetch product_id | |
$product_id = $this->get_product_id($subscription, $pricing); | |
// No step between - direct to payment screen | |
// Eway payment in PHP | |
$pathvalue = "http://122.201.80.175/~orthopt/register/?"; | |
$ewayurl.="?CustomerID=" . esc_attr(get_option( $this->gateway . "_eway_sid" )); | |
$ewayurl.="&UserName=" . esc_attr(get_option( $this->gateway . "_eway_username" )); | |
$ewayurl.="&Amount=" . number_format($pricing[0]['amount'], 2); | |
$ewayurl.="&Currency=" . $M_options['paymentcurrency']; | |
$ewayurl.="&Language=" . esc_attr(get_option( $this->gateway . "_eway_lang" )); | |
//$ewayurl.="&CustomerFirstName=John"; | |
//$ewayurl.="&CustomerLastName=Doe"; | |
//$ewayurl.="&CustomerAddress=123 ABC Street"; | |
//$ewayurl.="&CustomerCity=Sydney"; | |
//$ewayurl.="&CustomerState=NSW"; | |
//$ewayurl.="&CustomerPostCode=2000"; | |
//$ewayurl.="&CustomerCountry=Australia"; | |
$ewayurl.="&CustomerEmail=" . $user_data->data->user_email; | |
//$ewayurl.="&CustomerPhone=1800 10 65 65"; | |
$ewayurl.="&InvoiceDescription=" . $subscription->sub_name(); | |
$ewayurl.="&CancelURL=".$pathvalue."ewaysharedpage.php"; | |
//$ewayurl.="&ReturnUrl=" . M_get_registrationcompleted_permalink(); | |
//$ewayurl.="&ReturnUrl=" . apply_filters( 'membership_notify_url_' . $this->gateway, trailingslashit(get_option('home')) . 'paymentreturn/' . esc_attr($this->gateway)); | |
$ewayurl.="&ReturnUrl=" . apply_filters( 'membership_notify_url_' . $this->gateway, trailingslashit(get_option('home')) . 'paymentreturn/eway'); | |
//$ewayurl.="&ReturnUrl=" . apply_filters( 'membership_notify_url_' . $this->gateway, trailingslashit(get_option('home')) . 'resources/australian-orthoptic-journal/'); | |
//$ewayurl.="&ReturnUrl=" . apply_filters( 'membership_notify_url_' . $this->gateway, trailingslashit(get_option('home')) . 'welcome/'); | |
$ewayurl.="&MerchantReference=" . $subscription->id; | |
$ewayurl.="&MerchantInvoice="; | |
$ewayurl.="&MerchantOption1=" . $user_id; | |
$ewayurl.="&MerchantOption2=" . $subscription->sub_description(); | |
$ewayurl.="&MerchantOption3="; | |
$ewayurl.="&ModifiableCustomerDetails="; | |
$ewayurl.="&PageTitle=eWAY Hosted Payment Page"; | |
$ewayurl.="&PageDescription=Membership Registration"; | |
// $ewayurl.="&PageFooter=".$_POST['PageFooter']; | |
$ewayurl.="&CompanyName=Orthoptics Australia"; | |
// $ewayurl.="&CompanyLogo=".$_POST['CompanyLogo']; | |
// $ewayurl.="&PageBanner=".$_POST['(PageBanner']; | |
$spacereplace = str_replace(" ", "%20", $ewayurl); | |
$posturl="https://au.ewaygateway.com/Request/$spacereplace"; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $posturl); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_HEADER, 1); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); | |
if (CURL_PROXY_REQUIRED == 'True') | |
{ | |
$proxy_tunnel_flag = (defined('CURL_PROXY_TUNNEL_FLAG') && strtoupper(CURL_PROXY_TUNNEL_FLAG) == 'FALSE') ? false : true; | |
curl_setopt ($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_tunnel_flag); | |
curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); | |
curl_setopt ($ch, CURLOPT_PROXY, CURL_PROXY_SERVER_DETAILS); | |
} | |
$response = curl_exec($ch); | |
function fetch_data($string, $start_tag, $end_tag) | |
{ | |
$position = stripos($string, $start_tag); | |
$str = substr($string, $position); | |
$str_second = substr($str, strlen($start_tag)); | |
$second_positon = stripos($str_second, $end_tag); | |
$str_third = substr($str_second, 0, $second_positon); | |
$fetch_data = trim($str_third); | |
return $fetch_data; | |
} | |
$responsemode = fetch_data($response, '<result>', '</result>'); | |
$responseurl = fetch_data($response, '<uri>', '</uri>'); | |
if($responsemode=="True") | |
{ | |
header("location: ".$responseurl); | |
exit; | |
} | |
else | |
{ | |
header("location: ewaysharedpage.php?action=error"); | |
exit; | |
} | |
} | |
function build_subscribe_button($subscription, $pricing, $user_id, $sublevel) { | |
if(!empty($pricing)) { | |
// check to make sure there is a price in the subscription | |
// we don't want to display free ones for a payment system | |
$free = true; | |
foreach($pricing as $key => $price) { | |
if(!empty($price['amount']) && $price['amount'] > 0 ) { | |
$free = false; | |
} | |
} | |
if(!$free) { | |
if(count($pricing) == 1) { | |
// A basic price or a single subscription | |
if(in_array($pricing[0]['type'], array('indefinite','finite'))) { | |
// one-off payment | |
return $this->single_sub_button($pricing, $subscription, $user_id, false); | |
} else { | |
// simple subscription | |
return $this->single_sub_button($pricing, $subscription, $user_id, true); | |
} | |
} else { | |
// something much more complex | |
return $this->complex_sub_button($pricing, $subscription, $user_id); | |
} | |
} | |
} | |
} | |
function display_subscribe_button($subscription, $pricing, $user_id, $sublevel = 1) { | |
if(isset($pricing[$sublevel - 1]) && $pricing[$sublevel - 1]['amount'] < 1) | |
echo $this->single_free_button($pricing, $subscription, $user_id, $sublevel); | |
else | |
echo $this->build_subscribe_button($subscription, $pricing, $user_id, $sublevel); | |
} | |
function display_cancel_button($subscription, $pricing, $user_id) { | |
echo '<form class="unsubbutton" action="' . M_get_subscription_permalink() . '" method="post">'; | |
wp_nonce_field('cancel-sub_' . $subscription->sub_id()); | |
echo "<input type='hidden' name='action' value='unsubscribe' />"; | |
echo "<input type='hidden' name='gateway' value='" . $this->gateway . "' />"; | |
echo "<input type='hidden' name='subscription' value='" . $subscription->sub_id() . "' />"; | |
echo "<input type='hidden' name='user' value='" . $user_id . "' />"; | |
echo "<input type='submit' name='submit' value=' " . __('Unsubscribe', 'membership') . " ' class='button white' />"; | |
echo "</form>"; | |
} | |
// Return stuff | |
function handle_eway_return() { | |
// BMD mod | |
$CustomerID = esc_attr(get_option( $this->gateway . "_eway_sid" )); | |
$UserName = esc_attr(get_option( $this->gateway . "_eway_username" )); | |
$querystring = "CustomerID=" . $CustomerID . "&UserName=" . $UserName . "&AccessPaymentCode=".$_REQUEST['AccessPaymentCode']; | |
$posturl="https://au.ewaygateway.com/Result/?".$querystring; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $posturl); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_HEADER, 1); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); | |
if (CURL_PROXY_REQUIRED == 'True') | |
{ | |
$proxy_tunnel_flag = (defined('CURL_PROXY_TUNNEL_FLAG') && strtoupper(CURL_PROXY_TUNNEL_FLAG) == 'FALSE') ? false : true; | |
curl_setopt ($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_tunnel_flag); | |
curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); | |
curl_setopt ($ch, CURLOPT_PROXY, CURL_PROXY_SERVER_DETAILS); | |
} | |
function fetch_data($string, $start_tag, $end_tag) | |
{ | |
$position = stripos($string, $start_tag); | |
$str = substr($string, $position); | |
$str_second = substr($str, strlen($start_tag)); | |
$second_positon = stripos($str_second, $end_tag); | |
$str_third = substr($str_second, 0, $second_positon); | |
$fetch_data = trim($str_third); | |
return $fetch_data; | |
} | |
$response = curl_exec($ch); | |
$authecode = fetch_data($response, '<authCode>', '</authCode>'); | |
$responsecode = fetch_data($response, '<responsecode>', '</responsecode>'); | |
$retrunamount = fetch_data($response, '<returnamount>', '</returnamount>'); | |
$trxnnumber = fetch_data($response, '<trxnnumber>', '</trxnnumber>'); | |
$trxnstatus = fetch_data($response, '<trxnstatus>', '</trxnstatus>'); | |
$trxnresponsemessage = fetch_data($response, '<trxnresponsemessage>', '</trxnresponsemessage>'); | |
// user_id | |
$merchantoption1 = fetch_data($response, '<merchantoption1>', '</merchantoption1>'); | |
// subscription sub_description | |
$merchantoption2 = fetch_data($response, '<merchantoption2>', '</merchantoption2>'); | |
$merchantoption3 = fetch_data($response, '<merchantoption3>', '</merchantoption3>'); | |
// subscription id | |
$merchantreference = fetch_data($response, '<merchantreference>', '</merchantreference>'); | |
$merchantinvoice = fetch_data($response, '<merchantinvoice>', '</merchantinvoice>'); | |
// end BMD mod | |
// Return handling code | |
global $M_options, $M_membership_url; | |
$return = array(); | |
// No SSl required for return | |
//if($_SERVER['HTTPS'] != 'on') { | |
// wp_die(__('You must use HTTPS in order to do this','membership')); | |
// exit; | |
//} | |
// no coupon | |
// $coupon_code = (isset($_REQUEST['remove_coupon']) ? '' : $_REQUEST['coupon_code']); | |
if(empty($M_options['paymentcurrency'])) { | |
$M_options['paymentcurrency'] = 'AUD'; | |
} | |
$subscription = new M_Subscription($merchantreference); | |
$pricing = $subscription->get_pricingarray(); | |
// if(!empty($coupon_code)) | |
// $pricing = $subscription->apply_coupon_pricing($coupon_code,$pricing); | |
$pricing = $retrunamount; | |
$user_id = ( is_user_logged_in() ? get_current_user_id() : $merchantoption1 ); | |
$user = get_userdata($user_id); | |
$sub_id = $merchantreference; | |
// A basic price or a single subscription | |
if($pricing) { | |
$timestamp = time(); | |
// 00 - Transaction Approved - Successful | |
if ($responsecode == "00") { | |
$status = __('Processed','membership'); | |
$note = ''; | |
$member = new M_Membership($user_id); | |
if($member) { | |
if($member->has_subscription() && $member->on_sub($sub_id)) { | |
remove_action( 'membership_expire_subscription', 'membership_record_user_expire', 10, 2 ); | |
remove_action( 'membership_add_subscription', 'membership_record_user_subscribe', 10, 4 ); | |
$member->expire_subscription($sub_id); | |
$member->create_subscription($sub_id, $this->gateway); | |
} else { | |
$member->create_subscription($sub_id, $this->gateway); | |
} | |
} | |
// TODO: create switch for handling different authorize aim respone codes | |
$this->record_transaction($user_id, $sub_id, $amount, $M_options['paymentcurrency'], time(), ( $payment->results[6] == 0 ? 'TESTMODE' : $payment->results[6]) , $status, $note); | |
do_action('membership_payment_subscr_signup', $user_id, $sub_id); | |
$return['status'] = 'success'; | |
$return['redirect'] = (!strpos(home_url(),'https:') ? str_replace('https:','http:',M_get_registrationcompleted_permalink()) : M_get_registrationcompleted_permalink()); | |
} else { | |
$return['status'] = 'error'; | |
$return['errors'][] = __('Your payment was declined. Please check all your details or use a different card.','membership'); | |
} | |
} else { | |
$return['status'] = 'error'; | |
$return['errors'][] = __('There was an issue determining the price.','membership'); | |
} | |
header("location: ".$return); | |
exit; | |
} | |
} | |
M_register_gateway('eway', 'eway'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment