PHP支付宝支付,支付宝回調


公式SDKをダウンロード:https://openhome.alipay.com/doc/sdkDownload.resource?sdkType=PHP支付宝が作成したアプリに「パソコンサイト決済、携帯サイト決済、アプリ決済、支付宝口座への振り替え」機能vueを追加すると参考になります.https://blog.csdn.net/qq_33026699/article/details/106069905
一、パソコンのウェブサイトの支払い
public function pcpay($order_id){
    $m_order = Db::name("order");
    $order_map['id'] = $order_id;
    $order_data = $m_order->where($order_map)->find();
    
    Vendor('sdk.aop.AopClient');
    Vendor('sdk.aop.request.AlipayTradePagePayRequest');

    $aop = new \AopClient ();
    $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
    $aop->appId = '******';    #appid
    $aop->rsaPrivateKey = '******';     #     
    $aop->alipayrsaPublicKey='******';   #     
    $aop->apiVersion = '1.0';
    $aop->postCharset='UTF-8';
    $aop->format='json';
    $aop->signType='RSA2';
    $request = new \AlipayTradePagePayRequest ();
    $www_url = $_SERVER['HTTP_HOST'];
	
	$data['body']='******';  #  
    $data['subject']='******';   #   
    $data['out_trade_no']=$order_data['order'];     #   
    $data['timeout_express']='30m';
    $data['total_amount']=$order_data['money'];    #  
    $data['product_code']='FAST_INSTANT_TRADE_PAY';
    #$data['qr_pay_mode']='3';   #PC       ,           。 
    $bizcontent = json_encode($data);
    $request->setNotifyUrl("http://".$www_url.".******");    #      
    $request->setReturnUrl("http://".$www_url);   #      ,       ,                  ,       ,       
    $request->setBizContent($bizcontent);
    $result = $aop->pageExecute ( $request); 
    #$this->success("    ",$result);
    echo $result;
}

注目すべきはqr_pay_modeプロパティは、フロントエンドでiframeラベルを使用してアリペイが返すコードをカバーし、自分の支払いページのスタイルをカスタマイズできます.
二、携帯サイトの支払い
	public function webapp($order_id){
    $m_order = Db::name("order");
    $order_map['id'] = $order_id;
    $order_data = $m_order->where($order_map)->find();
    
    Vendor('sdk.aop.AopClient');
    Vendor('sdk.aop.request.AlipayTradeWapPayRequest');

    $aop = new \AopClient ();
    $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
    $aop->appId = '******';   #appid
    $aop->rsaPrivateKey = '******';    #     
    $aop->alipayrsaPublicKey='******';    #     
    $aop->apiVersion = '1.0';
    $aop->postCharset='UTF-8';
    $aop->format='json';
    $aop->signType='RSA2';
    $request = new \AlipayTradeWapPayRequest ();
    $www_url = $_SERVER['HTTP_HOST'];
    
	 $data['body']='******';   #  
    $data['subject']='******';   #   
    $data['out_trade_no']=$order_data['order'];     #   
    $data['timeout_express']='30m';
    $data['total_amount']=$order_data['money'];     #  
    $data['product_code']='QUICK_WAP_WAY';
    $bizcontent = json_encode($data);
    $request->setNotifyUrl("http://".$www_url."*****");    #      
    $request->setReturnUrl("http://".$www_url);   #        
    $request->setBizContent($bizcontent);
    $result = $aop->pageExecute ( $request); 
    #$this->success("    ",$result);
    echo $result;
}

