This class allows you to submit Credit Card transactions to the merchant provider GoEmerchant.com. It requires Curl. There is a debug feature you can turn on that will allow you to test it as you code without needing to send a request to their server.
By : sleighboy
<?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;
case 6 :
$card_type = "Discover";
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;
case 'Discover':
if(strlen($cvv2) != 3)
$this->error[] = "Discover 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;
}
}
}
}
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Miscellaneous Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
Join this webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download the latest trial version of IBM Data Studio V1.1 at no cost. IBM Data Studio is a comprehensive data management solution that helps you effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life cycle utilizing a consistent and integrated user interface. Unlike other client-side data management solutions that focus on only one aspect of the application lifecycle or database administration, Data Studio complements the Rational Software Delivery platform, providing unparalleled flexibility for a heterogeneous data server environment across platforms. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points. FREE! Go There Now!
|
|
|
|
Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. FREE! Go There Now!
|
|
|
|
This Fall, IBM Rational talks to you directly through a special teleconference series giving you access to the best minds in IBM Rational - product experts and market thought leaders who will answer your questions during these pre-scheduled telephone conference calls. Register today! FREE! Go There Now!
|
|
|
|
Rational Modeling Extension for Microsoft .NET enhances usability for code generation supporting a more intelligent refactoring. The latest enhancements enable organizations with Java and .NET systems and software development maintain architectural integrity across heterogeneous platforms. FREE! Go There Now!
|
|
|
|
As organizations have grown increasingly dependent on online software, the risk of malicious attacks has also become far more serious. Fortunately, well-governed organizations can protect their Web applications by injecting vulnerability assessments and ethical hacks into their software development and delivery processes. This paper describes 12 of the most common hacker attacks and provides basic rules that you can follow to help create more hack-resistant Web applications. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for process. The SOA Sandbox for process focuses on providing a trial environment with the necessary tooling and components required to gain a better understanding of business processes and how to best improve existing business processes to derive value quickly. FREE! Go There Now!
|
|
|
|
In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information. FREE! Go There Now!
|
|
|
|
IBM Lotus Notes 8 provides a wide range of developers the ability to provide customized, integrated user interfaces via composite applications and via custom sidebar and toolbar plug-ins. This webcast provides you with tips and techniques to use with out-of-the-box capabilities of Lotus Notes 8, and survey how you can share useful components within your own company and within a larger community. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |