支付寶作為國內主流的第三方支付平台,其接口整合是 PHP 開發者必備技能。本文將透過實際代碼範例,詳細解析如何使用 PHP 串接支付寶即時到帳接口,包含配置文件、核心類別與支付頁面的完整實作流程。
alipay_config.php集中管理所有支付寶業務參數,便於維護與環境切換:
<?php
// 支付寶網關地址(正式環境)
$interfaceurl = 'https://www.alipay.com/payto:';
// 合作身份者 ID(簽約帳號)
$partner = '2088002008096997';
// 安全校驗碼(密鑰)
$payalikey = '';
// 賣家支付寶帳號
$selleremail = '';
// 網站資訊
$sitename = '您的網站名稱';
$weburl = 'https://您的網站網址';
// 運費設定
$o_fee = '0.00'; // 平郵
$e_fee = '0.00'; // 快遞
// 支付按鈕
$imgurl = 'pay.gif';
$imgtitle = '使用支付寶購買';
?>
alipay.php封裝參數組裝、簽名生成與跳轉 URL 建構邏輯:
<?php
require_once('alipay_config.php');
class Alipay {
/**
* 產生支付寶跳轉連結
* @param array $params 業務參數陣列
* @return string 完整跳轉 URL
*/
public function getUrl(array $params) {
$gateway = $params['gateway'] . $params['seller'] . '?';
$signStr = '';
// 參數排序與 URL 編碼
ksort($params);
foreach ($params as $key => $value) {
if ($value !== '' && $key !== 'gateway' && $key !== 'sign_key') {
$gateway .= $key . '=' . urlencode($value) . '&';
$signStr .= $key . $value;
}
}
// MD5 簽名
$gateway .= 'sign=' . md5($signStr . $params['sign_key']);
return $gateway;
}
}
?>
pay.php接收訂單資料、驗證、產生支付連結並呈現確認頁面:
<?php
require_once('alipay_config.php');
require_once('alipay.php');
// 1. 獲取並過濾 POST 資料
$orderData = [
'out_trade_no' => $_POST['aliorder'] ?? '',
'subject' => '訂單號:' . ($_POST['aliorder'] ?? ''),
'body' => $_POST['alibody'] ?? '商品介紹',
'total_fee' => floatval($_POST['alimoney'] ?? 0),
'receive_name' => $_POST['aliname'] ?? '',
'receive_address' => $_POST['aliaddress'] ?? '',
'receive_zip' => $_POST['alizipcode'] ?? '',
'receive_phone' => $_POST['aliphone'] ?? '',
'receive_mobile' => $_POST['alimob'] ?? '',
'logistics_type' => $_POST['alimailtype'] ?? 'EXPRESS',
'logistics_fee' => '0.00',
'logistics_payment' => 'SELLER_PAY',
];
// 2. 組裝支付寶請求參數
$alipayParams = [
'gateway' => $interfaceurl,
'service' => 'trade_create_by_buyer',
'partner' => $partner,
'_input_charset' => 'utf-8',
'sign_type' => 'MD5',
'sign_key' => $payalikey,
'seller_email' => $selleremail,
'return_url' => $weburl . '/return_url.php',
'notify_url' => $weburl . '/notify_url.php',
'out_trade_no' => $orderData['out_trade_no'],
'subject' => $orderData['subject'],
'body' => $orderData['body'],
'price' => $orderData['total_fee'],
'quantity' => 1,
'logistics_type' => $orderData['logistics_type'],
'logistics_fee' => $orderData['logistics_fee'],
'logistics_payment' => $orderData['logistics_payment'],
'receive_name' => $orderData['receive_name'],
'receive_address' => $orderData['receive_address'],
'receive_zip' => $orderData['receive_zip'],
'receive_phone' => $orderData['receive_phone'],
'receive_mobile' => $orderData['receive_mobile'],
];
// 3. 產生支付連結
$alipay = new Alipay();
$payUrl = $alipay->getUrl($alipayParams);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>確認支付寶付款</title>
<style>body{font-family:Microsoft JhengHei,sans-serif;max-width:600px;margin:2rem auto;padding:1rem;}</style>
</head>
<body>
<h2>訂單確認</h2>
<table width="100%" border="1" cellpadding="8" cellspacing="0" style="border-collapse:collapse;">
<tr><th width="30%">訂單號碼</th><td><?= htmlspecialchars($orderData['out_trade_no']) ?></td></tr>
<tr><th>收貨人</th><td><?= htmlspecialchars($orderData['receive_name']) ?></td></tr>
<tr><th>付款金額</th><td><strong><?= number_format($orderData['total_fee'], 2) ?></strong> 元</td></tr>
<tr><th>收貨地址</th><td><?= htmlspecialchars($orderData['receive_address']) ?></td></tr>
<tr><th>聯繫電話</th><td><?= htmlspecialchars($orderData['receive_phone']) ?></td></tr>
<tr><th>手機號碼</th><td><?= htmlspecialchars($orderData['receive_mobile']) ?></td></tr>
<tr><th>客戶留言</th><td><?= htmlspecialchars($orderData['body']) ?></td></tr>
</table>
<div style="margin-top:1.5rem;text-align:center;">
<a href="<?= $payUrl ?>" target="_blank" style="display:inline-block;padding:10px 20px;background:#ff6600;color:#fff;text-decoration:none;border-radius:4px;">
<img src="<?= $imgurl ?>" alt="<?= $imgtitle ?>" style="vertical-align:middle;margin-right:8px;"> 立即支付
</a>
<button onclick="history.back()" style="margin-left:10px;padding:10px 20px;">返回修改</button>
</div>
</body>
</html>
notify_url.php 驗證支付寶回調簽名,確認交易狀態後再更新訂單。本文展示了從配置、核心邏輯到前端呈現的完整支付寶整合流程。雖然示範代碼基於經典接口,但核心概念(參數組裝、簽名驗證、跳轉支付、異步通知)在新版 API 中依然適用。開發者應根據專案需求選擇適合的接口版本(如電腦網站支付、手機網站支付、APP 支付等),並嚴格遵循官方文檔與安全規範進行開發。