Miscellaneous Code

  Home arrow Miscellaneous Code arrow GoEMerchant.com Gateway Class
MISCELLANEOUS CODE

GoEMerchant.com Gateway Class
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2004-01-12

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    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

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap