(官方文檔 https://docs.open.alipay.com/270/105899/)
第一步:創建應用
要在您的應用中接入電腦網站支付能力,您需要登錄支付寶開放平臺(open.alipay.com),在開發者中心中創建您的應用,應用審覈通過後會生成應用唯一標識(APPID),並且可以申請開通開放產品使用權限。通過 APPID 您的應用才能調用開放產品的接口能力。需要詳細瞭解開放平臺創建應用步驟請參考《開放平臺應用創建指南》。
第二步:配置應用
添加功能並簽約
應用創建完成後,系統會自動跳轉到應用詳情頁面。開發者可以在 功能列表 中點擊 添加功能 來添加電腦網站支付功能。待應用上線後,您可以給添加的功能進行簽約。電腦網站支付功能支持兩種簽約方式:商家中心簽約和應用詳情的功能列表處簽約(如下圖所示)。
詳細步驟步驟可以參考添加應用功能,第三方應用可以代替商戶簽約。
配置密鑰
爲了保證交易雙方(商戶和支付寶)的身份和數據安全,開發者在調用接口前,需要配置雙方密鑰,對交易數據進行雙方校驗。密鑰包含應用私鑰(APP_PRIVATE_KEY)和應用公鑰(APP_PUBLIC_KEY)。生成密鑰後,開發者需要在開放平臺開發者中心進行密鑰配置,配置完成後可以獲取支付寶公鑰(ALIPAY_PUBLIC_KEY),配置的詳細步驟請參考《配置應用環境》。您還可以通過觀看快速簽名教程學習密鑰的配置。
配置公鑰--查看下面地址
--https://docs.open.alipay.com/291/105971
說明:
支付寶開放平臺 SDK 封裝了簽名和驗簽過程,只需配置賬號及密鑰參數,建議開發者使用。開發者還可以通過自助排查流程和驗籤教程自助排查配置應用過程中遇到的問題。
第三步:集成並配置 SDK(我是github一個文件搞定支付)
轉載---- https://github.com/dedemao/alipay
<?php
header('Content-type:text/html; Charset=utf-8');
/*** 請填寫以下配置信息 ***/
$appid = 'xxxxx'; //https://open.alipay.com 賬戶中心->密鑰管理->開放平臺密鑰,填寫添加了電腦網站支付的應用的APPID
$returnUrl = 'http://www.xxx.com/alipay/return.php'; //付款成功後的同步回調地址
$notifyUrl = 'http://www.xxx.com/alipay/notify.php'; //付款成功後的異步回調地址
$outTradeNo = uniqid(); //你自己的商品訂單號,不能重複
$payAmount = 0.01; //付款金額,單位:元
$orderName = '支付測試'; //訂單標題
$signType = 'RSA2'; //簽名算法類型,支持RSA2和RSA,推薦使用RSA2
$rsaPrivateKey='xxxx'; //商戶私鑰,填寫對應簽名算法類型的私鑰,如何生成密鑰參考:https://docs.open.alipay.com/291/105971和https://docs.open.alipay.com/200/105310
/*** 配置結束 ***/
$aliPay = new AlipayService();
$aliPay->setAppid($appid);
$aliPay->setReturnUrl($returnUrl);
$aliPay->setNotifyUrl($notifyUrl);
$aliPay->setRsaPrivateKey($rsaPrivateKey);
$aliPay->setTotalFee($payAmount);
$aliPay->setOutTradeNo($outTradeNo);
$aliPay->setOrderName($orderName);
$sHtml = $aliPay->doPay();
echo $sHtml;
class AlipayService
{
protected $appId;
protected $returnUrl;
protected $notifyUrl;
protected $charset;
//私鑰值
protected $rsaPrivateKey;
protected $totalFee;
protected $outTradeNo;
protected $orderName;
public function __construct()
{
$this->charset = 'utf8';
}
public function setAppid($appid)
{
$this->appId = $appid;
}
public function setReturnUrl($returnUrl)
{
$this->returnUrl = $returnUrl;
}
public function setNotifyUrl($notifyUrl)
{
$this->notifyUrl = $notifyUrl;
}
public function setRsaPrivateKey($saPrivateKey)
{
$this->rsaPrivateKey = $saPrivateKey;
}
public function setTotalFee($payAmount)
{
$this->totalFee = $payAmount;
}
public function setOutTradeNo($outTradeNo)
{
$this->outTradeNo = $outTradeNo;
}
public function setOrderName($orderName)
{
$this->orderName = $orderName;
}
/**
* 發起訂單
* @return array
*/
public function doPay()
{
//請求參數
$requestConfigs = array(
'out_trade_no'=>$this->outTradeNo,
'product_code'=>'FAST_INSTANT_TRADE_PAY',
'total_amount'=>$this->totalFee, //單位 元
'subject'=>$this->orderName, //訂單標題
);
$commonConfigs = array(
//公共參數
'app_id' => $this->appId,
'method' => 'alipay.trade.page.pay', //接口名稱
'format' => 'JSON',
'return_url' => $this->returnUrl,
'charset'=>$this->charset,
'sign_type'=>'RSA2',
'timestamp'=>date('Y-m-d H:i:s'),
'version'=>'1.0',
'notify_url' => $this->notifyUrl,
'biz_content'=>json_encode($requestConfigs),
);
$commonConfigs["sign"] = $this->generateSign($commonConfigs, $commonConfigs['sign_type']);
return $this->buildRequestForm($commonConfigs);
}
/**
* 建立請求,以表單HTML形式構造(默認)
* @param $para_temp 請求參數數組
* @return 提交表單HTML文本
*/
protected function buildRequestForm($para_temp) {
$sHtml = "正在跳轉至支付頁面...<form id='alipaysubmit' name='alipaysubmit' action='https://openapi.alipay.com/gateway.do?charset=".$this->charset."' method='POST'>";
foreach($para_temp as $key=>$val){
if (false === $this->checkEmpty($val)) {
$val = str_replace("'","'",$val);
$sHtml.= "<input type='hidden' name='".$key."' value='".$val."'/>";
}
}
//submit按鈕控件請不要含有name屬性
$sHtml = $sHtml."<input type='submit' value='ok' style='display:none;''></form>";
$sHtml = $sHtml."<script>document.forms['alipaysubmit'].submit();</script>";
return $sHtml;
}
public function generateSign($params, $signType = "RSA") {
return $this->sign($this->getSignContent($params), $signType);
}
protected function sign($data, $signType = "RSA") {
$priKey=$this->rsaPrivateKey;
$res = "-----BEGIN RSA PRIVATE KEY-----\n" .
wordwrap($priKey, 64, "\n", true) .
"\n-----END RSA PRIVATE KEY-----";
($res) or die('您使用的私鑰格式錯誤,請檢查RSA私鑰配置');
if ("RSA2" == $signType) {
openssl_sign($data, $sign, $res, version_compare(PHP_VERSION,'5.4.0', '<') ? SHA256 : OPENSSL_ALGO_SHA256); //OPENSSL_ALGO_SHA256是php5.4.8以上版本才支持
} else {
openssl_sign($data, $sign, $res);
}
$sign = base64_encode($sign);
return $sign;
}
/**
* 校驗$value是否非空
* if not set ,return true;
* if is null , return true;
**/
protected function checkEmpty($value) {
if (!isset($value))
return true;
if ($value === null)
return true;
if (trim($value) === "")
return true;
return false;
}
public function getSignContent($params) {
ksort($params);
$stringToBeSigned = "";
$i = 0;
foreach ($params as $k => $v) {
if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
// 轉換成目標字符集
$v = $this->characet($v, $this->charset);
if ($i == 0) {
$stringToBeSigned .= "$k" . "=" . "$v";
} else {
$stringToBeSigned .= "&" . "$k" . "=" . "$v";
}
$i++;
}
}
unset ($k, $v);
return $stringToBeSigned;
}
/**
* 轉換字符集編碼
* @param $data
* @param $targetCharset
* @return string
*/
function characet($data, $targetCharset) {
if (!empty($data)) {
$fileType = $this->charset;
if (strcasecmp($fileType, $targetCharset) != 0) {
$data = mb_convert_encoding($data, $targetCharset, $fileType);
//$data = iconv($fileType, $targetCharset.'//IGNORE', $data);
}
}
return $data;
}
}