這篇文章主要介紹PHP如何接入支付寶即時到賬功能,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

首先請到支付寶那邊申請一個及時到賬的接口賬戶,需要提交相關(guān)材料申請。然后根據(jù)即時到賬的API文檔進行接入。API文檔提供了各種語言版本的demo,我這里是下的php版demo,然后再進行相關(guān)修改操作。你也可以將demo版本的代碼重新整合,我這里暫時為了走通充值及時到賬流程,就不進行代碼重構(gòu)了。
API文檔:/tupian/20230522/doc.htm><form action="/pay/alipayapi.php" class="tm-panel-small uk-form uk-form-stacked" method="post" target="_blank"> <div class="element" > </div> <div class="uk-form-row"> <div class="uk-form-controls"> <label for="form-username" class="uk-form-label" >訂單序號:</label> <input class="uk-form-width-large" type="text" name="WIDout_trade_no" id="out_trade_no" value="<?php echo trim($_GET['orderid']);?>" > <br> </div> </div> <div class="uk-form-row"> <div class="uk-form-controls"> <label for="form-username" class="uk-form-label" >商品名稱:</label> <input class="uk-form-width-large" type="text" name="WIDsubject" value="<?php echo trim($_GET['orderid']);?>" > <br> </div> </div> <div class="uk-form-row"> <div class="uk-form-controls"> <label for="form-username" class="uk-form-label" >付款金額:</label> <input class="uk-form-width-large" type="text" name="WIDtotal_fee" value="<?php echo trim($_GET['amt']);?>"> <br> </div> </div> <div class="uk-form-row"> <div class="uk-form-controls"> <label for="form-username" class="uk-form-label" >商品描述:</label> <input class="uk-form-width-large" type="text" name="WIDbody" value="訂單測試支付"> <br> </div> </div> <input type="hidden" name="uid" value="<?php echo $_GET['uid'];?>"> <input type="hidden" name="codes" value="<?php echo $_GET['code'];?>"> <input type="hidden" name="tags" value="<?php echo $_GET['tag'] ?>"> <div class="uk-form-row"> <input class="uk-button uk-button-primary uk-button-large uk-width-1-1" type="submit" class="alisubmit" value ="確認支付"> </div> </div> </form>
設(shè)置alipay.config.php配置文件,主要配置一下幾個參數(shù)
$alipay_config['partner'] 設(shè)置簽約賬號ID
$alipay_config['key'] 設(shè)置MD5校驗key
$alipay_config['notify_url'] 設(shè)置異步回調(diào)地址
$alipay_config['return_url'] 設(shè)置回調(diào)返回地址
$alipay_config['transport'] 設(shè)置協(xié)議類型,默認為http
$alipay_config['partner'] = 'xxxxxxxxxxxx';
//收款支付寶賬號,以2088開頭由16位純數(shù)字組成的字符串,一般情況下收款賬號就是簽約賬號
$alipay_config['seller_id'] = $alipay_config['partner'];
// MD5密鑰,安全檢驗碼,由數(shù)字和字母組成的32位字符串,查看地址:https://b.alipay.com/order/pidAndKey.htm
$alipay_config['key'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// 服務(wù)器異步通知頁面路徑 需http://格式的完整路徑,不能加?id=123這類自定義參數(shù),必須外網(wǎng)可以正常訪問
$alipay_config['notify_url'] = "http://xx.xx.xx.xx/pay/notify_url.php";
// 頁面跳轉(zhuǎn)同步通知頁面路徑 需http://格式的完整路徑,不能加?id=123這類自定義參數(shù),必須外網(wǎng)可以正常訪問
$alipay_config['return_url'] = "http://xx.xx.xx.xx/pay/return_url.php";
//簽名方式
$alipay_config['sign_type'] = strtoupper('MD5');
//字符編碼格式 目前支持 gbk 或 utf-8
$alipay_config['input_charset']= strtolower('utf-8');
//ca證書路徑地址,用于curl中ssl校驗
//請保證cacert.pem文件在當前文件夾目錄中
$alipay_config['cacert'] = getcwd().'\\cacert.pem';
//訪問模式,根據(jù)自己的服務(wù)器是否支持ssl訪問,若支持請選擇https;若不支持請選擇http
$alipay_config['transport'] = 'http';
// 支付類型 ,無需修改
$alipay_config['payment_type'] = "1";
// 產(chǎn)品類型,無需修改
$alipay_config['service'] = "create_direct_pay_by_user";交易處理完成后會發(fā)送訂單信息及交易狀態(tài)到你指定好的回傳return_url.php中,你可以通過回傳給你的訂單狀態(tài)判斷交易是否成功,成功則進入下一步邏輯(你自己的訂單處理邏輯),否則返回失敗。
<?php
/* *
* 功能:支付寶頁面跳轉(zhuǎn)同步通知頁面
* 版本:3.3
* 日期:2012-07-23
* 說明:
* 以下代碼只是為了方便商戶測試而提供的樣例代碼,商戶可以根據(jù)自己網(wǎng)站的需要,按照技術(shù)文檔編寫,并非一定要使用該代碼。
* 該代碼僅供學(xué)習(xí)和研究支付寶接口使用,只是提供一個參考。
*************************頁面功能說明*************************
* 該頁面可在本機電腦測試
* 可放入HTML等美化頁面的代碼、商戶業(yè)務(wù)邏輯程序代碼
* 該頁面可以使用PHP開發(fā)工具調(diào)試,也可以使用寫文本函數(shù)logResult,該函數(shù)已被默認關(guān)閉,見alipay_notify_class.php中的函數(shù)verifyReturn
require_once("alipay.config.php");
require_once("lib/alipay_notify.class.php");
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
//計算得出通知驗證結(jié)果
$alipayNotify = new AlipayNotify($alipay_config);
$verify_result = $alipayNotify->verifyReturn();
if($verify_result) {//驗證成功
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//請在這里加上商戶的業(yè)務(wù)邏輯程序代碼
//——請根據(jù)您的業(yè)務(wù)邏輯來編寫程序(以下代碼僅作參考)——
//獲取支付寶的通知返回參數(shù),可參考技術(shù)文檔中頁面跳轉(zhuǎn)同步通知參數(shù)列表
//商戶訂單號^M
$out_trade_no = $_GET['out_trade_no'];
//支付寶交易號^M
$trade_no = $_GET['trade_no'];
//交易狀態(tài)
$trade_status = $_GET['trade_status'];
$total_fee = $_GET['total_fee'];
//獲取用戶返回數(shù)據(jù)
$user_info = $_GET['extra_common_param'];
$user_arr = explode('.',$user_info);
$uid = $user_arr[0];
$code = $user_arr[1];
$tags = $user_arr[2];
if($_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS') {
$types = "alipay";
$user_url = "/user_obj/do_orderinfo.php";
die("<script>;window.location='{$user_url}?uid={$uid}&code={$code}&tags={$tags}&payAmount={$total_fee}&order={$out_trade_no}&types={$types}';</script>");
//判斷該筆訂單是否在商戶網(wǎng)站中已經(jīng)做過處理
//如果沒有做過處理,根據(jù)訂單號(out_trade_no)在商戶網(wǎng)站的訂單系統(tǒng)中查到該筆訂單的詳細,并執(zhí)行商戶的業(yè)務(wù)程序
//如果有做過處理,不執(zhí)行商戶的業(yè)務(wù)程序
}
else {
echo "trade_status=".$_GET['trade_status'];
}
var_dump($verify_result);
echo "驗證成功<br />";
//——請根據(jù)您的業(yè)務(wù)邏輯來編寫程序(以上代碼僅作參考)——
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
else {
//驗證失敗
//如要調(diào)試,請看alipay_notify.php頁面的verifyReturn函數(shù)
echo "驗證失敗";
}
?>
<title>支付寶即時到賬交易接口</title>
</head>
<body>
</body>
</html>以上是“PHP如何接入支付寶即時到賬功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
新聞名稱:PHP如何接入支付寶即時到賬功能-創(chuàng)新互聯(lián)
標題路徑:http://www.chinadenli.net/article32/dpoopc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、網(wǎng)站設(shè)計公司、小程序開發(fā)、移動網(wǎng)站建設(shè)、面包屑導(dǎo)航、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容