Skip to content

Instantly share code, notes, and snippets.

@aliharis
Last active April 2, 2020 09:59
Show Gist options
  • Save aliharis/658e2bbec178977e9ea2d593eadc6917 to your computer and use it in GitHub Desktop.
Save aliharis/658e2bbec178977e9ea2d593eadc6917 to your computer and use it in GitHub Desktop.
MPG.php
<?php
/**
* Maldives Payment Gateway Integration
* @author haris
*/
class MPG
{
private $version;
private $merchantId;
private $acquirerId;
private $signatureMethod;
private $transactionPassword;
private $purchaseCurrency;
private $merchantResponseUrl = 'https://notrequiredanymore.com';
public $orderId;
public $purchaseAmount;
// Example
// $mpg = new MPG("ORDER-001", 9.99);
// $mpg->getFormFields();
public function __construct($orderId, $purchaseAmount)
{
// Load Defaults from Configuration File
$this->version = '1.1';
$this->merchantId = '9000000000';
$this->acquirerId = '407387';
$this->signatureMethod = 'SHA1';
$this->transactionPassword = 'secret';
$this->purchaseCurrency = '462';
$this->orderId = $orderId;
$this->purchaseAmount = $this->formatAmount($purchaseAmount);
}
/**
* Format the purchase amount to 12 characters and left pad with 0
*
* @return string
*/
private function formatAmount($purchaseAmount)
{
$formattedAmount = number_format($purchaseAmount, 2, '.', '');
$formattedAmount = str_replace('.', '', $formattedAmount);
return str_pad($formattedAmount, 12, '0', STR_PAD_LEFT);
}
/**
* Return the data for MPG Order Form
*
* @return array
*/
public function getFormFields()
{
return [
'Version' => $this->version,
'MerID' => $this->merchantId,
'AcqID' => $this->acquirerId,
'MerRespURL' => $this->merchantResponseUrl,
'PurchaseCurrency' => $this->purchaseCurrency,
'OrderID' => $this->orderId,
'SignatureMethod' => $this->signatureMethod,
'PurchaseAmt' => $this->purchaseAmount,
'Signature' => $this->signature()
];
}
/**
* Generate the signature for MPG transaction
*
* @retun string
*/
public function signature()
{
// Concatenate the properties required for signature
$plain_signature = $this->transactionPassword . $this->merchantId . $this->acquirerId .
$this->orderId . $this->purchaseAmount . $this->purchaseCurrency;
$hash = base64_encode(sha1($plain_signature, true));
// Hash, base64 encode and return the signature
return $hash;
}
/**
* Verify generated signature
*
* @return Boolean
*/
public function verifySignature($orderId, $purchaseAmount, $signature) {
$purchaseAmount = $this->formatAmount($purchaseAmount);
$new_sig = $this->transactionPassword. $this->merchantId . $this->acquirerId .
$orderId . $purchaseAmount . $this->purchaseCurrency;
if ($signature == base64_encode(sha1($new_sig, TRUE)))
return true;
return false;
}
/**
* Generate the Signature after successful transaction
* To compare with the Signature returned by Bank
*
* @return String
*/
public function generateSignatureTwo($responseCode, $reasonCode)
{
$signature = $this->transactionPassword . $this->merchantId . $this->acquirerId . $this->orderId .
$responseCode . $reasonCode;
return base64_encode(sha1($signature, true));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment