XinhuiyidaiPayment.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/1/31
  6. * Time: 17:28
  7. */
  8. namespace Biz\Pay\Payment;
  9. use Biz\Pay\NewBasePayment;
  10. use stdClass;
  11. class XinhuiyidaiPayment extends NewBasePayment
  12. {
  13. function config()
  14. {
  15. $this->apiUrl = 'http://www.xhfpay2018.com/channel/payment';
  16. $this->setReturnType();//根据客户端类型返回支付接口类型
  17. $this->success = 'success';
  18. }
  19. /**
  20. * 接收通知
  21. * @param array $data
  22. * @return void [type] [description] 返回为1或success视接口而定
  23. */
  24. //http://www.localdev.com/Payment-Pay/Notify?paymentName=Xinhuiyidai&channel_id=4&mchid=10902&order_id=XC190222163222D150&order_no=XHF20190222163250376693&total_amount=1.00&sign=EDF8083DE10AC714FFE818C30F897164A471B514&
  25. //channel_id=4&mchid=10902&order_id=XC190222163222D150&order_no=XHF20190222163250376693&total_amount=1.00&sign=EDF8083DE10AC714FFE818C30F897164A471B514&
  26. //2019-02-22 16:33:04|para[channel_id=4&mchid=10902&order_id=XC190222163222D150&order_no=XHF20190222163250376693&total_amount=1.00&sign=EDF8083DE10AC714FFE818C30F897164A471B514&]|sign[EDF8083DE10AC714FFE818C30F897164A471B514]|other[EDF8083DE10AC714FFE818C30F897164A471B514]
  27. function notify(array $data)
  28. {
  29. $channel_code = $data['channel_id'];
  30. $gateway_identifier = $data['paymentName'];
  31. $param = [
  32. 'channel_id' => strip_tags($data['channel_id']),
  33. 'mchid' => strip_tags($data['mchid']),
  34. 'order_id' => strip_tags($data['order_id']),
  35. 'order_no' => strip_tags($data['order_no']),
  36. 'total_amount' => strip_tags($data['total_amount']),
  37. ];
  38. $order_no = $param['order_id'] ?? '';
  39. $price = $param['total_amount'] ? $param['total_amount'] : 0;
  40. $real_price = $param['total_amount'] ? $param['total_amount'] : 0;
  41. $sign = strip_tags(trim($data['sign']));
  42. $this->checkSign($param, $sign, $channel_code, $gateway_identifier, $order_no, $price, $real_price);
  43. }
  44. /**
  45. * 同步跳转
  46. * @param array $data
  47. * @return void [type] [description]
  48. */
  49. function redirect(array $data)
  50. {
  51. echo 'success';
  52. }
  53. /**
  54. * 检查支付信息
  55. * @return void [type] 0=失败,1=成功
  56. */
  57. function check()
  58. {
  59. // TODO: Implement check() method.
  60. }
  61. /**
  62. * 返回支付类型的数组
  63. * @return void [type] [description]
  64. */
  65. function getPaymentType()
  66. {
  67. // TODO: Implement getPaymentType() method.
  68. }
  69. /**
  70. * 构建订单提交数组
  71. * @return array
  72. */
  73. function buildOrder(): array
  74. {
  75. $orderInfo = [
  76. 'mchid' => $this->paymentConfig['merchant_id'],
  77. 'order_id' => $this->dataAccess->orderSn,
  78. 'total_amount' => number_format($this->dataAccess->money, 2, '.', ''),
  79. 'channel_id' => $this->dataAccess->payType,
  80. 'return_url' => $this->redirectUrl,
  81. 'notify_url' => $this->notifyUrl,
  82. ];
  83. $sign = $this->buildSign($orderInfo, $this->paymentConfig['merchant_md5_secret']);
  84. $orderInfo['sign'] = $sign;
  85. return $orderInfo;
  86. }
  87. /**
  88. * 生成签名函数
  89. * @param array $param
  90. * @param string $merchantSecret
  91. * @return mixed
  92. */
  93. function buildSign(array $param, $merchantSecret)
  94. {
  95. if (!is_array($param)) {
  96. return false;
  97. }
  98. ksort($param, SORT_NATURAL | SORT_FLAG_CASE);
  99. $signStr = '';
  100. foreach ($param as $k => $v) {
  101. // if (!$v) continue;
  102. $signStr = $signStr . $k . $v;
  103. }
  104. // $signStr = trim($signStr, '&') . $merchantSecret;
  105. $signStr = $signStr . $merchantSecret;
  106. return (strtoupper(sha1($signStr)));
  107. }
  108. /**
  109. * 设置返回类型
  110. * @param string $type
  111. */
  112. function setReturnType($type = 'HTML')
  113. {
  114. $this->returnType = $type;
  115. }
  116. /**
  117. * 发起支付函数
  118. */
  119. function goPay()
  120. {
  121. $this->debug = 1;
  122. $orderInfo = $this->buildOrder();
  123. $json = $this->goPayCurl($this->apiUrl, $orderInfo);
  124. $ret = json_decode($json, 1);
  125. if (!$ret || !$ret['status']) {
  126. Render(new stdClass(), -10086, $ret['msg']??'未知错误');
  127. }
  128. $apiUrl = $ret['url'];
  129. $this->apiUrl = $apiUrl;
  130. $this->orderInfo = [];
  131. // $this->method='JSON';
  132. $this->prePay(1);
  133. }
  134. }