FurongPayment.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace App\Repositories\Service\Payment;
  3. class FurongPayment extends PaymentInterface
  4. {
  5. /**
  6. * 支付请求地址
  7. *
  8. * @var string
  9. */
  10. private $payUrl = 'http://www.zhangyii.cn/api/mch/unifiedorder';
  11. /**
  12. * 查询请求地址
  13. *
  14. * @var string
  15. */
  16. private $queryUrl = 'http://www.zhangyii.cn/api/mch/query';
  17. const SUCESS_CODE = 0;
  18. const STATUS_CODE = 2;
  19. /**
  20. * @var null
  21. */
  22. private static $_instance = null;
  23. /**
  24. * 公钥
  25. *
  26. * @var string
  27. */
  28. private $publicKey = '4d232d3fd169419396aca3c0c5d4ed2c';
  29. /**
  30. * @return WhiteSharkPayment|null
  31. */
  32. public static function getInstance()
  33. {
  34. if (is_null(self::$_instance)) {
  35. self::$_instance = new self();
  36. }
  37. return self::$_instance;
  38. }
  39. public function createPayLink($data = array())
  40. {
  41. $payInfo = [
  42. 'mch_id' => $data['partner_id'],
  43. 'pass_code' => $data['pay_attr_code'],
  44. 'subject' => 'PAY',
  45. 'out_trade_no' => $data['order_sn'],
  46. 'money' => $data['money'],
  47. 'client_ip' => '127.0.0.1',
  48. 'notify_url' => $data['notify_url'],
  49. 'return_url' => $data['pay_url'],
  50. 'timestamp' => date('Y-m-d H:i:s'),
  51. ];
  52. $payInfo['sign'] = strtoupper(md5($this->getSign($payInfo) . 'key=' . $this->publicKey));
  53. $sRes = $this->httpPost($this->payUrl, $payInfo, 2);
  54. if (empty($sRes)) {
  55. $this->_lastError = '请求异常';
  56. payLogger(__CLASS__, '请求异常data='.$sRes, $payInfo);
  57. return '';
  58. }
  59. $aRes = json_decode($sRes, true);
  60. if (empty($aRes)) {
  61. $this->_lastError = '请求异常!';
  62. payLogger(__CLASS__, '请求异常 (解析) data='.$sRes, $payInfo);
  63. return '';
  64. }
  65. if ($aRes['code'] != self::SUCESS_CODE) {
  66. $this->_lastError = '请求异常,请求失败';
  67. payLogger(__CLASS__, '请求异常,请求失败 data='.$sRes, $payInfo);
  68. return '';
  69. }
  70. return $aRes['data']['pay_url'];
  71. }
  72. public function queryOrder($data, &$orderSn, &$money, $partnerId)
  73. {
  74. $queryInfo = [
  75. 'mch_id' => $partnerId,
  76. 'out_trade_no' => $orderSn,
  77. 'timestamp' => date('Y-m-d H:i:s'),
  78. ];
  79. $queryInfo['sign'] = strtoupper(md5($this->getSign($queryInfo) . 'key=' . $this->publicKey));
  80. $sRes = $this->httpPost($this->queryUrl, $queryInfo, 2);
  81. if (empty($sRes)) {
  82. $this->_lastError = '查询请求异常';
  83. payLogger(__CLASS__, '查询请求异常 $queryInfo='.json_encode($queryInfo), $data);
  84. return '';
  85. }
  86. $aRes = json_decode($sRes, true);
  87. if (empty($sRes)) {
  88. $this->_lastError = '查询解析异常';
  89. payLogger(__CLASS__, '查询解析异常 data='.$sRes, $data);
  90. return '';
  91. }
  92. if ($aRes['code'] != self::SUCESS_CODE) {
  93. $this->_lastError = '查询订单状态异常';
  94. payLogger(__CLASS__, '查询订单状态异常'.json_encode($data));
  95. return '';
  96. }
  97. if ($money != $aRes['data']['money']) {
  98. $this->_lastError = '查询订单金额异常';
  99. payLogger(__CLASS__, '查询订单金额异常'.json_encode($data));
  100. return '';
  101. }
  102. return true;
  103. }
  104. public function notify(&$orderSn, &$tradeNo, &$money, $partnerId)
  105. {
  106. $data = request()->all();
  107. if ($data['state'] != self::STATUS_CODE) {
  108. payLogger(__CLASS__, '回调订单状态失败!'.json_encode($data));
  109. return false;
  110. }
  111. $info = [
  112. 'mch_id' => $partnerId,
  113. 'trade_no' => $data['trade_no'],
  114. 'out_trade_no' => $data['out_trade_no'],
  115. 'psg_trade_no' => $data['psg_trade_no'],
  116. 'money' => $data['money'],
  117. 'notify_time' => $data['notify_time'],
  118. 'subject' => $data['subject'],
  119. 'state' => $data['state']
  120. ];
  121. $sign = strtoupper(md5($this->getSign($info) . 'key=' . $this->publicKey));
  122. if ($sign != $data['sign']) {
  123. $this->_lastError = '验证失败!';
  124. payLogger(json_encode($data) . ':验证失败!');
  125. return false;
  126. }
  127. $authMoney = $data['money'];
  128. $res = $this->queryOrder($data, $data['out_trade_no'], $authMoney, $partnerId);
  129. if (empty($res)) {
  130. return '';
  131. }
  132. $money = $data['money'];
  133. $orderSn = $data['out_trade_no'];
  134. $tradeNo = $data['trade_no'];
  135. return true;
  136. }
  137. public function stopNotify()
  138. {
  139. return 'SUCCESS';
  140. }
  141. }