Skip to content

Instantly share code, notes, and snippets.

@inash
Created June 30, 2014 09:04
Show Gist options
  • Save inash/67dceb2b1ddc3f2794b3 to your computer and use it in GitHub Desktop.
Save inash/67dceb2b1ddc3f2794b3 to your computer and use it in GitHub Desktop.
MPG class for signature generation
<?php
require_once 'mpg.php';
$mpg = new Mpg(array(
'merchantId' => '980xxxxxxx',
'merchantResponseUrl' => 'https://example.com/response',
'transactionPassword' => 'xxxxxxxx',
'orderId' => 'XXX12341234',
'amount' => '149.99'));
$mpg->generateSignature();
/**
* In the HTML template use $mpg values as:
*
* <input type="hidden" name="Version" value="<?php echo $mpg->version ?>" />
* <input type="hidden" name="MerID" value="<?php echo $mpg->merchantId ?>" />
* <input type="hidden" name="AcqID" value="<?php echo $mpg->acquirerId ?>" />
* <input type="hidden" name="MerRespURL" value="<?php echo $mpg->merchantResponseUrl ?>" />
* <input type="hidden" name="PurchaseCurrency" value="<?php echo $mpg->purchaseCurrency ?>" />
* <input type="hidden" name="PurchaseCurrencyExponent" value="<?php echo $mpg->purchaseCurrencyExponent ?>" />
* <input type="hidden" name="OrderID" value="<?php echo $mpg->orderId ?>" />
* <input type="hidden" name="SignatureMethod" value="<?php echo $mpg->signatureMethod ?>" />
* <input type="hidden" name="PurchaseAmt" value="<?php echo $mpg->amount ?>" />
* <input type="hidden" name="Signature" value="<?php echo $mpg->requestSignature ?>" />
*
*/
<?php
/**
* @author Inash Zubair <inash@leptone.com>
* @created Monday, July 05, 2010
*/
class Mpg
{
private $_values = array(
'version' => '1.0.0',
'merchantId' => null,
'acquirerId' => '407387',
'merchantResponseUrl' => null,
'purchaseCurrency' => '462',
'purchaseCurrencyExponent' => '2',
'orderId' => null,
'signatureMethod' => 'SHA1',
'transactionPassword' => null,
'amount' => null,
'requestSignature' => null);
private $_gatewayMethod = 'direct';
public function __construct($config = array())
{
if (!is_array($config)) {
throw new Exception('$config needs to be an array.');
}
$validParams = array(
'version',
'merchantId',
'acquirerId',
'merchantResponseUrl',
'purchaseCurrency',
'purchaseCurrencyExponent',
'orderId',
'signatureMethod',
'transactionPassword',
'amount',
'requestSignature');
foreach ($config as $key => $value) {
if (!in_array($key, $validParams)) {
throw new Exception("invalid config param '{$key}'.");
} else {
if ($key == 'amount') {
$this->setAmount($value);
continue;
}
$this->_values[$key] = $value;
}
}
}
public function setGatewayMethod($method = 'direct')
{
$validParams = array('direct', 'redirect');
if (!in_array($method, $validParams)) {
throw new Exception('invalid gateway method.');
} else {
$this->_gatewayMethod = $method;
}
}
public function setTransactionPassword($password)
{
$this->_transactionPassword = $password;
}
public function setOrderId($orderId)
{
$this->_orderId = $orderId;
}
public function setAmount($amount)
{
$amount = number_format($amount, 2, '.', '');
$amount = str_replace('.', '', $amount);
$amount = str_pad($amount, 12, '0', STR_PAD_LEFT);
$this->_values['amount'] = $amount;
}
public function generateSignature($regenerate = false)
{
if ($this->_values['requestSignature'] != null &&
$regenerate == false) {
return $this->_values['requestSignature'];
}
if ($this->_values['transactionPassword'] == null ||
$this->_values['merchantId'] == null ||
$this->_values['orderId'] == null ||
$this->_values['amount'] == null) {
throw new Exception('required params missing.');
}
$signature = $this->_values['transactionPassword']
. $this->_values['merchantId']
. $this->_values['acquirerId']
. $this->_values['orderId']
. $this->_values['amount']
. $this->_values['purchaseCurrency'];
$this->_values['requestSignature'] = base64_encode(sha1($signature, true));
return $this->_values['requestSignature'];
}
public function getRedirectFormData()
{
$formData = array(
'Version' => $this->_version,
'MerID' => $this->_merchantId,
'AcqID' => $this->_acquirerId,
'MerRespURL' => $this->_merchantResponseUrl,
'PurchaseCurrency' => $this->_purchaseCurrency,
'PurchaseCurrencyExponent' => $this->_purchaseCurrencyExponent,
'OrderID' => $this->_orderId,
'SignatureMethod' => $this->_signatureMethod,
'PurchaseAmt' => $this->_purchaseAmt,
'Signature' => $this->generateSignature());
return $formData;
}
public function __get($name)
{
return $this->_values[$name];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment