Finish: Build the payment URL

This commit is contained in:
htdat
2017-04-18 10:06:25 +07:00
parent 3eeb71bf69
commit 5f43aeb47d
2 changed files with 76 additions and 16 deletions
+65 -12
View File
@@ -1,5 +1,4 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
@@ -35,6 +34,7 @@ class WooViet_OnePay_Domestic extends WC_Payment_Gateway {
// Define user set variables.
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->testmode = 'yes' === $this->get_option( 'testmode', 'no' );
$this->merchant_id = $this->get_option( 'merchant_id' );
$this->access_code = $this->get_option( 'access_code' );
$this->secure_secret = $this->get_option( 'secure_secret' );
@@ -65,22 +65,75 @@ class WooViet_OnePay_Domestic extends WC_Payment_Gateway {
* @return string
*/
public function get_pay_url ( $order ) {
$access_key = $this->access_key;
$secret = $this->secret; // require your secret key from 1pay
$return_url = $this->get_1pay_return_url();
$command = 'request_transaction';
$amount = $order->get_total(); // >10000
$order_id = $order->id;
$order_info = sprintf( 'The payment for the order number %1$s on the site: %2$s', $order->id, get_home_url());
$args = array(
'Title' => __('OnePay Payment Title', 'woo-viet'),
'vpc_Merchant' => $this->merchant_id,
'vpc_AccessCode' => $this->access_code,
'vpc_MerchTxnRef' => sprintf('%1$s_%2$s', $order->id, date ( 'YmdHis' )),
'vpc_OrderInfo' => substr(
sprintf('Order #%1$s - %2$s', $order->id, get_home_url()),
0,
32 ), // Limit 32 characters
'vpc_Amount' => $order->get_total() * 100, // Multiplying 100 is a requirement from OnePay
'vpc_ReturnURL' => $this->get_onepay_return_url(),
'vpc_Version' => '2',
'vpc_Command' => 'pay',
'vpc_Locale' => ( 'vi' == get_locale() ) ? 'vn' : 'en',
'vpc_Currency' => 'VND',
'vpc_TicketNo' => $_SERVER['REMOTE_ADDR'],
);
// @todo: review and see the file do.php in the demo files
// Get the secure hash
$vpc_SecureHash = $this->create_vpc_SecureHash( $args );
// Add the secure hash to the args
$args['vpc_SecureHash'] = $vpc_SecureHash;
$http_args = http_build_query( $args, '', '&' );
if ( $this->testmode ) {
return 'https://mtf.onepay.vn/onecomm-pay/vpc.op?' . $http_args;
} else {
return 'https://onepay.vn/onecomm-pay/vpc.op?' . $http_args;
}
// @todo review this function
/*
public function static get_return_url(){
}
// @todo: need to check this and WC_Payment_Gateway::get_return_url($order = NULL)
public function get_onepay_return_url(){
return WC()->api_request_url( __CLASS__ );
}
/**
* Get the PayPal request URL for an order.
* @param array $args
* @return string
*/
public function create_vpc_SecureHash ( $args ){
$stringHashData = "";
// arrange array data a-z before make a hash
ksort ($args );
foreach($args as $key => $value) {
// tạo chuỗi đầu dữ liệu những tham số có dữ liệu
if (strlen($value) > 0) {
//sử dụng cả tên và giá trị tham số để mã hóa
if ((strlen($value) > 0) && ((substr($key, 0,4)=="vpc_") || (substr($key,0,5) =="user_"))) {
$stringHashData .= $key . "=" . $value . "&";
}
}
}
//xóa ký tự & ở thừa ở cuối chuỗi dữ liệu mã hóa*****************************
$stringHashData = rtrim($stringHashData, "&");
return strtoupper(hash_hmac('SHA256', $stringHashData, pack('H*', $this->secure_secret )));
}
public function process_return_url (){
// http://localhost/woo-viet/wc-api/WooViet_OnePay_Domestic/?vpc_AdditionData=686868&vpc_Amount=40000000&vpc_Command=pay&vpc_CurrencyCode=VND&vpc_Locale=en&vpc_MerchTxnRef=%23133_20170418024557&vpc_Merchant=ONEPAY&vpc_OrderInfo=%23133+-+http%3A%2F%2Flocalhost%2Fwoo-viet&vpc_TransactionNo=1576998&vpc_TxnResponseCode=0&vpc_Version=2&vpc_SecureHash=25C4443CFF95F6D5BCE4AD86DAA6756960688BD8868CBE898989125050504361
}
}
+9 -2
View File
@@ -15,6 +15,13 @@ return array(
'label' => __( 'OnePay Domestic Gateway (by Woo Viet)', 'woo-viet' ),
'default' => 'yes'
),
'testmode' => array(
'title' => __( 'OnePay Sandbox', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable OnePay sandbox (testing)', 'woocommerce' ),
'default' => 'no',
'description' => sprintf( __( 'OnePay sandbox can be used to test payments. See <a href="%s">the testing info</a>.', 'woocommerce' ), 'https://mtf.onepay.vn/developer/?page=modul_noidia' ),
),
'title' => array(
'title' => __( 'Title', 'woo-viet' ),
'type' => 'text',
@@ -27,7 +34,7 @@ return array(
'type' => 'text',
'desc_tip' => true,
'description' => __( 'This controls the description which the user sees during checkout.', 'woo-viet' ),
'default' => __( 'With OnePay Bank, you can make payment by using any local Vietnam ATM card.', 'woo-viet' )
'default' => __( 'With OnePay, you can make payment by using any local Vietnam ATM card.', 'woo-viet' )
),
'api_details' => array(
'title' => __( 'API Credentials', 'woo-viet' ),
@@ -42,7 +49,7 @@ return array(
'desc_tip' => true,
'placeholder' => __( 'Required. Provided by OnePay.', 'woo-viet' )
),
'access code' => array(
'access_code' => array(
'title' => __( 'Access Code', 'woocommerce' ),
'type' => 'text',
'description' => __( 'Get your Access Code from from OnePay.', 'woo-viet' ),