USDT信用卡和转账入账接口

class Usdtpay{

    public function index(){
		$data = $this->request->param();
        $uid= $data['uid'] ?? '';
        $token= $data['token'] ?? '';
        $type= $data['type'] ?? '';
        $changeid= $data['changeid'] ?? '';
        $coin= $data['coin'] ?? '';
        $money= $data['money'] ?? '';

        if( !$uid || !$token || !$type || !$changeid || !$coin || !$money || checkToken($uid,$token)==700 ){
			$reason='您的登陆状态失效,请重新登陆!';
			$this->assign('reason', $reason);
			return $this->fetch(':error');
		}

		$charge = DB::name("usdt_charge_rules")->where(['id'=>$changeid])->find();

		if(!$charge || $charge['money']!=$money || ($charge['coin']!=$coin  && $charge['coin_ios']!=$coin && $charge['coin_paypal']!=$coin)){

			$reason='订单信息有误,请重新提交';
			$this->assign('reason', $reason);
			return $this->fetch(':error');
		}

		$orderid=$uid.'_'.date('YmdHis').rand(100,999);

		$orderinfo=array(
			"uid"=>$uid,
			"touid"=>$uid,
			"money"=>$money,
			"coin"=>$coin,
			"orderno"=>$orderid,
			"type"=>8,
			"status"=>0,
			"addtime"=>time()
		);

		$orderinfo['coin_give']=$charge['give'];
		$orderinfo['currency_type']='美元';

		$result= Db::name("usdt_charge_user")->insert($orderinfo);

		if(!$result){
			$reason='订单提交失败';
			$this->assign('reason', $reason);
			return $this->fetch(':error');
		}

		if($type=='card'){
			//信用卡
			$product="RMQuickPay";
		}else if($type=='transfer'){
			//转账
			$product="USDT-TRC20Deposit";
		}
		
		
		$params=array(
			'merchant_ref'=>$orderid,
			'product'=>$product,
			'amount'=>$money
		);

		$params_json = json_encode($params,320);

		$usdt_merchant_no=''; //商户号
		$merchant_key=''; //密钥

		/*var_dump($configpri);
		die;*/

		if(!$usdt_merchant_no){
			$reason='商户号未配置';
			$this->assign('reason', $reason);
			return $this->fetch(':error');
		}

		$data=array(
			'merchant_no'=>$usdt_merchant_no,
			'timestamp'=>time(),
			'sign_type'=>'MD5',
			'params'=>$params_json,

		);

		$data['sign'] = $this->get_sign($data, $merchant_key);

		$payurl="https://api.stars-pay.com/api/gateway/pay";

		$response = $this->http($payurl, 'POST', $data);//post参数提交,获取数据处理 Post parameter submission, get data processing
        $result = json_decode($response, true);//转换为数组 Convert to array

        $code = isset($result['code']) ? $result['code'] : 404;
        $message = isset($result['message']) ? $result['message'] : 'errorMsg:'.(string)$response;


        if ($code == 200) {
            //下单成功,跳转支付链接 The order is successfully placed, jump to the payment link
            $params = json_decode($result['params'],true);
            $payurl = isset($params['payurl']) ? $params['payurl'] : '';//支付链接 Payment link
            header("Location:{$payurl}");//跳转支付链接 Jump payment link
            exit();

            //使用二维码 Use QR code
//            include 'phpqrcode/phpqrcode.php';
//            $qrcode = isset($params['qrcode']) ? $params['qrcode'] : '';
//            QRcode::png($qrcode, 'qrimg.png');
//            $html = '<img src="qrimg.png" style="width:200px;height:200px;"><br/><p>扫码支付</p>';//Scan code to pay
//            exit($html);

        } else {
            //exit('下单失败,返回错误信息:'.$message);//The order fails and an error message is returned
            $reason='下单失败,返回错误信息:'.$message;
			$this->assign('reason', $reason);
			return $this->fetch(':error');
        }



	}


	public function get_sign($data = array(), $key = '') {
        //组装签名字段 签名 MD5(merchant_no+params+sign_type+timestamp+Key)-说明key 是商户秘钥
        //Assemble the signature field Signature MD5 (merchant_no+params+sign_type+timestamp+Key)-indicating that the key is the merchant secret key
        $merchant_no = isset($data['merchant_no']) ? $data['merchant_no'] : '';
        $params = isset($data['params']) ? $data['params'] : '';
        $sign_type = isset($data['sign_type']) ? $data['sign_type'] : '';
        $timestamp = isset($data['timestamp']) ? $data['timestamp'] : '';

        $sign_str = $merchant_no . $params . $sign_type . $timestamp . $key;
        $sign = md5($sign_str);//MD5签名 不区分大小写  MD5 signature is not case sensitive
        return $sign;
    }


