MingsenPayment.php 5.1 KB

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