Commerce
|
|
|
|
<?php /* Author: Daniel Anderson */
/* http://www.dattrix.com/ */
/* GoEMerchant.com Gateway PHP Class*/
/* Requires: CURL */
class GoEmerchant {
/* Specify your username and password */
var $merchant = "USERNAME";
var $password = "PASSWORD";
/* You can set this to 1, and it will not send the transaction. It will just return whatever you specify for debug_return */
var $debug = 0;
var $debug_return = 0;
/* An array that stores the last data sent to the class */
var $recent_transaction = array("success"=>"","authcode"=>"","authresponse"=>"","avs_code"=>"","orderid_given"=>"","orderid_returned"=>"","total"=>"","cardname"=>"","cardnum1"=>"","cardnum2"=>"","cardnum3"=>"","cardnum4"=>"","cardexpm"=>"","cardexpy"=>"","cvv2"=>"","nameoncard"=>"","cardstreet"=>"","cardcity"=>"","cardstate"=>"","cardzip"=>"","cardcountry"=>"");
/* Will be 1 if Curl is available */
var $curl_available;
/* Array that will store errors */
var $error;
/* This function runs automatically upon object creation to determine if Curl is available*/
function GoEmerchant(){
if(extension_loaded("curl"))
$this->curl_available = 1;
else
$this->curl_available = 0;
}
/* This does the processing, the $orderid argument is your own order ID number, everything else should be pretty self-explanatory , default Country is US*/
function process_credit_card($orderid,$total,$cardnum1,$cardnum2,$cardnum3,$cardnum4,$cardexpm,$cardexpy,$cvv2,$nameoncard,$cardstreet,$cardcity,$cardstate,$cardzip,$cardcountry = "US"){
/* If you set debug to 1, it skips any real processing and just returns what you told it to return */
if($this->debug == 0){
/* Make sure all arguments are passed to function */
if($cardcountry != "US")
$num_args = 15;
else
$num_args = 14;
if(func_num_args() == $num_args){
/*Auto-Correct Total Field*/
$total = str_replace("$","",str_replace(",","",$total));
/*Check argument data types*/
$cc_string = $cardnum1 . $cardnum2 . $cardnum3 . $cardnum4;
if(!is_numeric($cc_string)){
$this->error[] = "Invalid Credit Card Number Format";
}
unset($cc_string);
if(strlen($cardexpm) != 2){
$this->error[] = "Invalid Credit Card Expiry Month Format";
}
if(strlen($cardexpy) != 2){
$this->error[] = "Invalid Credit Card Expiry Year Format";
}
if(strlen($cardstate) != 2){
$this->error[] = "Invalid Credit Card State Format";
}
if(strlen($cardcountry) != 2){
$this->error[] = "Invalid Credit Card Country Format";
}
/* Determine Card Type*/
switch (substr($cardnum1,0,1)) {
default:
$this->error[] = "Invalid/Unknown Card Type";
break;
case 3 :
$card_type = "Amex";
break;
case 4 :
$card_type = "Visa";
break;
case 5 :
$card_type = "MasterCard";
break;
}
/* Run error checks based on card type */
switch($card_type){
case 'Amex':
if(strlen($cvv2) != 4)
$this->error[] = "American Express Requires Four-Digit CVV2 Code";
if(strlen($cardnum4) != 3)
$this->error[] = "Invalid American Express Card Number";
break;
case 'MasterCard':
if(strlen($cvv2) != 3)
$this->error[] = "MasterCard Requires Three-Digit CVV2 Code";
break;
case 'Visa':
if(strlen($cvv2) != 3)
$this->error[] = "Visa Requires Three-Digit CVV2 Code";
break;
}
if(count($this->error) == 0){
$this->recent_transaction["orderid_given"] = $orderid;
$this->recent_transaction["total"] = $total;
$this->recent_transaction["cardname"] = $card_type;
$this->recent_transaction["cardnum1"] = $cardnum1;
$this->recent_transaction["cardnum2"] = $cardnum2;
$this->recent_transaction["cardnum3"] = $cardnum3;
$this->recent_transaction["cardnum4"] = $cardnum4;
$this->recent_transaction["cardexpm"] = $cardexpm;
$this->recent_transaction["cardexpy"] = $cardexpy;
$this->recent_transaction["cvv2"] = $cvv2;
$this->recent_transaction["nameoncard"] = $nameoncard;
$this->recent_transaction["cardstreet"] = $cardstreet;
$this->recent_transaction["cardcity"] = $cardcity;
$this->recent_transaction["cardstate"] = $cardstate;
$this->recent_transaction["cardzip"] = $cardzip;
$this->recent_transaction["cardcountry"] = $cardcountry;
$csess = curl_init();
curl_setopt ($csess, CURLOPT_URL, "https://www.goemerchant7.com/cgi-bin/gateway/gateway.cgi");
curl_setopt ($csess, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($csess, CURLOPT_HEADER, 0);
curl_setopt ($csess, CURLOPT_POST, 1);
curl_setopt ($csess, CURLOPT_POSTFIELDS, "merchant=" . $this->merchant . "&password=" . $this->password . "&operation_type=auth&orderid=" . urlencode($orderid) . "&total=" . number_format($total,2,'.','') . "&cardname=" . $card_type . "&cardnum1=" . $cardnum1 ."&cardnum2=" . $cardnum2 ."&cardnum3=" . $cardnum3 ."&cardnum4=" . $cardnum4 ."&cardexpm=" . $cardexpm . "&cardexpy=" . $cardexpy . "&CVV2=" . $cvv2 . "&nameoncard=" . urlencode($nameoncard) . "&cardstreet=" . urlencode($cardstreet) . "&cardcity=" . urlencode($cardcity) . "&cardstate=" . urlencode($cardstate) . "&cardzip=" . $cardzip . "&cardcountry=" . urlencode($cardcountry) . "");
$returned = curl_exec ($csess);
if(curl_errno($csess) == 0){
$get_status = explode("|",$returned);
$this->recent_transaction["success"] = $get_status[0];
$this->recent_transaction["authcode"] = $get_status[1];
$this->recent_transaction["authresponse"] = trim(strip_tags($get_status[2]));
$this->recent_transaction["avs_code"] = $get_status[3];
$this->recent_transaction["orderid_returned"] = $get_status[4];
if($this->recent_transaction["success"] == 1)
return true;
else
return false;
}else{
$this->error[] = "Error Contacting CC Authorization Server";
return false;
}
curl_close ($csess);
}
}else{
$this->error[] = "Function process_credit_card Missing argument(s)";
return false;
}
}else{
/* If debugging was toggled, then do as it was instructed to do */
if($this->debug_return == 0){
$this->recent_transaction["success"] = 0;
$this->recent_transaction["authcode"] = "";
$this->recent_transaction["authresponse"] = "Developer Debugging Forces Decline";
$this->recent_transaction["avs_code"] = 0;
$this->recent_transaction["orderid_returned"] = $orderid;
return false;
}else{
$this->recent_transaction["success"] = 1;
$this->recent_transaction["authcode"] = "ASDFG12345";
$this->recent_transaction["authresponse"] = "Developer Debugging Forces Accept";
$this->recent_transaction["avs_code"] = 1;
$this->recent_transaction["orderid_returned"] = $orderid;
return true;
}
}
}
}
?>
|
|
|
Usage Example
|
|
|
Rate This Script
|
|
|
|