    //post参数提交,可根据自己情况修改换成别的函数 Post parameter submission, you can modify and replace it with other functions according to your own situation
    public function http($url = '', $method = 'POST', $postData = array(), $header = array()) {
        $data = '';
        if (!empty($url)) {
            try {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_HEADER, false);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_TIMEOUT, 30); //30秒超时
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书  
				curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名

                if ($header) {
                    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
                }

                if (strtoupper($method) == 'POST') {
                    $curlPost = is_array($postData) ? http_build_query($postData) : $postData;
                    curl_setopt($ch, CURLOPT_POST, 1);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
                }
                $data = curl_exec($ch);
                curl_close($ch);
            } catch (Exception $e) {
                $data = '';
            }
        }
        return $data;
    }

    //异步回调
    public function notify_usdt() {
        //params的参数
        //merchant_ref	是	string	商户订单号
        //system_ref	是	string	平台订单号
        //amount	是	string	金额
        //pay_amount	是	string	实际支付金额
        //fee	是	string	手续费
        //status	是	integer	订单状态:0:Unpaid;1:Paid
        //success_time	是	integer	成功支付时间(时间戳)

		$merchant_key=''; //密钥

        $request = $_POST; //POST参数 POST parameters
        $this->logusdt("data:".json_encode($request));
        $mysign = $this->get_sign($request, $merchant_key); //生成签名 Generate signature

        $params = isset($request['params']) ? json_decode($request['params'],true) : array();
        $sign = isset($request['sign']) ? $request['sign'] : '';

        if ($sign == $mysign) { //验签 Verify signature
            //订单状态,0:待支付,1:支付成功 Order status, 0: Unpaid, 1: paid
            $status = isset($params['status']) ? (int)$params['status'] : 0;
            if ($status == 1) {
                //处理订单成功的逻辑 The logic of processing the order successfully
                
                $where=[
                	'orderno'=>$params['merchant_ref'],
                	'type'=>8
                ];

                $this->logusdt("where:".json_encode($where));

            	$orderinfo = Db::name("usdt_charge_user")->where($where)->find();

            	if(!$orderinfo){
            		$text = "FAIL";
            		exit($text);
            		return;
            	}

            	if($orderinfo['status'] !=0){
            		$text = "SUCCESS";
            		exit($text);
            		return;
            	}

            	$usdt=$orderinfo['coin']+$orderinfo['coin_give'];

            	Db::name("user")->where(['id'=>$orderinfo['touid']])->inc("usdt",$usdt)->update();

            	$data=[
            		'status'=>1,
            		'trade_no'=>$params['system_ref']
            	];

            	$this->logusdt("update:".json_encode($data));

            	Db::name("usdt_charge_user")->where(['id'=>$orderinfo['id']])->update();

                $text = "SUCCESS"; //处理成功,返回的标识 The processing is successful, the returned ID
            } else {
                $text = "FAIL"; //处理失败 Processing failed
            }
        }  else {
            $text = "SIGN_ERROR"; //签名失败 Signature failed
        }
        exit($text);
    }


    public function return_url() {
        //params的参数
        //merchant_ref	是	string	商户订单号
        //system_ref	是	string	平台订单号
        //amount	是	string	金额
        //fee	是	string	手续费
        //status	是	integer	入金订单状态:0:Unpaid;1:Paid
        //success_time	是	integer	成功支付时间(时间戳)

    	$configpri=getConfigPri();
		$merchant_key=$configpri['usdt_merchant_key'];

        $request = $_GET;//GET parameters
        $mysign = $this->get_sign($request, $merchant_key); //生成签名 Generate signature

        $params = isset($request['params']) ? json_decode($request['params'],true) : array();
        $sign = isset($request['sign']) ? $request['sign'] : '';

        if ($sign != $mysign) { //验签 Verify signature
            //订单状态,0:待支付,1:支付成功 Order status, 0: Unpaid, 1: paid
            $status = isset($params['status']) ? (int)$params['status'] : 0;
            if ($status == 1) {
                //展示成功页面 Show success page
                $text = 'pay success';
            } else {
                //展示处理失败页面 Display the failed page
                $text = 'pay fail';
            }
        }  else {
            //展示错误信息页面 签名失败 Display error message page Signing failed
            $text = 'sign_error';
        }
        exit($text);
    }

    public function logusdt($msg){
		file_put_contents(CMF_ROOT.'log/think/appapi/usdtpay/logusdt_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').'  msg:'.$msg."\r\n",FILE_APPEND);
	}

}

猜你喜欢

转载自blog.csdn.net/salestina/article/details/128429231