三、app支払い
public function aoppay($order_id){
    Vendor('sdk.aop.AopClient');
    Vendor('sdk.aop.request.AlipayTradeAppPayRequest');
    $aop = new \AopClient();

    $m_order = Db::name("order");
    $order_map['id'] = $order_id;
    $order_data = $m_order->where($order_map)->find();

    $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
    $aop->appId = '******';   #appid
    $aop->rsaPrivateKey = '******';//     
    $aop->format = "json";
    $aop->charset = "UTF-8";
    $aop->signType = "RSA2";
    $aop->alipayrsaPublicKey = '******';//     
    //     API   request ,          ,        :alipay.trade.app.pay
    //$this->ajaxReturn($aop);
    $request = new \AlipayTradeAppPayRequest();
    //SDK          ,           
    $data['body']='******';   #  
    $data['subject']='******';    #   
    $data['out_trade_no']=$order_data['order'];    #   
    $data['timeout_express']='30m';
    $data['total_amount']=$order_data['money'];    #  
    $data['product_code']='QUICK_MSECURITY_PAY';
    $bizcontent = json_encode($data);
    $www_url = $_SERVER['HTTP_HOST'];
    //$this->ajaxReturn($bizcontent);
    $request->setNotifyUrl("http://".$www_url."****");   #    
    $request->setBizContent($bizcontent);
//            ,    sdkExecute
    $response = $aop->sdkExecute($request);

//htmlspecialchars                    html  ,         http         
//        echo htmlspecialchars($response);//  orderString           ,      。
//         $res= htmlspecialchars($response);
     //$res= htmlspecialchars_decode($response);
    
     $this->success("    ",$response);
}

四、支付宝の振り替え
    Vendor('sdk.aop.AopClient');
    Vendor('sdk.aop.request.AlipayFundTransToaccountTransferRequest');
    $order = date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);   //   
    $aop = new \AopClient ();
    $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
    $aop->appId = '     appid';
    $aop->rsaPrivateKey = '     ';

    //              ,                 ,                     。          。
    //$aop->alipayrsaPublicKey='        ,     ';

    $aop->apiVersion = '1.0';
    $aop->signType = 'RSA2';
    $aop->postCharset='UTF-8';
    $aop->format='json';
    $request = new \AlipayFundTransToaccountTransferRequest ();
	
	$data['out_biz_no']=$order;     #   
    $data['payee_type']='ALIPAY_LOGONID';
    $data['payee_account']='******';    #        
    $data['payee_real_name']='******';   #       
    $data['remark']='******';   #  
    $data['amount']='******';   #  ,  0.1
    $request->setBizContent($data);
     
     $result = $aop->execute ($request);
    $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
    $resultCode = $result->$responseNode->code;
    #$msg = $result->$responseNode->msg;
    if(!empty($resultCode)&&$resultCode == 10000){
        echo "    !";
    } else {
        echo "    !";
    }

支払成功コールバック
	header("Content-Type: text/html;charset=utf-8");
    $request = Request::instance();
    
    Vendor('sdk.AopSdk');
    Vendor('sdk.aop.AopClient');

    $aop = new \AopClient;
    $aop->alipayrsaPublicKey = '******************';  #     ,    ,        ,      
    $flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");
    /*
    #           ,                
    if($request->IsPost()){
        $str = json_encode($_POST);
        $fp = fopen("./pay.txt",'w');
        fwrite($fp,$str);
        
        $flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");
        $fp = fopen("./pay1.txt",'w');
        fwrite($fp,$flag);
    }else{
        $filename = "./pay.txt";
        $handle = fopen($filename, "r");
        $contents = fread($handle, filesize ($filename));
        $json_array = json_decode($contents,TRUE);
        $flag = $aop->rsaCheckV1($json_array, NULL, "RSA2");
        dump($json_array);
        dump($flag);
        die;
    }*/
    
    #            ,             。
    /**
     *        trade_status        ,  out_trade_no     
     *      ,         
     **/
    if (!empty($flag)) {
        if ($_POST['trade_status'] == 'TRADE_SUCCESS' || $_POST['trade_status'] == 'TRADE_FINISHED') {    //               

            $data['out_trade_no'] = $_POST['out_trade_no'];
            #$data['out_trade_no'] = '2020042955504899';

            $m_order = Db::name('order');
            
            #          ,        ,          ,           
            $order_map['order'] = $data['out_trade_no'];
            $order_map['status'] = 2;
            $order_data = $m_order->where($order_map)->find();
            if(empty($order_data)){
                echo 'success';exit();
            }

            #      ,         
           
        }
        //  success,     。          。    success,               。
        echo 'success';
    }
    echo 'success';