TianzePayment.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * 天泽付
  4. * Created by PhpStorm.
  5. * User: Administrator
  6. * Date: 2019/3/4
  7. * Time: 17:28
  8. */
  9. namespace Biz\Pay\Payment;
  10. use Biz\Pay\NewBasePayment;
  11. /**
  12. * Class TianzePayment
  13. * @package Biz\Pay\Payment
  14. */
  15. class TianzePayment extends NewBasePayment
  16. {
  17. /**
  18. * 基础配置
  19. * @return void
  20. */
  21. function config()
  22. {
  23. $this->apiUrl = 'http://101.132.128.241/GateWay/ReceiveBank.aspx';
  24. $this->setReturnType();//根据客户端类型返回支付接口类型
  25. $this->success = 'success';
  26. }
  27. /**
  28. * 接收通知
  29. * @param array $data
  30. * @return void [type] [description] 返回为1或success视接口而定
  31. */
  32. function notify(array $data)
  33. {
  34. $param['r0_Cmd'] = trim(strip_tags($data['r0_Cmd']));
  35. $param['r1_Code'] = trim(strip_tags($data['r1_Code']));
  36. $param['r2_TrxId'] = trim(strip_tags($data['r2_TrxId']));
  37. $param['r3_Amt'] = trim(strip_tags($data['r3_Amt']));
  38. $param['r4_Cur'] = trim(strip_tags($data['r4_Cur']));
  39. $param['r5_Pid'] = iconv("UTF-8", "gb2312//TRANSLIT", trim(strip_tags($data['r5_Pid'])));
  40. $param['r6_Order'] = strip_tags(trim($data['r6_Order']));
  41. $param['r7_Uid'] = strip_tags(trim($data['r7_Uid']));
  42. $param['r8_MP'] = iconv("UTF-8", "gb2312//TRANSLIT", trim(strip_tags($data['r8_MP'])));
  43. $param['r9_BType'] = trim(strip_tags($data['r9_BType']));
  44. $order_no = $param['r6_Order'] ?? '';
  45. $price = $param['r3_Amt'] ? $param['r3_Amt'] : 0;
  46. $real_price = $param['r3_Amt'] ? $param['r3_Amt'] : 0;
  47. $bill_no = $param['r2_TrxId'] ?? $order_no;
  48. $sign = strip_tags(trim($data['hmac']));
  49. $this->checkSign($param, $sign, $order_no, $price, $real_price, $is_bill_no = 0, $bill_no);
  50. }
  51. /**
  52. * 同步跳转
  53. * @param array $data
  54. * @return void [type] [description]
  55. */
  56. function redirect(array $data)
  57. {
  58. $html = <<<EOT
  59. <script>setTimeout(function() {
  60. window.history.go(-2);
  61. },3000);</script>
  62. EOT;
  63. echo '支付已受理。如有疑问请联系客服!3秒后返回支付页,请稍候...';
  64. echo $html;
  65. }
  66. /**
  67. * 检查支付信息
  68. * @return void [type] 0=失败,1=成功
  69. */
  70. function check()
  71. {
  72. // TODO: Implement check() method.
  73. }
  74. /**
  75. * 返回支付类型的数组
  76. * @return void [type] [description]
  77. */
  78. function getPaymentType()
  79. {
  80. // TODO: Implement getPaymentType() method.
  81. }
  82. /**
  83. * 构建订单提交数组
  84. * @return array
  85. */
  86. function buildOrder(): array
  87. {
  88. $this->dataAccess->orderSn = str_replace(array('-', '_'), '', $this->dataAccess->orderSn);
  89. $orderInfo = array(
  90. 'p0_Cmd' => 'Buy',
  91. 'p1_MerId' => $this->paymentConfig['merchant_id'],
  92. 'p2_Order' => $this->dataAccess->orderSn,
  93. 'p3_Amt' => number_format($this->dataAccess->money, 2, '.', ''),
  94. 'p4_Cur' => 'CNY',
  95. 'p5_Pid' => iconv("UTF-8", "gb2312//TRANSLIT", $this->dataAccess->goodsName ?? 'testGoods'),
  96. 'p6_Pcat' => iconv("UTF-8", "gb2312//TRANSLIT", $this->dataAccess->goodsName ?? 'testGoodsDesc'),
  97. 'p7_Pdesc' => iconv("UTF-8", "gb2312//TRANSLIT", $this->dataAccess->goodsName ?? 'testGoodsDesc'),
  98. 'p8_Url' => $this->notifyUrl,
  99. 'p9_SAF' => '0',
  100. 'pa_MP' => iconv("UTF-8", "gb2312//TRANSLIT", $this->dataAccess->goodsName ?? 'testGoods'),
  101. 'pd_FrpId' => $this->dataAccess->payType,
  102. 'pr_NeedResponse' => '1',
  103. );
  104. $orderInfo['hmac'] = $this->getReqHmacString($orderInfo['p2_Order'], $orderInfo['p3_Amt'], $orderInfo['p4_Cur'], $orderInfo['p5_Pid'], $orderInfo['p6_Pcat'], $orderInfo['p7_Pdesc'], $orderInfo['p8_Url'], $orderInfo['pa_MP'], $orderInfo['pd_FrpId'], $orderInfo['pr_NeedResponse'], $orderInfo['p0_Cmd'], $orderInfo['p9_SAF'], $orderInfo['p1_MerId'], $this->paymentConfig['merchant_md5_secret']);
  105. return $orderInfo;
  106. }
  107. /**
  108. * 生成签名函数
  109. * @param array $param
  110. * @param string $merchantSecret
  111. * @return mixed
  112. */
  113. function buildSign(array $param, $merchantSecret)
  114. {
  115. //本例此函数只用于异步回调签名
  116. $order_info = $this->getOrder($param['r6_Order']);
  117. if (!$order_info) return false;
  118. $p1_MerId = $order_info->merchant_id;
  119. $r0_Cmd = $param['r0_Cmd'];
  120. $r1_Code = $param['r1_Code'];
  121. $r2_TrxId = $param['r2_TrxId'];
  122. $r3_Amt = $param['r3_Amt'];
  123. $r4_Cur = $param['r4_Cur'];
  124. $r5_Pid = $param['r5_Pid'];
  125. $r6_Order = $param['r6_Order'];
  126. $r7_Uid = $param['r7_Uid'];
  127. $r8_MP = $param['r8_MP'];
  128. $r9_BType = $param['r9_BType'];
  129. #取得加密前的字符串
  130. $sbOld = "";
  131. #加入商家ID
  132. $sbOld = $sbOld . $p1_MerId;
  133. #加入消息类型
  134. $sbOld = $sbOld . $r0_Cmd;
  135. #加入业务返回码
  136. $sbOld = $sbOld . $r1_Code;
  137. #加入交易ID
  138. $sbOld = $sbOld . $r2_TrxId;
  139. #加入交易金额
  140. $sbOld = $sbOld . $r3_Amt;
  141. #加入货币单位
  142. $sbOld = $sbOld . $r4_Cur;
  143. #加入产品Id
  144. $sbOld = $sbOld . $r5_Pid;
  145. #加入订单ID
  146. $sbOld = $sbOld . $r6_Order;
  147. #加入用户ID
  148. $sbOld = $sbOld . $r7_Uid;
  149. #加入商家扩展信息
  150. $sbOld = $sbOld . $r8_MP;
  151. #加入交易结果返回类型
  152. $sbOld = $sbOld . $r9_BType;
  153. return $this->HmacMd5($sbOld, $merchantSecret);
  154. }
  155. /**
  156. * 设置返回类型
  157. * @param string $type
  158. */
  159. function setReturnType($type = 'HTML')
  160. {
  161. // TODO: Implement setReturnType() method.
  162. $this->returnType = $type;
  163. }
  164. /**
  165. * 发起支付函数
  166. */
  167. function goPay()
  168. {
  169. $this->debug = 0;
  170. $this->method = "POST";
  171. $this->prePay();
  172. }
  173. /**
  174. * 签名函数
  175. * @param $param
  176. * @param $merchantSecret
  177. * @return string
  178. */
  179. protected function HmacMd5($param, $merchantSecret)
  180. {
  181. $key = iconv("GB2312", "UTF-8", $merchantSecret);
  182. $data = iconv("GB2312", "UTF-8", $param);
  183. $b = 64; // byte length for md5
  184. if (strlen($key) > $b) {
  185. $key = pack("H*", md5($key));
  186. }
  187. $key = str_pad($key, $b, chr(0x00));
  188. $ipad = str_pad('', $b, chr(0x36));
  189. $opad = str_pad('', $b, chr(0x5c));
  190. $k_ipad = $key ^ $ipad;
  191. $k_opad = $key ^ $opad;
  192. return md5($k_opad . pack("H*", md5($k_ipad . $data)));
  193. }
  194. /**
  195. * 发起支付签名字符串
  196. * @param $p2_Order
  197. * @param $p3_Amt
  198. * @param $p4_Cur
  199. * @param $p5_Pid
  200. * @param $p6_Pcat
  201. * @param $p7_Pdesc
  202. * @param $p8_Url
  203. * @param $pa_MP
  204. * @param $pd_FrpId
  205. * @param $pr_NeedResponse
  206. * @param $p0_Cmd
  207. * @param $p9_SAF
  208. * @param $p1_MerId
  209. * @param $merchantKey
  210. * @return mixed
  211. */
  212. protected function getReqHmacString($p2_Order, $p3_Amt, $p4_Cur, $p5_Pid, $p6_Pcat, $p7_Pdesc, $p8_Url, $pa_MP, $pd_FrpId, $pr_NeedResponse, $p0_Cmd, $p9_SAF, $p1_MerId, $merchantKey)
  213. {
  214. #进行签名处理,一定按照文档中标明的签名顺序进行
  215. $sbOld = "";
  216. #加入业务类型
  217. $sbOld = $sbOld . $p0_Cmd;
  218. #加入商户编号
  219. $sbOld = $sbOld . $p1_MerId;
  220. #加入商户订单号
  221. $sbOld = $sbOld . $p2_Order;
  222. #加入支付金额
  223. $sbOld = $sbOld . $p3_Amt;
  224. #加入交易币种
  225. $sbOld = $sbOld . $p4_Cur;
  226. #加入商品名称
  227. $sbOld = $sbOld . $p5_Pid;
  228. #加入商品分类
  229. $sbOld = $sbOld . $p6_Pcat;
  230. #加入商品描述
  231. $sbOld = $sbOld . $p7_Pdesc;
  232. #加入商户接收支付成功数据的地址
  233. $sbOld = $sbOld . $p8_Url;
  234. #加入送货地址标识
  235. $sbOld = $sbOld . $p9_SAF;
  236. #加入商户扩展信息
  237. $sbOld = $sbOld . $pa_MP;
  238. #加入支付通道编码
  239. $sbOld = $sbOld . $pd_FrpId;
  240. #加入是否需要应答机制
  241. $sbOld = $sbOld . $pr_NeedResponse;
  242. return $this->HmacMd5($sbOld, $merchantKey);
  243. }
  244. }