XiyouPayment.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace App\Repositories\Service\Payment;
  3. class XiyouPayment extends PaymentInterface
  4. {
  5. /**
  6. * 支付请求地址
  7. *
  8. * @var string
  9. */
  10. private $payUrl = 'http://wed.xiyousf.top/Pay_Index.html';
  11. /**
  12. * 查询请求地址
  13. *
  14. * @var string
  15. */
  16. private $queryUrl = 'http://wed.xiyousf.top/Pay_Trade_query.html';
  17. /**
  18. * 公钥
  19. *
  20. * @var string
  21. */
  22. private $publicKey = 'jl63lzr5ddy9m9f3of9i29by8h6ft4yf';
  23. const SUCESS_CODE = '00';
  24. /**
  25. * @var null
  26. */
  27. private static $_instance = null;
  28. /**
  29. * @return WhiteSharkPayment|null
  30. */
  31. public static function getInstance()
  32. {
  33. if (is_null(self::$_instance)) {
  34. self::$_instance = new self();
  35. }
  36. return self::$_instance;
  37. }
  38. public function createPayLink($data = array())
  39. {
  40. $payInfo = [
  41. 'pay_memberid' => $data['partner_id'], //商户号
  42. 'pay_orderid' => $data['order_sn'],
  43. 'pay_applydate' => date('Y-m-d H:i:s'),
  44. 'pay_bankcode' => $data['pay_attr_code'],
  45. 'pay_notifyurl' => $data['notify_url'],
  46. 'pay_callbackurl' => $data['pay_url'],
  47. 'pay_amount' => sprintf('%.2f', $data['money']),
  48. ];
  49. $payInfo['pay_md5sign'] = strtoupper(md5($this->getSign($payInfo) . "key=" . $this->publicKey));
  50. $sRes = $this->httpPost($this->payUrl, $payInfo);
  51. if (empty($sRes)) {
  52. $this->_lastError = '请求异常';
  53. payLogger(__CLASS__,'请求异常data='.$sRes,$payInfo);
  54. return '';
  55. }
  56. $aRes = json_decode($sRes, true);
  57. if (empty($aRes)) {
  58. $this->_lastError = '请求异常!';
  59. payLogger(__CLASS__,'请求异常 (解析) data='.$sRes,$payInfo);
  60. return '';
  61. }
  62. if ((isset($aRes['code']) && $aRes['code'] != 200) || (isset($aRes['status']) && $aRes['status']== 'error')) {
  63. $this->_lastError = '请求异常,请求失败';
  64. payLogger(__CLASS__,'请求异常,请求失败 data='.$sRes,$payInfo);
  65. return '';
  66. }
  67. return $aRes['data'];
  68. }
  69. public function queryOrder($data, &$orderSn, &$money, $partnerId)
  70. {
  71. $queryInfo = [
  72. 'pay_memberid' => $partnerId,
  73. 'pay_orderid' => $orderSn
  74. ];
  75. $queryInfo['pay_md5sign'] = strtoupper(md5($this->getSign($queryInfo) . "key=" . $this->publicKey));
  76. $sRes = $this->httpPost($this->queryUrl, $queryInfo);
  77. if (empty($sRes)) {
  78. $this->_lastError = '查询请求异常';
  79. payLogger(__CLASS__,'查询请求异常 $queryInfo='.json_encode($queryInfo),$data);
  80. return '';
  81. }
  82. $aRes = json_decode($sRes, true);
  83. if (empty($aRes)) {
  84. $this->_lastError = '查询解析异常';
  85. payLogger(__CLASS__,'查询解析异常 data='.$sRes,$data);
  86. return '';
  87. }
  88. if ($aRes['returncode'] != self::SUCESS_CODE || $aRes['trade_state'] !== 'SUCCESS') {
  89. $this->_lastError = '查询订单状态异常';
  90. payLogger(__CLASS__,'查询订单状态异常 data='.$sRes,$data);
  91. return '';
  92. }
  93. if ($money != $aRes['amount']) {
  94. $this->_lastError = '查询订单金额异常';
  95. payLogger(__CLASS__,'查询订单金额异常 data='.$sRes,$data);
  96. return '';
  97. }
  98. return true;
  99. }
  100. public function notify(&$orderSn, &$tradeNo, &$money, $partnerId)
  101. {
  102. $data = request()->all();
  103. if ($data['returncode'] != self::SUCESS_CODE) {
  104. payLogger(__CLASS__,'回调订单状态失败',$data);
  105. return false;
  106. }
  107. $info = [
  108. 'memberid' => $partnerId,
  109. 'orderid' => $data['orderid'],
  110. 'amount' => $data['amount'],
  111. 'transaction_id' => $data['transaction_id'],
  112. 'datetime' => $data['datetime'],
  113. 'returncode' => $data['returncode']
  114. ];
  115. $sign = strtoupper(md5($this->getSign($info) . "key=" . $this->publicKey));
  116. if ($sign != $data['sign']) {
  117. $this->_lastError = '验证失败!';
  118. payLogger(__CLASS__,'验证失败 $sign='.$sign,$data);
  119. return false;
  120. }
  121. $money = $data['amount'];
  122. $res = $this->queryOrder($data, $data['orderid'], $money, $partnerId);
  123. if (empty($res)) {
  124. return '';
  125. }
  126. $orderSn = $data['orderid'];
  127. $tradeNo = $data['transaction_id'];
  128. return true;
  129. }
  130. public function stopNotify()
  131. {
  132. return 'ok';
  133. }
  134. public function queryRradeNo($orderSn, $partnerId)
  135. {
  136. $queryInfo = [
  137. 'pay_memberid' => $partnerId,
  138. 'pay_orderid' => $orderSn
  139. ];
  140. $queryInfo['pay_md5sign'] = strtoupper(md5($this->getSign($queryInfo) . "key=" . $this->publicKey));
  141. $sRes = $this->httpPost($this->queryUrl, $queryInfo);
  142. if (empty($sRes)) {
  143. $this->_lastError = '查询请求异常';
  144. payLogger(__CLASS__,'查询请求异常 $queryInfo='.json_encode($queryInfo));
  145. return '';
  146. }
  147. $aRes = json_decode($sRes, true);
  148. if (empty($aRes)) {
  149. $this->_lastError = '查询解析异常';
  150. payLogger(__CLASS__,'查询解析异常 data='.$sRes);
  151. return '';
  152. }
  153. return $aRes['transaction_id'];
  154. }
  155. }