Skip to content

Commit

Permalink
First release
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaijoldis committed Jun 3, 2016
1 parent 4f0c742 commit c8448e0
Show file tree
Hide file tree
Showing 3 changed files with 413 additions and 0 deletions.
226 changes: 226 additions & 0 deletions class-virtualmerchant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
<?php
class wpec_merchant_virtualmerchant extends wpsc_merchant {

public function submit(){

// basic credit card verification
$errorMsg = "";

if ( isset( $_POST['CardNumber'] ) && strlen( $_POST['CardNumber'] ) > 0 ) {
$CardNumber = $_POST['CardNumber'];
} else {
$errorMsg .= __( 'Credit Card Number Required', 'wpsc_gold_cart' ) . '<br/>';
}

if ( isset( $_POST['ExpiryMonth'] ) && strlen( $_POST['ExpiryMonth'] ) > 0 ) {
$ExpiryMonth = $_POST['ExpiryMonth'];
} else {
$errorMsg .= __( 'Credit Card Expiry Month Required', 'wpsc_gold_cart' ) . '<br/>';
}

if ( isset( $_POST['ExpiryYear'] ) && strlen( $_POST['ExpiryYear'] ) > 0 ) {
$ExpiryYear = $_POST['ExpiryYear'];
} else {
$errorMsg .= __( 'Credit Card Expiry Year Required', 'wpsc_gold_cart' ) . '<br/>';
}

if ( isset( $_POST['Cvc2'] ) && strlen( $_POST['Cvc2'] ) > 0 ) {
$Cvc2 = $_POST['Cvc2'];
} else {
$errorMsg .= __( 'Credit Card Cvc2 code Required', 'wpsc_gold_cart' ) . '<br/>';
}

if ( strlen( $errorMsg ) > 0 ) {
$this->set_error_message( $errorMsg );
header( 'Location: '.$this->cart_data['shopping_cart_url'] );
exit();
}

$options = get_option( 'wpsc_vmerchnat' );

// temp vars to make things easier
if ( get_option('permalink_structure') != '' ) {
$separator ="?";
} else {
$separator ="&";
}

if ( $options['mode'] == 'test' ) {
// test url goes here
$url = 'https://demo.myvirtualmerchant.com/VirtualMerchantDemo/process.do';
} else {
//live url goes here
$url = 'https://www.myvirtualmerchant.com/VirtualMerchant/process.do';
}

$amount = number_format( $this->cart_data['total_price'], 2, '.', '' );
$sales_tax = $this->cart_data['cart_tax'];
$invoice_number = $this->cart_data['session_id'];
$email = $this->cart_data['email_address'];
$transaction_results_page = $this->cart_data['transaction_results_url'];
$credit_card_date = $ExpiryMonth . '' . $ExpiryYear;

// optional vars
$first_name = $this->cleanInput($this->cart_data['billing_address']['first_name']);
$last_name = $this->cleanInput($this->cart_data['billing_address']['last_name']);
$address2 = $this->cleanInput($this->cart_data['billing_address']['address']);
$city = $this->cleanInput($this->cart_data['billing_address']['city']);
$state = $this->cleanInput($this->cart_data['billing_address']['state']);
$country = $this->cart_data['billing_address']['country'];

// avs vars
if ( $options['avs'] == 'yes' ) {
$avs_zip = $this->cart_data['billing_address']['post_code'];
$avs_address = $this->cleanInput($this->cart_data['billing_address']['address']);
}

$form = '
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title></title>
</head>
<body>
<form id="vmerchant_form" action="' .$url . '" method="POST">
<input type="hidden" name="ssl_transaction_type" value="ccsale">
<input type="hidden" name="ssl_show_form" value="false">
<input type="hidden" name="ssl_merchant_id" value="'. $options['merchant_id'] .'">
<input type="hidden" name="ssl_user_id" value="'. $options['user_id'] .'">
<input type="hidden" name="ssl_pin" value="'. $options['pin'] .'">
<input type="hidden" name="ssl_amount" value="'. $amount .'">
<input type="hidden" name="ssl_salestax" value="'. $sales_tax .'">
<input type="hidden" name="ssl_invoice_number" value="'. $invoice_number . '">
<input type="hidden" name="ssl_email" value="'. $email . '">
<input type="hidden" name="ssl_card_number" value="'. $CardNumber . '">
<input type="hidden" name="ssl_exp_date" value="'. $credit_card_date . '">
<input type="hidden" name="ssl_cvv2cvc2_indicator" value="1">
<input type="hidden" name="ssl_cvv2cvc2" value="'. $Cvc2 . '">
<input type="hidden" name="ssl_receipt_decl_get_url" value="'. $transaction_results_page . '">
<input type="hidden" name="ssl_receipt_apprvl_get_url" value="'. $transaction_results_page . '' .$separator .'">
<input type="hidden" name="ssl_result_format" value="HTML">
<input type="hidden" name="ssl_receipt_decl_method" value="REDG">
<input type="hidden" name="ssl_receipt_apprvl_method" value="REDG">
<input type="hidden" name="ssl_customer_code" value="1111">';
if ( strlen( $first_name ) > 0 ){
$form .= '<input type="hidden" name="ssl_first_name" value="' . $first_name . '">';
}

if ( strlen( $last_name ) > 0 ) {
$form .= '<input type="hidden" name="ssl_last_name" value="' . $last_name . '">';
}

if ( strlen( $address2 ) > 0 ) {
$form .= '<input type="hidden" name="ssl_address2" value="' . $address2 . '">';
}

if ( strlen( $city ) > 0 ) {
$form .= '<input type="hidden" name="ssl_city" value="' . $city . '">';
}

if ( strlen( $state ) > 0 ) {
$form .= '<input type="hidden" name="ssl_state" value="' . $state . '">';
}

if ( strlen( $country ) > 0 ) {
$form .= '<input type="hidden" name="ssl_country" value="' . $country. '">';
}

if ( $options['mode'] == 'test' ) {
$form .= '<input type="hidden" name="ssl_test_mode" value="true">';
} else {
$form .= '<input type="hidden" name="ssl_test_mode" value="false">';
}

if ( $options['avs'] == 'yes' ) {
$form .= '<input type="hidden" name="ssl_avs_address" value="' . $avs_address . '">
<input type="hidden" name="ssl_avs_zip" value="' . $avs_zip . '">';
}

$form .= '
</form>
<script type="text/javascript">document.getElementById("vmerchant_form").submit();</script>
</body>
</html>';

echo $form;
exit();
}

private function cleanInput($strRawText){
$iCharPos = 0;
$chrThisChar = "";
$strCleanedText = "";
$strAllowableChars = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_/\(),.:|";
$blnAllowAccentedChars = TRUE;

//Compare each character based on list of acceptable characters
while ( $iCharPos < strlen( $strRawText ) ) {
// Only include valid characters **
$chrThisChar = substr($strRawText, $iCharPos, 1);
if ( strpos( $strAllowableChars, $chrThisChar ) !== FALSE ) {
$strCleanedText = $strCleanedText . $chrThisChar;
} elseIf ( $blnAllowAccentedChars == TRUE ) {
// Allow accented characters and most high order bit chars which are harmless **
if ( ord( $chrThisChar ) >= 191 ) {
$strCleanedText = $strCleanedText . $chrThisChar;
}
}

$iCharPos = $iCharPos + 1;
}

return $strCleanedText;
}
}

