88 lines
2.6 KiB
PHP
88 lines
2.6 KiB
PHP
<?php
|
||
/**
|
||
* Created by PhpStorm.
|
||
* User: kongqi
|
||
* Date: 2019/4/21
|
||
* Time: 6:13
|
||
*/
|
||
|
||
namespace App\ServicePay\Usdtpay;
|
||
|
||
class Usdtpay
|
||
{
|
||
protected $token;//token
|
||
protected $mch_id;//商户号
|
||
protected $config;
|
||
static public $instance;//声明一个静态变量(保存在类中唯一的一个实例)
|
||
|
||
private function __construct($config)
|
||
{
|
||
$this->token = $config['token'];
|
||
$this->mch_id = $config['mch_id'];
|
||
$this->config = $config;
|
||
|
||
}
|
||
|
||
public static function make($config)
|
||
{
|
||
if (!self::$instance) self::$instance = new self($config);
|
||
return self::$instance;
|
||
}
|
||
|
||
public function makeSign($request, $salt = '')
|
||
{
|
||
$signStr = $request['pickupUrl'];
|
||
$signStr .= $request['receiveUrl'];
|
||
$signStr .= $request['signType'];
|
||
$signStr .= $request['orderNo'];
|
||
$signStr .= $request['orderAmount'];
|
||
$signStr .= $request['orderCurrency'];
|
||
$signStr .= $request['customerId'];
|
||
$signStr .= $salt;
|
||
|
||
return strtolower(md5($signStr));
|
||
}
|
||
|
||
public function notifySign($request, $salt = '')
|
||
{
|
||
$signStr = $request['signType'];
|
||
$signStr .= $request['orderNo'];
|
||
$signStr .= $request['orderAmount'];
|
||
$signStr .= $request['orderCurrency'];
|
||
$signStr .= $request['transactionId'];
|
||
$signStr .= $request['status'];
|
||
$signStr .= $request['token'];
|
||
$signStr .= $salt;
|
||
return md5($signStr);
|
||
}
|
||
|
||
public function orderBuy($pay_data)
|
||
{
|
||
$request = [
|
||
'orderNo' => $pay_data['orderNo'], //商户订单号,最大长度60个字符
|
||
'customerId' => $pay_data['customerId'], //客户交易者账号
|
||
'orderCurrency' => $pay_data['orderCurrency'], //订单币种,固定值:USDT或CNY
|
||
'orderAmount' => $pay_data['orderAmount'], //订单金额,USDT单位为个,CNY单位为元
|
||
'receiveUrl' => $this->config['notify_url'], // 通知回调地址
|
||
'pickupUrl' => $this->config['return_url'] . '?orderNo=' . $pay_data['orderNo'], // 交易完成后跳转URL
|
||
'signType' => 'MD5', //固定值不要改
|
||
];
|
||
|
||
$sign = $this->makeSign($request, $this->token); //签名
|
||
|
||
$request['sign'] = $sign;
|
||
$url = $this->config['url'] . '?' . http_build_query($request); //访问链接
|
||
|
||
return ['redirect_url' => $url];
|
||
}
|
||
|
||
public function checkSign($para)
|
||
{
|
||
$para['token'] = $this->token;
|
||
$sign = $this->notifySign($para);
|
||
if ($sign === $para['sign']) {
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
} |