XindaPayment.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Repositories\Service\Payment;
  3. use App\Repositories\Service\CommonService;
  4. class XindaPayment extends PaymentInterface
  5. {
  6. /**
  7. * 支付请求地址
  8. *
  9. * @var string
  10. */
  11. private $payUrl = 'http://xd.88xdxd88.com/pay';
  12. const SUCCESS_STATUS = 1;
  13. /**
  14. * 公钥
  15. *
  16. * @var string
  17. */
  18. private $publicKey = 'XckeAtPpBFcQnYxmxRyapeSTGewiZmJY';
  19. /**
  20. * @var null
  21. */
  22. private static $_instance = null;
  23. /**
  24. * @return WhiteSharkPayment|null
  25. */
  26. public static function getInstance()
  27. {
  28. if (is_null(self::$_instance)) {
  29. self::$_instance = new self();
  30. }
  31. return self::$_instance;
  32. }
  33. public function createPayLink($data = array())
  34. {
  35. $payInfo = [
  36. 'fxid' => $data['partner_id'], //商户号
  37. 'fxddh' => $data['order_sn'],
  38. 'fxdesc' => 'CJ',
  39. 'fxattch' => 'CJ',
  40. 'fxfee' => sprintf('%.2f', $data['money']),
  41. 'fxnotifyurl' => $data['notify_url'],
  42. 'fxbackurl' => $data['pay_url'],
  43. 'fxpay' => $data['pay_attr_code'],
  44. ];
  45. $payInfo['fxip'] = '127.0.0.1';
  46. $payInfo['fxsign'] = md5($payInfo['fxid'] . $payInfo['fxddh'] . $payInfo['fxfee'] . $payInfo['fxnotifyurl'] . $this->publicKey);
  47. $sRes = $this->httpPost($this->payUrl, $payInfo);
  48. if (empty($sRes)) {
  49. $this->_lastError = '请求异常';
  50. payLogger(__CLASS__, '请求异常data=' . $sRes, $payInfo);
  51. return '';
  52. }
  53. $aRes = json_decode($sRes, true);
  54. if (empty($aRes)) {
  55. $this->_lastError = '请求异常!';
  56. payLogger(__CLASS__, '请求异常 (解析) data=' . $sRes, $payInfo);
  57. return '';
  58. }
  59. if ($aRes['status'] != self::SUCCESS_STATUS) {
  60. $this->_lastError = '请求异常,请求失败';
  61. payLogger(__CLASS__, '请求异常,请求失败 data=' . $sRes, $payInfo);
  62. return '';
  63. }
  64. return $aRes['payurl'];
  65. }
  66. public function queryOrder($data, &$orderSn, &$money, $partnerId)
  67. {
  68. $queryInfo = [
  69. 'fxid' => $partnerId,
  70. 'fxddh' => $orderSn,
  71. 'fxaction' => 'orderquery',
  72. ];
  73. $queryInfo['fxsign'] = md5($partnerId . $orderSn . 'orderquery' . $this->publicKey);
  74. $sRes = $this->httpPost($this->payUrl, $queryInfo);
  75. if (empty($sRes)) {
  76. $this->_lastError = '查询请求异常';
  77. payLogger(__CLASS__, '查询请求异常 data=' . $sRes, $queryInfo);
  78. return '';
  79. }
  80. $aRes = json_decode($sRes, true);
  81. if (empty($aRes)) {
  82. $this->_lastError = '查询解析异常';
  83. payLogger(__CLASS__, '查询解析异常 data=' . $sRes, $queryInfo);
  84. return '';
  85. }
  86. if ($aRes['fxstatus'] != self::SUCCESS_STATUS) {
  87. $this->_lastError = '查询订单状态异常';
  88. payLogger(__CLASS__, '查询订单状态异常 data=' . $sRes, $queryInfo);
  89. return '';
  90. }
  91. if ($money != $aRes['fxfee']) {
  92. $this->_lastError = '查询订单金额异常';
  93. payLogger(__CLASS__, '查询订单金额异常 data=' . $sRes, $queryInfo);
  94. return '';
  95. }
  96. return true;
  97. }
  98. public function notify(&$orderSn, &$tradeNo, &$money, $partnerId)
  99. {
  100. $data = request()->all();
  101. if ($data['fxstatus'] != self::SUCCESS_STATUS) {
  102. payLogger(__CLASS__, '回调订单状态失败', $data);
  103. return false;
  104. }
  105. $sign = md5($data['fxstatus'] . $partnerId . $data['fxddh'] . $data['fxfee'] . $this->publicKey);
  106. if ($sign != $data['fxsign']) {
  107. $this->_lastError = '验证失败!';
  108. payLogger(__CLASS__, '验证失败 key=' . $this->publicKey, $data);
  109. return false;
  110. }
  111. $money = $data['fxfee'];
  112. $res = $this->queryOrder($data, $data['fxddh'], $money, $partnerId);
  113. if (empty($res)) {
  114. return '';
  115. }
  116. $orderSn = $data['fxddh'];
  117. $tradeNo = $data['fxorder'];
  118. return true;
  119. }
  120. public function stopNotify()
  121. {
  122. return 'success';
  123. }
  124. }