| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace App\Repositories\Service\Payment;
- class UsdtPayment extends PaymentInterface
- {
- /**
- * 支付请求地址
- *
- * @var string
- */
- private $payUrl = 'http://dtpay.vip58.xyz/api/payment/create_transaction';
- /**
- * 查询请求地址
- *
- * @var string
- */
- private $queryUrl = 'http://dtpay.vip58.xyz/api/payment/query';
- /**
- * 公钥
- *
- * @var string
- */
- private $publicKey = '4dfdea096f5efe0c2fa22bcd88fb59c9';
- const STATUS_CODE = 1;
- /**
- * @var null
- */
- private static $_instance = null;
- /**
- * @return WhiteSharkPayment|null
- */
- public static function getInstance()
- {
- if (is_null(self::$_instance)) {
- self::$_instance = new self();
- }
- return self::$_instance;
- }
- public function createPayLink($data = array())
- {
- $payInfo = [
- 'merchant_code' => $data['partner_id'], //商户号
- 'currency' => 'CNY', //订单货币
- 'coin_code' => $data['pay_attr_code'], //支付币种
- 'amount' => $data['money'], //支付金额
- 'order_id' => $data['order_sn'], //订单号
- 'product_name' => "CJ", //商品名
- 'date' => date('Y-m-d H:i:s'), //支付时间
- 'notify_url' => $data['notify_url'],
- 'redirect_url' => $data['pay_url'],
- ];
- $payInfo['signature'] = strtoupper(md5($this->getSign($payInfo) . "key=" . $this->publicKey));
- $sRes = $this->httpPost($this->payUrl, $payInfo);
- if (empty($sRes)) {
- $this->_lastError = '请求异常';
- payLogger(__CLASS__,'请求异常data=' . $sRes, $payInfo);
- return '';
- }
- $aRes = json_decode($sRes, true);
- if (empty($aRes)) {
- $this->_lastError = '请求异常!';
- payLogger(__CLASS__,'请求异常=' . $sRes, $payInfo);
- return '';
- }
- if ((isset($aRes['code']) && $aRes['code'] != 200) || $aRes['data']['status'] != self::STATUS_CODE) {
- $this->_lastError = '请求异常,请求失败';
- payLogger(__CLASS__,'请求异常,请求失败 data=' . $sRes, $payInfo);
- return '';
- }
- return $aRes['data']['pay_url'];
- }
- public function queryOrder($data, &$orderSn, &$money, $partnerId)
- {
- $queryInfo = [
- 'merchant_code' => $partnerId,
- 'order_id' => $orderSn
- ];
- $queryInfo['signature'] = strtoupper(md5($this->getSign($queryInfo) . "key=" . $this->publicKey));
- $sRes = $this->httpPost($this->queryUrl, $queryInfo);
- if (empty($sRes)) {
- $this->_lastError = '查询请求异常';
- payLogger(__CLASS__,'查询请求异常 $queryInfo='.json_encode($queryInfo),$data);
- }
- $aRes = json_decode($sRes, true);
- if (empty($sRes)) {
- $this->_lastError = '查询解析异常';
- payLogger(__CLASS__,'查询解析异常 data=' . $sRes, $data);
- return '';
- }
- if ($aRes['data']['status'] != self::STATUS_CODE) {
- $this->_lastError = '查询订单状态异常';
- payLogger(__CLASS__,'查询订单状态异常 data=' . $sRes, $data);
- return '';
- }
- if ($money != $aRes['data']['amount']) {
- $this->_lastError = '查询订单金额异常';
- payLogger(__CLASS__,'查询订单金额异常 data='.$sRes,$data);
- return '';
- }
- return true;
- }
- public function notify(&$orderSn, &$tradeNo, &$money, $partnerId)
- {
- $data = request()->all()['data'];
- if ((isset($aRes['code']) && $aRes['code'] != 200) || (isset($aRes['status']) && $aRes['status'] != self::STATUS_CODE)) {
- payLogger(__CLASS__,'回调订单状态失败',$data);
- return false;
- }
- $info = [
- 'merchant_code' => $partnerId,
- 'status' => $data['status'],
- 'order_id' => $data['order_id'],
- 'amount' => $data['amount'],
- 'currency' => $data['currency'],
- 'coin_amount' => $data['coin_amount'],
- 'coin_code' => $data['coin_code'],
- 'merchant_order_sn' => $data['merchant_order_sn'],
- ];
- $sign = strtoupper(md5($this->getSign($info) . "key=" . $this->publicKey));
- if ($sign != $data['signature']) {
- $this->_lastError = '验证失败!';
- logger(json_encode($data) . ':验证失败!');
- return false;
- }
- $money = $data['amount'];
- $res = $this->queryOrder($data, $data['merchant_order_sn'], $money, $partnerId);
- if (empty($res)) {
- return '';
- }
- $orderSn = $data['merchant_order_sn'];
- $tradeNo = $data['order_id'];
- return true;
- }
- public function stopNotify()
- {
- return 'SUCCESS';
- }
- }
|