/modules/shops/payment/nganluong.class.php
http://nukeviet-shop.googlecode.com/ · PHP · 198 lines · 135 code · 30 blank · 33 comment · 19 complexity · 85751c4c0c8046c4f609a331beea4c53 MD5 · raw file
- <?php
- /**
- *
- * Phięn b?n: 0.1
- * Tęn l?p: NL_CheckOut
- * Ch?c n?ng: Tích h?p thanh toán qua nganluong.vn cho các merchant site có ??ng ký API
- * - Xây d?ng URL chuy?n thông tin t?i Nganluong.vn ?? x? lý vi?c thanh toán cho merchant site.
- * - Xác th?c tính chính xác c?a thông tin ??n hŕng ???c g?i v? t? nganluong.vn.
- *
- **/
- require_once ( NV_ROOTDIR . "/includes/class/nusoap.php" );
- class NL_Checkout
- {
- // URL chheckout c?a nganluong.vn
- private $nganluong_url = '';
- private $merchant_site_code;
- private $secure_pass;
- function __construct ( $nganluong_url, $merchant_site_code, $secure_pass )
- {
- $this->nganluong_url = trim( $nganluong_url );
- $this->merchant_site_code = trim( $merchant_site_code );
- $this->secure_pass = trim( $secure_pass );
- }
- //Hŕm xây d?ng url, trong ?ó có tham s? mă hóa (cňn g?i lŕ public key)
- public function buildCheckoutUrl ( $return_url, $receiver, $transaction_info, $order_code, $price )
- {
- // M?ng các tham s? chuy?n t?i nganluong.vn
- $arr_param = array(
- 'merchant_site_code' => strval( $this->merchant_site_code ), 'return_url' => strtolower( urlencode( $return_url ) ), 'receiver' => strval( $receiver ), 'transaction_info' => strval( $transaction_info ), 'order_code' => strval( $order_code ), 'price' => strval( $price )
- );
-
- $secure_code = implode( ' ', $arr_param ) . ' ' . $this->secure_pass;
- $arr_param['secure_code'] = md5( $secure_code );
- /* B??c 2. Ki?m tra bi?n $redirect_url xem có '?' không, n?u không có thě b? sung vŕo*/
- $redirect_url = $this->nganluong_url;
- if ( strpos( $redirect_url, '?' ) === false )
- {
- $redirect_url .= '?';
- }
- else if ( substr( $redirect_url, strlen( $redirect_url ) - 1, 1 ) != '?' && strpos( $redirect_url, '&' ) === false )
- {
- // N?u bi?n $redirect_url có '?' nh?ng không k?t thúc b?ng '?' vŕ có ch?a d?u '&' thě b? sung vŕo cu?i
- $redirect_url .= '&';
- }
-
- /* B??c 3. t?o url*/
- $url = '';
- foreach ( $arr_param as $key => $value )
- {
- if ( $url == '' ) $url .= $key . '=' . $value;
- else $url .= '&' . $key . '=' . $value;
- }
-
- return $redirect_url . $url;
- }
- /*Hŕm th?c hi?n xác minh tính ?úng ??n c?a các tham s? tr? v? t? nganluong.vn*/
-
- public function verifyPaymentUrl ( $transaction_info, $order_code, $price, $payment_id, $payment_type, $error_text, $secure_code )
- {
- // T?o mă xác th?c t? ch? web
- $str = '';
- $str .= ' ' . strval( $transaction_info );
- $str .= ' ' . strval( $order_code );
- $str .= ' ' . strval( $price );
- $str .= ' ' . strval( $payment_id );
- $str .= ' ' . strval( $payment_type );
- $str .= ' ' . strval( $error_text );
- $str .= ' ' . strval( $this->merchant_site_code );
- $str .= ' ' . strval( $this->secure_pass );
-
- // Mă hóa các tham s?
- $verify_secure_code = md5( $str );
-
- // Xác th?c mă c?a ch? web v?i mă tr? v? t? nganluong.vn
- if ( $verify_secure_code === $secure_code ) return true;
-
- return false;
- }
- public function checkOrder ( $public_api_url, $order_code, $payment_id )
- {
- $payment_id = ( empty( $payment_id ) ) ? "" : $payment_id;
- $param = "<ORDERS>
- <TOTAL>1</TOTAL>
- <ORDER>
- <ORDER_CODE>" . $order_code . "</ORDER_CODE>
- <PAYMENT_ID>" . $payment_id . "</PAYMENT_ID>
- </ORDER>
- </ORDERS>";
-
- $client = new nusoap_client( $public_api_url, 'wsdl' );
- $result = $client->call( 'checkOrder', array(
- 'merchant_id' => $this->merchant_site_code, 'param' => $param, 'checksum' => md5( $this->merchant_site_code . $param . $this->secure_pass )
- ) );
- if ( $xml = simplexml_load_string( $result ) )
- {
- $error_code = ( string )$xml->ERROR_CODE;
- if ( $error_code == "00" )
- {
- $transaction = $xml->xpath( 'TRANSACTION' );
- $data = array_map( "trim", ( array )$transaction[0] );
- if ( $data['TRANSACTION_ERROR_CODE'] == '00' )
- {
- $preg_match_time = '/(\d{2}):(\d{2}):(\d{2}) - (\d{2})\/(\d{2})\/(\d{4})/';
-
- unset( $matches );
- preg_match( $preg_match_time, $data['CREATED_TIME'], $matches );
- $data['CREATED_TIME'] = mktime( $matches[1], $matches[2], $matches[3], $matches[5], $matches[4], $matches[6] );
-
- unset( $matches );
- preg_match( $preg_match_time, $data['PAID_TIME'], $matches );
- $data['PAID_TIME'] = mktime( $matches[1], $matches[2], $matches[3], $matches[5], $matches[4], $matches[6] );
-
- /* CHU?N HÓA CÁC TR?NG THÁI GIAO D?CH V? CÁC TR?NG THÁI SAU
- 0 Giao d?ch m?i t?o;
- 1 Ch?a thanh toán;
- 2 ?ă thanh toán, ?ang b? t?m gi?;
- 3 Giao d?ch b? hu?;
- 4 Giao d?ch ?ă hoŕn thŕnh thŕnh công (tr??ng h?p thanh toán ngay ho?c thanh toán t?m gi? nh?ng ng??i mua ?ă phę chu?n)
- */
- $data['nv_transaction_status'] = intval( $data['TRANSACTION_STATUS'] );
-
- return $data;
- }
-
- }
- }
- return false;
- }
- public function checkOrders ( $public_api_url, $array_order )
- {
- $data_orders_return = array();
- $param = "<ORDERS>
- <TOTAL>" . count( $array_order ) . "</TOTAL>";
- foreach ( $array_order as $arr_order_i )
- {
- $payment_id = ( empty( $arr_order_i['payment_id'] ) ) ? "" : $arr_order_i['payment_id'];
- $param .= "<ORDER>
- <ORDER_CODE>" . $arr_order_i['order_code'] . "</ORDER_CODE>
- <PAYMENT_ID>" . $payment_id . "</PAYMENT_ID>
- </ORDER>";
- }
- $param .= "</ORDERS>";
-
- $client = new nusoap_client( $public_api_url, 'wsdl' );
- $result = $client->call( 'checkOrder', array(
- 'merchant_id' => $this->merchant_site_code, 'param' => $param, 'checksum' => md5( $this->merchant_site_code . $param . $this->secure_pass )
- ) );
-
- if ( $xml = simplexml_load_string( $result ) )
- {
- $error_code = ( string )$xml->ERROR_CODE;
- if ( $error_code == "00" )
- {
- $transactions = $xml->xpath( 'TRANSACTION' );
- foreach ( $transactions as $transaction )
- {
- $data = array_map( "trim", ( array )$transaction );
- if ( $data['TRANSACTION_ERROR_CODE'] == '00' )
- {
- $preg_match_time = '/(\d{2}):(\d{2}):(\d{2}) - (\d{2})\/(\d{2})\/(\d{4})/';
-
- unset( $matches );
- preg_match( $preg_match_time, $data['CREATED_TIME'], $matches );
- $data['CREATED_TIME'] = mktime( $matches[1], $matches[2], $matches[3], $matches[5], $matches[4], $matches[6] );
-
- unset( $matches );
- preg_match( $preg_match_time, $data['PAID_TIME'], $matches );
- $data['PAID_TIME'] = mktime( $matches[1], $matches[2], $matches[3], $matches[5], $matches[4], $matches[6] );
-
- /* CHU?N HÓA CÁC TR?NG THÁI GIAO D?CH V? CÁC TR?NG THÁI SAU
- 0 Giao d?ch m?i t?o;
- 1 Ch?a thanh toán;
- 2 ?ă thanh toán, ?ang b? t?m gi?;
- 3 Giao d?ch b? hu?;
- 4 Giao d?ch ?ă hoŕn thŕnh thŕnh công (tr??ng h?p thanh toán ngay ho?c thanh toán t?m gi? nh?ng ng??i mua ?ă phę chu?n)
- */
- $data['nv_transaction_status'] = intval( $data['TRANSACTION_STATUS'] );
-
- $data_orders_return[] = $data;
- }
- }
- }
- }
- return $data_orders_return;
- }
- }
- ?>