Skip to content

Instantly share code, notes, and snippets.

@tkjaergaard
Created May 10, 2012 11:14
Show Gist options
  • Select an option

  • Save tkjaergaard/2652485 to your computer and use it in GitHub Desktop.

Select an option

Save tkjaergaard/2652485 to your computer and use it in GitHub Desktop.
QuickPay
<?php
$params = array(
'msgtype' => 'recurring',
'merchant' => 89898978,
'ordernumber' => 00002376630418111216,
'amount' => 400,
'currency' => 'DKK',
'autocapture' => 1,
'transaction' => 42309147,
'testmode' => 1,
'apikey' => 'MIl16LPa152HZB6wA4dNXv31UF8n1f7e8W327mrz684cg399C2j49t7Yb8Qxy79i',
'secret' => '29p61DveBZ79c3144LW61lVz1qrwk2gfAFCxPyi5sn49m3Y3IRK5M6SN5d8a68u7',
);
/**
* QuickPay XML-API library
*
* This class serves as an working example on how to communicate
* with the QuickPay payment gateway
*
*/
class QuickPay {
const QUICKPAY_VERSION = 4;
const QUICKPAY_SECRET = '29p61DveBZ79c3144LW61lVz1qrwk2gfAFCxPyi5sn49m3Y3IRK5M6SN5d8a68u7';
/**
* Constructor
*
* @param array $message
*/
public function __construct(Array $message = array()) {
foreach ($message as $k => $v) {
$this->$k = $v;
}
}
/**
* Commits the message to QuickPay
*
* @return string
*/
public function commit() {
$msg = $this->buildMessage();
$response = $this->transmit($msg);
// reset variables
foreach ($this as $k => $v) {
unset($this->$k);
}
return $this->responseToArray($response);
}
/**
* Builds a QuickPay message based on class variables set
*
* @return array
*/
private function buildMessage() {
//
$message = array();
$md5fields = array(
'msgtype' => null,
'merchant' => null,
'ordernumber' => null,
'amount' => null,
'currency' => null,
'autocapture' => null,
'cardnumber' => null,
'expirationdate' => null,
'cvd' => null,
'cardtypelock' => null,
'transaction' => null,
'description' => null,
'testmode' => null,
);
foreach ($this as $k => $v) {
$message[$k] = $v;
if (array_key_exists($k, $md5fields)) {
$md5fields[$k] = $v;
}
}
$md5str = self::QUICKPAY_VERSION . implode('', $md5fields) . self::QUICKPAY_SECRET;
$message['protocol'] = self::QUICKPAY_VERSION;
$message['md5check'] = md5($md5str);
return $message;
}
/**
* Transmits the message to QuickPay
*
* @param array $message
* @return unknown
*/
private function transmit(Array $message) {
// Create a HTTP POST request with QuickPay message as data
$context = stream_context_create(
array(
'http' => array(
'method' => 'POST',
'content' => http_build_query($message, false, '&'),
),
)
);
if (!$fp = @fopen('https://secure.quickpay.dk/api', 'r', false, $context)) {
throw new Exception('Could not connect to gateway');
}
if (($response = @stream_get_contents($fp)) === false) {
throw new Exception('Could not read data from gateway');
}
return $response;
}
/**
* Converts QuickPay XML response to an array
*
* @param string $response
* @return array
*/
private function responseToArray($response) {
// Load XML in response into DOM
$result = array();
$dom = new DOMDocument;
$dom->loadXML($response);
// Find elements en response and put them in an associative array
$xpath = new DOMXPath($dom);
$elements = $xpath->query('/response/*');
foreach ($elements as $cn) {
// If the element has (real) children - this is the case for status->history and chstatus->entry
if ($cn->childNodes->length > 1) {
foreach ($cn->childNodes as $hn) {
$result[$cn->nodeName][intval($i)][$hn->nodeName] = $hn->nodeValue;
}
$i++;
} else {
$result[$cn->nodeName] = $cn->nodeValue;
}
}
return $result;
}
}
$qp = new QuickPay( $params );
print_r( $qp->commit() );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment