UsdtPayment.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Repositories\Service\Payment;
  3. class UsdtPayment extends PaymentInterface
  4. {
  5. /**
  6. * 支付请求地址
  7. *
  8. * @var string
  9. */
  10. private $payUrl = 'http://dtpay.vip58.xyz/api/payment/create_transaction';
  11. /**
  12. * 查询请求地址
  13. *
  14. * @var string
  15. */
  16. private $queryUrl = 'http://dtpay.vip58.xyz/api/payment/query';
  17. /**
  18. * 公钥
  19. *
  20. * @var string
  21. */
  22. private $publicKey = '4dfdea096f5efe0c2fa22bcd88fb59c9';
  23. const STATUS_CODE = 1;
  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. 'merchant_code' => $data['partner_id'], //商户号
  42. 'currency' => 'CNY', //订单货币
  43. 'coin_code' => $data['pay_attr_code'], //支付币种
  44. 'amount' => $data['money'], //支付金额
  45. 'order_id' => $data['order_sn'], //订单号
  46. 'product_name' => "CJ", //商品名
  47. 'date' => date('Y-m-d H:i:s'), //支付时间
  48. 'notify_url' => $data['notify_url'],
  49. 'redirect_url' => $data['pay_url'],
  50. ];
  51. $payInfo['signature'] = strtoupper(md5($this->getSign($payInfo) . "key=" . $this->publicKey));
  52. $sRes = $this->httpPost($this->payUrl, $payInfo);
  53. if (empty($sRes)) {
  54. $this->_lastError = '请求异常';
  55. payLogger(__CLASS__,'请求异常data=' . $sRes, $payInfo);
  56. return '';
  57. }
  58. $aRes = json_decode($sRes, true);
  59. if (empty($aRes)) {
  60. $this->_lastError = '请求异常!';
  61. payLogger(__CLASS__,'请求异常=' . $sRes, $payInfo);
  62. return '';
  63. }
  64. if ((isset($aRes['code']) && $aRes['code'] != 200) || $aRes['data']['status'] != self::STATUS_CODE) {
  65. $this->_lastError = '请求异常,请求失败';
  66. payLogger(__CLASS__,'请求异常,请求失败 data=' . $sRes, $payInfo);
  67. return '';
  68. }
  69. return $aRes['data']['pay_url'];
  70. }
  71. public function queryOrder($data, &$orderSn, &$money, $partnerId)
  72. {
  73. $queryInfo = [
  74. 'merchant_code' => $partnerId,
  75. 'order_id' => $orderSn
  76. ];
  77. $queryInfo['signature'] = strtoupper(md5($this->getSign($queryInfo) . "key=" . $this->publicKey));
  78. $sRes = $this->httpPost($this->queryUrl, $queryInfo);
  79. if (empty($sRes)) {
  80. $this->_lastError = '查询请求异常';
  81. payLogger(__CLASS__,'查询请求异常 $queryInfo='.json_encode($queryInfo),$data);
  82. }
  83. $aRes = json_decode($sRes, true);
  84. if (empty($sRes)) {
  85. $this->_lastError = '查询解析异常';
  86. payLogger(__CLASS__,'查询解析异常 data=' . $sRes, $data);
  87. return '';
  88. }
  89. if ($aRes['data']['status'] != self::STATUS_CODE) {
  90. $this->_lastError = '查询订单状态异常';
  91. payLogger(__CLASS__,'查询订单状态异常 data=' . $sRes, $data);
  92. return '';
  93. }
  94. if ($money != $aRes['data']['amount']) {
  95. $this->_lastError = '查询订单金额异常';
  96. payLogger(__CLASS__,'查询订单金额异常 data='.$sRes,$data);
  97. return '';
  98. }
  99. return true;
  100. }
  101. public function notify(&$orderSn, &$tradeNo, &$money, $partnerId)
  102. {
  103. $data = request()->all()['data'];
  104. if ((isset($aRes['code']) && $aRes['code'] != 200) || (isset($aRes['status']) && $aRes['status'] != self::STATUS_CODE)) {
  105. payLogger(__CLASS__,'回调订单状态失败',$data);
  106. return false;
  107. }
  108. $info = [
  109. 'merchant_code' => $partnerId,
  110. 'status' => $data['status'],
  111. 'order_id' => $data['order_id'],
  112. 'amount' => $data['amount'],
  113. 'currency' => $data['currency'],
  114. 'coin_amount' => $data['coin_amount'],
  115. 'coin_code' => $data['coin_code'],
  116. 'merchant_order_sn' => $data['merchant_order_sn'],
  117. ];
  118. $sign = strtoupper(md5($this->getSign($info) . "key=" . $this->publicKey));
  119. if ($sign != $data['signature']) {
  120. $this->_lastError = '验证失败!';
  121. logger(json_encode($data) . ':验证失败!');
  122. return false;
  123. }
  124. $money = $data['amount'];
  125. $res = $this->queryOrder($data, $data['merchant_order_sn'], $money, $partnerId);
  126. if (empty($res)) {
  127. return '';
  128. }
  129. $orderSn = $data['merchant_order_sn'];
  130. $tradeNo = $data['order_id'];
  131. return true;
  132. }
  133. public function stopNotify()
  134. {
  135. return 'SUCCESS';
  136. }
  137. }