PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  1. <?php
  2. /**
  3. *
  4. * Phięn b?n: 0.1
  5. * Tęn l?p: NL_CheckOut
  6. * Ch?c n?ng: Tích h?p thanh toán qua nganluong.vn cho các merchant site có ??ng ký API
  7. * - Xây d?ng URL chuy?n thông tin t?i Nganluong.vn ?? x? lý vi?c thanh toán cho merchant site.
  8. * - 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.
  9. *
  10. **/
  11. require_once ( NV_ROOTDIR . "/includes/class/nusoap.php" );
  12. class NL_Checkout
  13. {
  14. // URL chheckout c?a nganluong.vn
  15. private $nganluong_url = '';
  16. private $merchant_site_code;
  17. private $secure_pass;
  18. function __construct ( $nganluong_url, $merchant_site_code, $secure_pass )
  19. {
  20. $this->nganluong_url = trim( $nganluong_url );
  21. $this->merchant_site_code = trim( $merchant_site_code );
  22. $this->secure_pass = trim( $secure_pass );
  23. }
  24. //Hŕm xây d?ng url, trong ?ó có tham s? mă hóa (cňn g?i lŕ public key)
  25. public function buildCheckoutUrl ( $return_url, $receiver, $transaction_info, $order_code, $price )
  26. {
  27. // M?ng các tham s? chuy?n t?i nganluong.vn
  28. $arr_param = array(
  29. '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 )
  30. );
  31. $secure_code = implode( ' ', $arr_param ) . ' ' . $this->secure_pass;
  32. $arr_param['secure_code'] = md5( $secure_code );
  33. /* B??c 2. Ki?m tra bi?n $redirect_url xem có '?' không, n?u không có thě b? sung vŕo*/
  34. $redirect_url = $this->nganluong_url;
  35. if ( strpos( $redirect_url, '?' ) === false )
  36. {
  37. $redirect_url .= '?';
  38. }
  39. else if ( substr( $redirect_url, strlen( $redirect_url ) - 1, 1 ) != '?' && strpos( $redirect_url, '&' ) === false )
  40. {
  41. // 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
  42. $redirect_url .= '&';
  43. }
  44. /* B??c 3. t?o url*/
  45. $url = '';
  46. foreach ( $arr_param as $key => $value )
  47. {
  48. if ( $url == '' ) $url .= $key . '=' . $value;
  49. else $url .= '&' . $key . '=' . $value;
  50. }
  51. return $redirect_url . $url;
  52. }
  53. /*Hŕm th?c hi?n xác minh tính ?úng ??n c?a các tham s? tr? v? t? nganluong.vn*/
  54. public function verifyPaymentUrl ( $transaction_info, $order_code, $price, $payment_id, $payment_type, $error_text, $secure_code )
  55. {
  56. // T?o mă xác th?c t? ch? web
  57. $str = '';
  58. $str .= ' ' . strval( $transaction_info );
  59. $str .= ' ' . strval( $order_code );
  60. $str .= ' ' . strval( $price );
  61. $str .= ' ' . strval( $payment_id );
  62. $str .= ' ' . strval( $payment_type );
  63. $str .= ' ' . strval( $error_text );
  64. $str .= ' ' . strval( $this->merchant_site_code );
  65. $str .= ' ' . strval( $this->secure_pass );
  66. // Mă hóa các tham s?
  67. $verify_secure_code = md5( $str );
  68. // Xác th?c mă c?a ch? web v?i mă tr? v? t? nganluong.vn
  69. if ( $verify_secure_code === $secure_code ) return true;
  70. return false;
  71. }
  72. public function checkOrder ( $public_api_url, $order_code, $payment_id )
  73. {
  74. $payment_id = ( empty( $payment_id ) ) ? "" : $payment_id;
  75. $param = "<ORDERS>
  76. <TOTAL>1</TOTAL>
  77. <ORDER>
  78. <ORDER_CODE>" . $order_code . "</ORDER_CODE>
  79. <PAYMENT_ID>" . $payment_id . "</PAYMENT_ID>
  80. </ORDER>
  81. </ORDERS>";
  82. $client = new nusoap_client( $public_api_url, 'wsdl' );
  83. $result = $client->call( 'checkOrder', array(
  84. 'merchant_id' => $this->merchant_site_code, 'param' => $param, 'checksum' => md5( $this->merchant_site_code . $param . $this->secure_pass )
  85. ) );
  86. if ( $xml = simplexml_load_string( $result ) )
  87. {
  88. $error_code = ( string )$xml->ERROR_CODE;
  89. if ( $error_code == "00" )
  90. {
  91. $transaction = $xml->xpath( 'TRANSACTION' );
  92. $data = array_map( "trim", ( array )$transaction[0] );
  93. if ( $data['TRANSACTION_ERROR_CODE'] == '00' )
  94. {
  95. $preg_match_time = '/(\d{2}):(\d{2}):(\d{2}) - (\d{2})\/(\d{2})\/(\d{4})/';
  96. unset( $matches );
  97. preg_match( $preg_match_time, $data['CREATED_TIME'], $matches );
  98. $data['CREATED_TIME'] = mktime( $matches[1], $matches[2], $matches[3], $matches[5], $matches[4], $matches[6] );
  99. unset( $matches );
  100. preg_match( $preg_match_time, $data['PAID_TIME'], $matches );
  101. $data['PAID_TIME'] = mktime( $matches[1], $matches[2], $matches[3], $matches[5], $matches[4], $matches[6] );
  102. /* CHU?N HÓA CÁC TR?NG THÁI GIAO D?CH V? CÁC TR?NG THÁI SAU
  103. 0 – Giao d?ch m?i t?o;
  104. 1 – Ch?a thanh toán;
  105. 2 – ?ă thanh toán, ?ang b? t?m gi?;
  106. 3 – Giao d?ch b? hu?;
  107. 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)
  108. */
  109. $data['nv_transaction_status'] = intval( $data['TRANSACTION_STATUS'] );
  110. return $data;
  111. }
  112. }
  113. }
  114. return false;
  115. }
  116. public function checkOrders ( $public_api_url, $array_order )
  117. {
  118. $data_orders_return = array();
  119. $param = "<ORDERS>
  120. <TOTAL>" . count( $array_order ) . "</TOTAL>";
  121. foreach ( $array_order as $arr_order_i )
  122. {
  123. $payment_id = ( empty( $arr_order_i['payment_id'] ) ) ? "" : $arr_order_i['payment_id'];
  124. $param .= "<ORDER>
  125. <ORDER_CODE>" . $arr_order_i['order_code'] . "</ORDER_CODE>
  126. <PAYMENT_ID>" . $payment_id . "</PAYMENT_ID>
  127. </ORDER>";
  128. }
  129. $param .= "</ORDERS>";
  130. $client = new nusoap_client( $public_api_url, 'wsdl' );
  131. $result = $client->call( 'checkOrder', array(
  132. 'merchant_id' => $this->merchant_site_code, 'param' => $param, 'checksum' => md5( $this->merchant_site_code . $param . $this->secure_pass )
  133. ) );
  134. if ( $xml = simplexml_load_string( $result ) )
  135. {
  136. $error_code = ( string )$xml->ERROR_CODE;
  137. if ( $error_code == "00" )
  138. {
  139. $transactions = $xml->xpath( 'TRANSACTION' );
  140. foreach ( $transactions as $transaction )
  141. {
  142. $data = array_map( "trim", ( array )$transaction );
  143. if ( $data['TRANSACTION_ERROR_CODE'] == '00' )
  144. {
  145. $preg_match_time = '/(\d{2}):(\d{2}):(\d{2}) - (\d{2})\/(\d{2})\/(\d{4})/';
  146. unset( $matches );
  147. preg_match( $preg_match_time, $data['CREATED_TIME'], $matches );
  148. $data['CREATED_TIME'] = mktime( $matches[1], $matches[2], $matches[3], $matches[5], $matches[4], $matches[6] );
  149. unset( $matches );
  150. preg_match( $preg_match_time, $data['PAID_TIME'], $matches );
  151. $data['PAID_TIME'] = mktime( $matches[1], $matches[2], $matches[3], $matches[5], $matches[4], $matches[6] );
  152. /* CHU?N HÓA CÁC TR?NG THÁI GIAO D?CH V? CÁC TR?NG THÁI SAU
  153. 0 – Giao d?ch m?i t?o;
  154. 1 – Ch?a thanh toán;
  155. 2 – ?ă thanh toán, ?ang b? t?m gi?;
  156. 3 – Giao d?ch b? hu?;
  157. 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)
  158. */
  159. $data['nv_transaction_status'] = intval( $data['TRANSACTION_STATUS'] );
  160. $data_orders_return[] = $data;
  161. }
  162. }
  163. }
  164. }
  165. return $data_orders_return;
  166. }
  167. }
  168. ?>