Jump to content

Payment Gateway using PHP


sithkong

Recommended Posts

Hello PHP developers,

I am a begining developer, and I am trying to setup the payment gateway from authorize.net using PHP.

 

I found a very good tutorial and read through the explanation also download the codes.

 

However, it does not seem to be working....I am wondering if I didn't do it correctly. I don't know if PHP talented developers out there could show me the correct ways, or tell what was wrong with the code I added....

 

<code>

class Authnet

{

private $login    = "7989SeRGCRr7";

private $transkey = "29ZvRs59mZh2C48A";

private $params  = array();

private $results  = array();

 

private $approved = false;

private $declined = false;

private $error    = true;

 

private $test;

private $fields;

private $response;

 

static $instances = 0;

 

public function __construct($test = false)

{

if (self::$instances == 0)

{

$this->test    = trim($test);

if ($this->test)

{

$this->url = "https://test.authorize.net/gateway/transact.dll";

}

else

{

$this->url = "https://secure.authorize.net/gateway/transact.dll";

}

$this->params['x_delim_data']    = "TRUE";

$this->params['x_delim_char']    = "|";

$this->params['x_relay_response'] = "FALSE";

$this->params['x_url']            = "FALSE";

$this->params['x_version']        = "3.1";

$this->params['x_method']        = "CC";

$this->params['x_type']          = "AUTH_CAPTURE";

$this->params['x_login']          = $this->login;

$this->params['x_tran_key']      = $this->transkey;

 

self::$instances++;

}

else

{

return false;

}

}

 

public function transaction($cardnum, $expiration, $amount, $cvv = "", $invoice = "", $tax = "")

{

$this->params['x_card_num']  = trim($cardnum);

$this->params['x_exp_date']  = trim($expiration);

$this->params['x_amount']    = trim($amount);

$this->params['x_po_num']    = trim($invoice);

$this->params['x_tax']      = trim($tax);

$this->params['x_card_code'] = trim($cvv);

}

 

public function process($retries = 3)

{

$this->_prepareParameters();

$ch = curl_init($this->url);

 

$count = 0;

while ($count < $retries)

{

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, rtrim($this->fields, "& "));

$this->response = curl_exec($ch);

$this->_parseResults();

if ($this->getResultResponseFull() == "Approved")

{

$this->approved = true;

$this->declined = false;

$this->error    = false;

break;

}

else if ($this->getResultResponseFull() == "Declined")

{

$this->approved = false;

$this->declined = true;

$this->error    = false;

break;

}

$count++;

}

curl_close($ch);

}

 

private function _parseResults()

{

$this->results = explode("|", $this->response);

}

 

public function setParameter($param, $value)

{

$param                = trim($param);

$value                = trim($value);

$this->params[$param] = $value;

}

 

public function setTransactionType($type)

{

$this->params['x_type'] = strtoupper(trim($type));

}

 

private function _prepareParameters()

{

foreach($this->params as $key => $value)

{

$this->fields .= "$key=" . urlencode($value) . "&";

}

}

 

public function getResultResponse()

{

return $this->results[0];

}

 

public function getResultResponseFull()

{

$response = array("", "Approved", "Declined", "Error");

return $response[$this->results[0]];

}

 

public function isApproved()

{

return $this->approved;

}

 

public function isDeclined()

{

return $this->declined;

}

 

public function isError()

{

return $this->error;

}

 

public function getResponseText()

{

return $this->results[3];

}

 

public function getAuthCode()

{

return $this->results[4];

}

 

public function getAVSResponse()

{

return $this->results[5];

}

 

public function getTransactionID()

{

return $this->results[6];

}

}

 

//process the payment

//user information variables

//$price = $_POST['_CC_PRICE'];

//$card_number = $_POST['_CC_Number'];

//$card_type = $_POST['_CC_Type'];

//$ex_month = $_POST['_CC_ExpDateMonth'];

//$ex_year = $_POST['_CC_ExpDateYear'];

//$fname_oncard = $_POST['_CC_FirstName'];

//$lname_oncard = $_POST['_CC_LastName'];

//$security_code = $_POST['_CC_Code'];

 

$tax = 0;

$cardnum = $_POST['_CC_Number'];

$expiration = $_POST['_CC_ExpDateMonth'] . $_POST['_CC_ExpDateYear'];

$amount = 49.95;

$invoice = rand(0000,10000);

$cvv = $_POST['_CC_Code'];

//$total = 49.95;

$fname_oncard = $_POST['_CC_FirstName'];

$lname_oncard = $_POST['_CC_LastName'];

 

$payment = new Authnet(false);

$payment->transaction($cardnum, $expiration,, $amount, $cvv, $invoice, $tax);

//$payment->setParameter("x_address", $business_address);

//$payment->setParameter("x_zip", $business_zipcode);

$payment->setParameter("x_first_name", $fname_oncard);

$payment->setParameter("x_last_name", $lname_oncard);

$payment->process();

 

if ($payment -> isApproved()) {

  // Display a printable receipt

  echo "<table widh=500 align=center border=1>";

  echo "<tr><td>";

  echo "<p>Your payment has been processed successfully!</p>";

  echo "<p>Below your receipt</p>";

  echo "<p>Price: $49.95 <br /></p>";

  echo "</td></tr>";

  echo "</table>";

 

} else if ($payment -> isDeclined()) {

  $reason = $payment -> getResponseText();

  // As for another form of payment

  echo "<p>Your payment is not approved. Please contact sithkong@gmail.com</p>";

} else {

  // Ask the merchant to call us

 

}

</code>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.