if ( isset( $_GET['ssl_card_number'] ) &&
isset( $_GET['ssl_exp_date'] ) &&
isset( $_GET['ssl_amount'] ) &&
isset( $_GET['ssl_invoice_number']) &&
isset( $_GET['ssl_result_message'] ) &&
isset( $_GET['ssl_txn_id'] ) &&
isset( $_GET['ssl_approval_code'] ) &&
isset( $_GET['ssl_cvv2_response'] ) &&
isset( $_GET['ssl_txn_time'] ) ) {
add_action('init', 'wpec_vmerchant_ipn');
}

function wpec_vmerchant_ipn() {

$sessionid = $_GET['ssl_invoice_number'];

if ( $_GET['ssl_result_message'] == 'APPROVED' || $_GET['ssl_result_message'] == 'APPROVAL' ) {
// success
$purchase_log = new WPSC_Purchase_Log( $sessionid, 'sessionid' );
$purchase_log->set( array(
'processed' => WPSC_Purchase_Log::ACCEPTED_PAYMENT,
'transactid' => $_GET['ssl_txn_id'],
'notes' => 'Virtual Merchant time : "' . $_GET['ssl_txn_time'] . '"',
) );
$purchase_log->save();

// set this global, wonder if this is ok
transaction_results( $sessionid, true );
} else {
// success
$purchase_log = new WPSC_Purchase_Log( $sessionid, 'sessionid' );
$purchase_log->set( array(
'processed' => WPSC_Purchase_Log::INCOMPLETE_SALE,
'transactid' => $_GET['ssl_txn_id'],
'notes' => 'Virtual Merchant time : "' . $_GET['ssl_txn_time'] . '"',
) );
$purchase_log->save();
$error_messages = wpsc_get_customer_meta( 'checkout_misc_error_messages' );
if ( ! is_array( $error_messages ) )
$error_messages = array();
$error_messages[] = '<strong style="color:red">' . urldecode( $_GET['ssl_result_message'] ) . ' </strong>';
wpsc_update_customer_meta( 'checkout_misc_error_messages', $error_messages );
$checkout_page_url = get_option( 'shopping_cart_url' );

if ( $checkout_page_url ) {
header( 'Location: '.$checkout_page_url );
exit();
}
}
}
?>
131 changes: 131 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
function wpec_save_virtualmerchant_settings() {
$options = get_option( 'wpsc_vmerchnat' );

foreach( $_POST['wpsc_vmerchnat'] as $name => $value ) {
$options[$name] = rtrim($value);
}

update_option( 'wpsc_vmerchnat', $options );

return true;
}

