Skip to content

Instantly share code, notes, and snippets.

@kwylez
Created September 24, 2010 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwylez/595412 to your computer and use it in GitHub Desktop.
Save kwylez/595412 to your computer and use it in GitHub Desktop.
<?php
/**
* @see Zend_Validate_Abstract
*/
require_once 'Zend/Validate/Abstract.php';
/**
* @see Zend_Http_Client
*/
require_once 'Zend/Http/Client.php';
class CW_Validate_PayFlow extends Zend_Validate_Regex {
const CHARGE_FAILED = "ppChargeFailed";
const CHARGE_EXCEPTED_CODE = "0";
/**
* Error message to display to the user if the credit card transaction fails
*
* @access protected
* @var array
*/
protected $_messageTemplates = array(
self::CHARGE_FAILED => "There was an error processing your card. Error code: '%code%' Error message: '%msg%'"
);
/**
* @var array
*/
protected $_messageVariables = array(
'code' => '_code',
'msg' => '_msg'
);
/**
* Error Code value
*
* @var mixed
*/
protected $_code;
/**
* Error Message
*
* @param Zend_Config $options
*/
protected $_msg;
/**
* CC validation using PayFlow
*
* @param Zend_Config $options
* @param string $ccnum
* @param string $exp
* @param array $billing
* @param string $amount
* @param string $country
*/
public function __construct(Zend_Config $paypaloptions, $ccnum, $amount, $exp, $billing = array(), $country = 'US') {
if (!$paypaloptions instanceof Zend_Config) {
throw new Exception("Options must be an instance of Zend Config");
} else {
$options = $paypaloptions->toArray();
}
/**
* Random string used to error checking with payflow - DUPLICATION TRANSACTIONS
*/
$requestId = md5(date('YmdGis'));
$zfClient = new Zend_Http_Client($options['url']);
$zfClient->setHeaders(array('X-VPS-Request-ID' => $requestId));
/**
* Recommended from their documentation to change the timeout from 30 seconds (default)
* to 45
*/
$zfClient->setConfig(array('timeout' => 45));
$zfClient->setMethod(Zend_Http_Client::POST);
$zfClient->setParameterPost(array(
'USER' => $options['username'],
'VENDOR' => $options['vendor'],
'PARTNER' => $options['partner'],
'PWD' => $options['password'],
'FIRSTNAME' => $billing['firstname'],
'LASTNAME' => $billing['lastname'],
'STREET' => $billing['street'],
'ZIP' => $billing['zip'],
'TENDER' => 'C',
'TRXTYPE' => 'S',
'ACCT' => $ccnum,
'EXPDATE' => $exp,
'AMT' => $amount,
'CURRENCY' => 'USD',
'COUNTRY' => $country,
'CLIENTIP' => $_SERVER['REMOTE_ADDR'],
'VERBOSITY' => 'MEDIUM'
));
/**
* The response key/value pairs are seperated by ampersands.
*
* I extract the RESULT and RESPMSG
*
* If anything but a RESULT=0 is found then the code and error message are
* set to the validator's variables to display to the form user.
*/
$payFlowResponse = explode("&", $zfClient->request()->getBody());
$regex = "/(\w*)=(.*)/i";
@preg_match($regex, $payFlowResponse[0], $matches);
$this->setCode($matches[2]);
if ($this->getCode() !== self::CHARGE_EXCEPTED_CODE) {
@preg_match($regex, $payFlowResponse[2], $matches);
$this->setMsg($matches[2]);
}
}
public function isValid($value) {
$this->_setValue($value);
if ($this->getCode() !== self::CHARGE_EXCEPTED_CODE) {
$this->_error();
return false;
}
return true;
}
public function setCode($code) {
$this->_code = $code;
}
public function setMsg($msg) {
$this->_msg = $msg;
}
public function getCode() {
return $this->_code;
}
public function getMsg() {
return $this->_msg;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment