PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mall/includes/modules/payment/tenpay.php

https://bitbucket.org/huanteng/touyou
PHP | 242 lines | 142 code | 40 blank | 60 comment | 15 complexity | d88da69822bbc1a04aecbb8bbacc878b MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * ECSHOP 财付通插件
  4. * ============================================================================
  5. * * 版权所有 2005-2012 上海商派网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.ecshop.com;
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布。
  10. * ============================================================================
  11. * $Author: liubo $
  12. * $Id: tenpay.php 17217 2011-01-19 06:29:08Z liubo $
  13. */
  14. if (!defined('IN_ECS'))
  15. {
  16. die('Hacking attempt');
  17. }
  18. $payment_lang = ROOT_PATH . 'languages/' .$GLOBALS['_CFG']['lang']. '/payment/tenpay.php';
  19. if (file_exists($payment_lang))
  20. {
  21. global $_LANG;
  22. include_once($payment_lang);
  23. }
  24. /* 模块的基本信息 */
  25. if (isset($set_modules) && $set_modules == TRUE)
  26. {
  27. $i = isset($modules) ? count($modules) : 0;
  28. /* 代码 */
  29. $modules[$i]['code'] = basename(__FILE__, '.php');
  30. /* 描述对应的语言项 */
  31. $modules[$i]['desc'] = 'tenpay_desc';
  32. /* 是否支持货到付款 */
  33. $modules[$i]['is_cod'] = '0';
  34. /* 是否支持在线支付 */
  35. $modules[$i]['is_online'] = '1';
  36. /* 作者 */
  37. $modules[$i]['author'] = 'ECSHOP TEAM';
  38. /* 网址 */
  39. $modules[$i]['website'] = 'http://www.tenpay.com';
  40. /* 版本号 */
  41. $modules[$i]['version'] = '2.0.0';
  42. /* 配置信息 */
  43. $modules[$i]['config'] = array(
  44. array('name' => 'tenpay_account', 'type' => 'text', 'value' => ''),
  45. array('name' => 'tenpay_key', 'type' => 'text', 'value' => ''),
  46. array('name' => 'magic_string', 'type' => 'text', 'value' => '')
  47. );
  48. return;
  49. }
  50. /**
  51. * 类
  52. */
  53. class tenpay
  54. {
  55. /**
  56. * 构造函数
  57. *
  58. * @access public
  59. * @param
  60. *
  61. * @return void
  62. */
  63. function tenpay()
  64. {
  65. }
  66. function __construct()
  67. {
  68. $this->tenpay();
  69. }
  70. /**
  71. * 生成支付代码
  72. * @param array $order 订单信息
  73. * @param array $payment 支付方式信息
  74. */
  75. function get_code($order, $payment)
  76. {
  77. $cmd_no = '1';
  78. /* 获得订单的流水号,补零到10位 */
  79. $sp_billno = $order['order_sn'];
  80. /* 交易日期 */
  81. $today = date('Ymd');
  82. /* 将商户号+年月日+流水号 */
  83. $bill_no = str_pad($order['log_id'], 10, 0, STR_PAD_LEFT);
  84. $transaction_id = $payment['tenpay_account'].$today.$bill_no;
  85. /* 银行类型:支持纯网关和财付通 */
  86. $bank_type = '0';
  87. /* 订单描述,用订单号替代 */
  88. if (!empty($order['order_id']))
  89. {
  90. //$desc = get_goods_name_by_id($order['order_id']);
  91. $desc = $order['order_sn'];
  92. $attach = '';
  93. }
  94. else
  95. {
  96. $desc = $GLOBALS['_LANG']['account_voucher'];
  97. $attach = 'voucher';
  98. }
  99. /* 编码标准 */
  100. if (!defined('EC_CHARSET') || EC_CHARSET == 'utf-8')
  101. {
  102. $desc = ecs_iconv('utf-8', 'gbk', $desc);
  103. }
  104. /* 返回的路径 */
  105. $return_url = return_url('tenpay');
  106. /* 总金额 */
  107. $total_fee = floatval($order['order_amount']) * 100;
  108. /* 货币类型 */
  109. $fee_type = '1';
  110. /* 财付通风险防范参数 */
  111. $spbill_create_ip = $_SERVER['REMOTE_ADDR'];
  112. /* 数字签名 */
  113. $sign_text = "cmdno=" . $cmd_no . "&date=" . $today . "&bargainor_id=" . $payment['tenpay_account'] .
  114. "&transaction_id=" . $transaction_id . "&sp_billno=" . $sp_billno .
  115. "&total_fee=" . $total_fee . "&fee_type=" . $fee_type . "&return_url=" . $return_url .
  116. "&attach=" . $attach . "&spbill_create_ip=" . $spbill_create_ip . "&key=" . $payment['tenpay_key'];
  117. $sign = strtoupper(md5($sign_text));
  118. /* 交易参数 */
  119. $parameter = array(
  120. 'cmdno' => $cmd_no, // 业务代码, 财付通支付支付接口填 1
  121. 'date' => $today, // 商户日期:如20051212
  122. 'bank_type' => $bank_type, // 银行类型:支持纯网关和财付通
  123. 'desc' => $desc, // 交易的商品名称
  124. 'purchaser_id' => '', // 用户(买方)的财付通帐户,可以为空
  125. 'bargainor_id' => $payment['tenpay_account'], // 商家的财付通商户号
  126. 'transaction_id' => $transaction_id, // 交易号(订单号),由商户网站产生(建议顺序累加)
  127. 'sp_billno' => $sp_billno, // 商户系统内部的定单号,最多10位
  128. 'total_fee' => $total_fee, // 订单金额
  129. 'fee_type' => $fee_type, // 现金支付币种
  130. 'return_url' => $return_url, // 接收财付通返回结果的URL
  131. 'attach' => $attach, // 用户自定义签名
  132. 'sign' => $sign, // MD5签名
  133. 'spbill_create_ip' => $spbill_create_ip, //财付通风险防范参数
  134. 'sys_id' => '542554970', //ecshop C账号 不参与签名
  135. 'sp_suggestuser' => '1202822001' //财付通分配的商户号
  136. );
  137. $button = '<br /><form style="text-align:center;" action="https://www.tenpay.com/cgi-bin/v1.0/pay_gate.cgi" target="_blank" style="margin:0px;padding:0px" >';
  138. foreach ($parameter AS $key=>$val)
  139. {
  140. $button .= "<input type='hidden' name='$key' value='$val' />";
  141. }
  142. $button .= '<input type="image" src="'. $GLOBALS['ecs']->url() .'images/tenpay.gif" value="' .$GLOBALS['_LANG']['pay_button']. '" /></form><br />';
  143. return $button;
  144. }
  145. /**
  146. * 响应操作
  147. */
  148. function respond()
  149. {
  150. /*取返回参数*/
  151. $cmd_no = $_GET['cmdno'];
  152. $pay_result = $_GET['pay_result'];
  153. $pay_info = $_GET['pay_info'];
  154. $bill_date = $_GET['date'];
  155. $bargainor_id = $_GET['bargainor_id'];
  156. $transaction_id = $_GET['transaction_id'];
  157. $sp_billno = $_GET['sp_billno'];
  158. $total_fee = $_GET['total_fee'];
  159. $fee_type = $_GET['fee_type'];
  160. $attach = $_GET['attach'];
  161. $sign = $_GET['sign'];
  162. $payment = get_payment('tenpay');
  163. //$order_sn = $bill_date . str_pad(intval($sp_billno), 5, '0', STR_PAD_LEFT);
  164. //$log_id = preg_replace('/0*([0-9]*)/', '\1', $sp_billno); //取得支付的log_id
  165. if ($attach == 'voucher')
  166. {
  167. $log_id = get_order_id_by_sn($sp_billno, "true");
  168. }
  169. else
  170. {
  171. $log_id = get_order_id_by_sn($sp_billno);
  172. }
  173. /* 如果pay_result大于0则表示支付失败 */
  174. if ($pay_result > 0)
  175. {
  176. return false;
  177. }
  178. /* 检查支付的金额是否相符 */
  179. if (!check_money($log_id, $total_fee / 100))
  180. {
  181. return false;
  182. }
  183. /* 检查数字签名是否正确 */
  184. $sign_text = "cmdno=" . $cmd_no . "&pay_result=" . $pay_result .
  185. "&date=" . $bill_date . "&transaction_id=" . $transaction_id .
  186. "&sp_billno=" . $sp_billno . "&total_fee=" . $total_fee .
  187. "&fee_type=" . $fee_type . "&attach=" . $attach .
  188. "&key=" . $payment['tenpay_key'];
  189. $sign_md5 = strtoupper(md5($sign_text));
  190. if ($sign_md5 != $sign)
  191. {
  192. return false;
  193. }
  194. else
  195. {
  196. /* 改变订单状态 */
  197. order_paid($log_id);
  198. return true;
  199. }
  200. }
  201. }
  202. ?>