Last active
November 7, 2019 03:16
-
-
Save khanhicetea/599839ec906be3ee9f9b8aa7c372b4e4 to your computer and use it in GitHub Desktop.
OnePay Vietnam PHP SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class OnePayGateway | |
{ | |
private $options = [ | |
'version' => '2', | |
'currency' => 'VND', | |
'merchant' => 'ONEPAY', | |
'access_code' => 'D67342C2', | |
'secure_hash' => 'A3EFDFABA8653DF2342E8DAC29B51AF0', | |
'locale' => 'vn', | |
'title' => 'Tua De WEB', | |
]; | |
/** | |
* @param array $options | |
*/ | |
public function __construct(array $options = []) | |
{ | |
$this->options = array_replace_recursive($this->options, $options); | |
} | |
private function getClientIp() { | |
$ip = '127.0.0.1'; | |
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { | |
$ip = $_SERVER['HTTP_CLIENT_IP']; | |
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { | |
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
} elseif (!empty($_SERVER['REMOTE_ADDR'])) { | |
$ip = $_SERVER['REMOTE_ADDR']; | |
} | |
return $ip; | |
} | |
// Lay link thanh toan | |
public function getPaymentUrl($return_url, $back_url, $domestic, $order_id, $amount) | |
{ | |
$endpoint = $domestic ? 'https://mtf.onepay.vn/onecomm-pay/vpc.op?' : 'https://mtf.onepay.vn/vpcpay/vpcpay.op?'; | |
$options = $this->options; | |
$payment_data = [ | |
'vpc_Version' => $options['version'], | |
'vpc_Currency' => $options['currency'], | |
'vpc_Command' => 'pay', | |
'vpc_AccessCode' => $options['access_code'], | |
'vpc_Merchant' => $options['merchant'], | |
'vpc_Locale' => $options['locale'], | |
'vpc_ReturnURL' => $return_url, | |
'vpc_MerchTxnRef' => floor(microtime(true)) . rand(10000, 99999), | |
'vpc_OrderInfo' => $order_id, | |
'vpc_Amount' => round($amount * 100, 0), | |
'vpc_TicketNo' => $this->getClientIp(), | |
]; | |
ksort($payment_data); | |
$secure_hash = $options['secure_hash']; | |
$hash_values = []; | |
foreach ($payment_data as $key => $value) { | |
$hash_values[] = $key . '=' . $value; | |
} | |
$hmac = strtoupper(hash_hmac('SHA256', implode('&', $hash_values), pack('H*',$secure_hash))); | |
$payment_data['vpc_SecureHash'] = $hmac; | |
$payment_data['AgainLink'] = $back_url; | |
$payment_data['Title'] = $options['title']; | |
return $endpoint . http_build_query($payment_data, '&'); | |
} | |
// Verify request thanh toan OnePay redirect ve | |
public function isValidResponse(array $query) | |
{ | |
$hash = $query['vpc_SecureHash']; | |
unset($query['vpc_SecureHash']); | |
$secure_hash = $this->options['secure_hash']; | |
ksort($query); | |
$hash_values = []; | |
foreach ($query as $key => $value) { | |
if (substr($key, 0, 4) == 'vpc_') { | |
$hash_values[] = $key . '=' . $value; | |
} | |
} | |
$hmac = strtoupper(hash_hmac('SHA256', implode('&', $hash_values), pack('H*',$secure_hash))); | |
return $hmac === $hash; | |
} | |
} | |
// $test_quocnoi = new OnePayGateway(); | |
// $url_qn = $test_quocnoi->getPaymentUrl('http://website.test/onepay/success', 'http://website.test/onepay/pay', true, 123, 100000); | |
// $test_quocte = new OnePayGateway([ | |
// 'merchant' => 'TESTONEPAY', | |
// 'access_code' => '6BEB2546', | |
// 'secure_hash' => '6D0870CDE5F24F34F3915FB0045120DB', | |
// ]); | |
// $url_qt = $test_quocte->getPaymentUrl('http://website.test/onepay/success', 'http://website.test/onepay/pay', false, 456, 100000); | |
// var_dump($url_qn); | |
// var_dump($url_qt); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment