QwjPayment.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/2/23
  6. * Time: 13:45
  7. */
  8. namespace Biz\Pay\Payment;
  9. use Biz\Pay\NewBasePayment;
  10. use Biz\Pay\PayInfo;
  11. use stdClass;
  12. class QwjPayment extends NewBasePayment
  13. {
  14. /**
  15. * 基础配置
  16. * @return void
  17. */
  18. function config()
  19. {
  20. $this->apiUrl = 'http://pay.qwj8686.com/orderpay.do';
  21. $this->setReturnType();//根据客户端类型返回支付接口类型
  22. $this->success = 'success';
  23. }
  24. /**
  25. * 接收通知
  26. * @param array $data
  27. * @return void [type] [description] 返回为1或success视接口而定
  28. */
  29. function notify(array $data)
  30. {
  31. $param = [
  32. 'retCode' => strip_tags($data['retCode']),
  33. 'userId' => strip_tags($data['userId']),
  34. 'orderNo' => strip_tags($data['orderNo']),
  35. 'transNo' => strip_tags($data['transNo']),
  36. 'payAmt' => strip_tags($data['payAmt']),
  37. 'sign' => strip_tags($data['sign']),
  38. ];
  39. $order_no = $param['orderNo'] ?? '';
  40. $bill_no = $param['transNo'] ?? '';
  41. $price = $param['payAmt'] ? $param['payAmt'] : 0;
  42. $real_price = $param['payAmt'] ? $param['payAmt'] : 0;
  43. $sign = strip_tags(trim($param['sign']));
  44. $this->checkSign($param, $sign, $order_no, $price, $real_price,false,$bill_no);
  45. }
  46. /**
  47. * 同步跳转
  48. * @param array $data
  49. * @return void [type] [description]
  50. */
  51. function redirect(array $data)
  52. {
  53. $html = <<<EOT
  54. <script>setTimeout(function() {
  55. window.history.go(-2);
  56. },3000);</script>
  57. EOT;
  58. echo '支付已受理。如有疑问请联系客服!3秒后返回支付页,请稍候...';
  59. echo $html;
  60. }
  61. /**
  62. * 检查支付信息
  63. * @return void [type] 0=失败,1=成功
  64. */
  65. function check()
  66. {
  67. // TODO: Implement check() method.
  68. }
  69. /**
  70. * 返回支付类型的数组
  71. * @return void [type] [description]
  72. */
  73. function getPaymentType()
  74. {
  75. // TODO: Implement getPaymentType() method.
  76. }
  77. /**
  78. * 构建订单提交数组
  79. * @return array
  80. */
  81. function buildOrder(): array
  82. {
  83. $orderInfo = [
  84. 'userId' => $this->paymentConfig['merchant_id'],
  85. 'orderNo' => $this->dataAccess->orderSn,
  86. 'tradeType' => $this->dataAccess->payType,
  87. 'payAmt' => $this->dataAccess->money,
  88. 'goodsName' => $this->dataAccess->goodsName ?? 'testGoods',
  89. 'notifyUrl' => $this->notifyUrl,
  90. 'returnUrl' => $this->redirectUrl,
  91. ];
  92. $sign = $this->create_sign($orderInfo, $this->paymentConfig['merchant_md5_secret']);
  93. $orderInfo['sign'] = $sign;
  94. return $orderInfo;
  95. }
  96. /**
  97. * 生成签名函数
  98. * @param array $param
  99. * @param string $merchantSecret
  100. * @return mixed
  101. */
  102. function create_sign($postData, $key) {
  103. $sign_str = '';
  104. $sign_str = $sign_str . 'notifyUrl=' . $postData['notifyUrl'];
  105. $sign_str = $sign_str . '&orderNo=' . $postData['orderNo'];
  106. $sign_str = $sign_str . '&payAmt=' . $postData['payAmt'];
  107. $sign_str = $sign_str . '&returnUrl=' . $postData['returnUrl'];
  108. $sign_str = $sign_str . '&tradeType=' . $postData['tradeType'];
  109. $sign_str = $sign_str . '&userId=' . $postData['userId'];
  110. $sign_str = $sign_str . '&key=' . $key;
  111. return (md5($sign_str));
  112. }
  113. //回调签名函数
  114. function buildSign(array $param, $merchantSecret)
  115. {
  116. $signStr = '';
  117. $signStr = $signStr . 'orderNo=' . $param['orderNo'];
  118. $signStr = $signStr . '&payAmt=' . $param['payAmt'];
  119. $signStr = $signStr . '&retCode=' . $param['retCode'];
  120. $signStr = $signStr . '&transNo=' . $param['transNo'];
  121. $signStr = $signStr . '&userId=' . $param['userId'];
  122. $signStr = $signStr . '&key=' . $merchantSecret;
  123. return (md5($signStr));
  124. }
  125. /**
  126. * 设置返回类型
  127. * @param string $type
  128. */
  129. function setReturnType($type = 'HTML')
  130. {
  131. // TODO: Implement setReturnType() method.
  132. $this->returnType = $type;
  133. }
  134. /**
  135. * 发起支付函数
  136. */
  137. function goPay()
  138. {
  139. $this->debug = 0;
  140. $orderInfo = $this->buildOrder();
  141. $header = array('Content-Type: application/x-www-form-urlencoded');
  142. $json = $this->goPayCurl($this->apiUrl, $orderInfo,$header);
  143. $ret = json_decode($json, 1);
  144. if (!$ret || $ret['retCode'] != '0') {
  145. Render(new stdClass(), '-1', $ret['retMsg']);
  146. }
  147. $apiUrl = $ret['payUrl'];
  148. $this->apiUrl = $apiUrl;
  149. $this->method = 'GET';
  150. $this->prePay(1);
  151. }
  152. }