天天看點

解決apiclode alipay子產品 app支付寶支付問題

app 支付寶支付流程:

先去螞蟻支付 下載下傳支付寶php  demo:  https://docs.open.alipay.com/54/106370/

apiclode支付 alipay子產品位址:https://docs.apicloud.com/Client-API/Open-SDK/aliPay

支付時,主要是以app配置要完成好,app支付完成後,跳回到原app需要apiclode中的app架構殼子,具體看文檔。

**************************************************************調用支付寶請求方法**********************************************

require "AopSdk.php";

require "aop/AopClient.php";

require 'aop/request/AlipayTradeAppPayRequest.php';

// 開啟錯誤提示

// ini_set("display_errors","On");

// error_reporting(E_ALL);

//初始化配置

******調用授權接口alipay.wap.trade.create.direct擷取授權碼token**************************

//伺服器異步通知頁面路徑

$notify_url = "http://".$_SERVER['HTTP_HOST']."/mobile/apiclode/ajax_url.php";

//需http://格式的完整路徑,不允許加?id=123這類自定義參數

//商戶訂單号

$out_trade_no = $_GET['out_trade_no'];

//商戶網站訂單系統中唯一訂單号,必填\

$order_sn = isset($_GET['order_sn']) ? $_GET['order_sn'] : '';

//訂單名稱

$subject = "訂單号:".$order_sn;

//必填

//付款金額

$total_fee = $_GET['total_fee'];

$c = new AopClient;

$c->gatewayUrl = "https://openapi.alipay.com/gateway.do";

// 申請的appid

$c->appId = xxxxxxxxx;

// 螞蟻平台申請的私鑰

$c->rsaPrivateKey =  私鑰 ;

$c->format = "json";

$c->charset= "utf-8";

$c->signType= "RSA2";

// 螞蟻平台申請的公鑰

$c->alipayrsaPublicKey = 公鑰;

//執行個體化具體API對應的request類,類名稱和接口名稱對應,目前調用接口名稱:alipay.trade.app.pay

$request = new AlipayTradeAppPayRequest();

$body = 'xxx';

$expire = '30';

$pre_price = "0.01";

//SDK已經封裝掉了公共參數,這裡隻需要傳入業務參數

$bizcontent = "{\"body\":\"{$body}\"," //支付商品描述

  . "\"subject\":\"{$subject}\"," //支付商品的标題

  . "\"out_trade_no\":\"{$out_trade_no}\"," //商戶網站唯一訂單号

  . "\"timeout_express\":\"{$expire}m\"," //該筆訂單允許的最晚付款時間,逾期将關閉交易

  . "\"total_amount\":\"{$pre_price}\"," //訂單總金額,機關為元,精确到小數點後兩位,取值範圍[0.01,100000000]

  . "\"product_code\":\"QUICK_MSECURITY_PAY\""

  . "}";

$request->setNotifyUrl($notify_url);

$request->setBizContent($bizcontent);

//這裡和普通的接口調用不同,使用的是sdkExecute

$response   = $c->sdkExecute($request);

$smarty->assign('order_info', $response);

//必填

$smarty->display('aliapi_index.html');

********************************************************以上是你調用支付寶請求方法**********************************************

*******************************************************調用支付寶支付界面 前端頁面start**********************************************

aliapi_index.html  隻寫body

<body style='background:#EDEDED;'>

    <div id="wrap">

        <div id="main" class='center'>

            <img id='loading' src='../../images/money.png' width=100 height=100>

        </div> 

        <p class="paying">正在支付...</p>

    </div>

</body>

<script type="text/javascript" src="../../apiclode/widget/script/api.js"></script>

<script type="text/javascript">

    var orderInfo = "{$order_info}";

    apiready = function() {

        var aliPay = api.require('aliPay');

        aliPay.payOrder({

            orderInfo: orderInfo

        }, function(ret, err) {

            var url = window.location.host;

            window.location.href='http://'+url+'/mobile/apiclode/result_url.php?result='+ret.code;

        });

    };

</script>

*******************************************************調用支付寶支付界面 前端頁面end**********************************************

*******************************************************回調函數使用,處理背景訂單狀态**********************************************

ajax_url.php

//---------------------支付寶回調-------------------

require "AopSdk.php";

require "aop/AopClient.php";

require('../includes/lib_payment.php');

//驗證簽名

$aop = new AopClient();

$aop->alipayrsaPublicKey = 公鑰;

// 支付寶傳回的fund_bill_list 被轉義,此時轉義回來

$fund_bill_list = stripslashes($_POST['fund_bill_list']);

$_POST['fund_bill_list'] = $fund_bill_list;

$flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");

$result = $flag ? 'true' : "false";

file_put_contents("./error-log.txt", date("Y-m-d H:i:s") . "-----" . $result ."\r\n" , FILE_APPEND);

if ($flag) {

//驗簽

 //處理業務,并從$_POST中提取需要的參數内容

 if($_POST['trade_status'] == 'TRADE_SUCCESS' || $_POST['trade_status'] == 'TRADE_FINISHED'){//處理交易完成或者支付成功的通知

    //擷取訂單号

    $out_trade_no     = $_POST['out_trade_no'];

    //交易号

    $trade_no         = $_POST['trade_no'];

    //訂單支付時間

    $gmt_payment     = $_POST['gmt_payment'];

    //轉換為時間戳

    $gtime = strtotime($gmt_payment);

    //此處編寫回調處理邏輯

    xxxxxxxxxx

    //處理成功一定要傳回 success 這7個字元組成的字元串,

    die('success');//響應success表示業務處理成功,告知支付寶無需在異步通知

 }

}

支付寶支付有個問題,需要注意,那就是 異步回調函數中,傳回的 資訊[{\"amount\":\"0.01\",\"fundChannel\":\"ALIPAYACCOUNT\"}]   注意,此處有 反斜杠,支付寶傳回的本身就有反斜杠,這時候确認2點:1、是否環境内配置了專一,2、如果沒有,你要對支付寶傳回的 字段進行轉義   $fund_bill_list = stripslashes($_POST['fund_bill_list']);

這樣就好了。

繼續閱讀