function wpec_virtualmerchant_settings_form() {
$args = array(
'user_id' => '',
'merchant_id' => '',
'pin' => '',
'avs' => 'no',
'mode' => 'live'
);
add_option( 'wpsc_vmerchnat', $args );
$options = get_option( 'wpsc_vmerchnat' );
$output = '
<tr>
<td>
'. __( 'Account ID', 'wpsc_gold_cart' ) .'
</td>
<td>
<input type="text" value="'.$options['merchant_id'].'" name="wpsc_vmerchnat[merchant_id]" />
</td>
</tr>
<tr>
<td>
' . __( 'User ID', 'wpsc_gold_cart' ) .'
</td>
<td>
<input type="text" value="'.$options['user_id'].'" name="wpsc_vmerchnat[user_id]" />
</td>
</tr>
<tr>
<td>
' . __( 'Merchant Pin', 'wpsc_gold_cart' ) .'
</td>
<td>
<input type="text" value="'.$options['pin'].'" name="wpsc_vmerchnat[pin]" />
</td>
</tr>
<tr>
<td>
' . __( 'AVS Security', 'wpsc_gold_cart' ) .'
</td>
<td>
<input type="radio" value="yes" name="wpsc_vmerchnat[avs]" ' . checked( 'yes',$options['avs'],false ) .' /><label> ' . TXT_WPSC_YES . ' </label>
<input type="radio" value="no" name="wpsc_vmerchnat[avs]" ' . checked( 'no', $options['avs'],false ) .' /><label> ' . TXT_WPSC_NO . '</label>
</td>
</tr>
<tr>
<td>
' . __( 'Mode', 'wpsc_gold_cart' ) .'
</td>
<td>
<input type="radio" value="live" name="wpsc_vmerchnat[mode]" ' . checked( 'live', $options['mode'],false ) .' /><label> ' . __( 'Live Mode', 'wpsc_gold_cart' ) . ' </label>
<input type="radio" value="test" name="wpsc_vmerchnat[mode]" ' . checked( 'test', $options['mode'],false ) .' /><label> ' . __( 'Test Mode', 'wpsc_gold_cart' ) . '</label>
</td>
</tr>
';
$struc = get_option('permalink_structure');
if ( $struc == '' ) {
$output .= '
<tr>
<td colspan="2">
<strong style="color:red;">'.__( 'This Gateway will only work if you change your permalink structure do anything except the default setting. In Settings->Permalinks', 'wpsc_gold_cart' ).'</strong>
</td>
</tr>
';
}
return $output;
}

function wpec_vmerchant_checkout_fields() {
global $gateway_checkout_form_fields;
if( in_array( 'wpec_virtualmerchant', (array) get_option('custom_gateway_options') ) ) {

$curryear = date( 'Y' );
$curryear_2 = date( 'y' );
$years = '';
//generate year options
for ( $i = 0; $i < 10; $i++ ) {
$years .= "<option value='" . $curryear_2 . "'>" . $curryear . "</option>\r\n";
$curryear++;
$curryear_2++;
}
ob_start(); ?>
<tr>
<td class="wpsc_CC_details"> <?php _e( 'Credit Card Number *', 'wpsc' ); ?></td>
<td>
<input type="text" value='' name="CardNumber" />
</td>
</tr>
<tr>
<td class='wpsc_CC_details'><?php _e( 'Credit Card Expiry *', 'wpsc' ); ?></td>
<td>
<select class='wpsc_ccBox' name='ExpiryMonth'>
<option value='01'>01</option>
<option value='02'>02</option>
<option value='03'>03</option>
<option value='04'>04</option>
<option value='05'>05</option>
<option value='06'>06</option>
<option value='07'>07</option>
<option value='08'>08</option>
<option value='09'>09</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
</select>
<select class='wpsc_ccBox' name='ExpiryYear'>
<?php echo $years; ?>
</select>
</td>
</tr>
<tr>
<td class='wpsc_CC_details'><?php _e( 'CVC *', 'wpsc' ); ?></td>
<td><input type='text' size='4' value='' maxlength='4' name='Cvc2' /></td>
</tr>
<?php
$gateway_checkout_form_fields['wpec_virtualmerchant'] = ob_get_clean();
}
}
add_action( 'wpsc_init', 'wpec_vmerchant_checkout_fields' );
Loading

0 comments on commit c8448e0

Please sign in to comment.