> 快速對接支付寶接口,首先您需要創建一個控制器,名稱叫`Alipay.class.php`,然后保存下面的代碼到文件中,命名空間需要修改成您當前實例的命名空間:
~~~
<?php
namespace App\home;
class Alipay
{
protected $appId;
protected $charset;
protected $returnUrl;
protected $notifyUrl;
//私鑰值
protected $rsaPrivateKey;
protected $totalFee;
protected $outTradeNo;
protected $orderName;
public function __construct()
{
$this->charset = 'utf-8';
}
public function setAppid($appid)
{
$this->appId = $appid;
}
public function setReturnUrl($returnUrl)
{
$this->returnUrl = $returnUrl;
}
public function setNotifyUrl($notifyUrl)
{
$this->notifyUrl = $notifyUrl;
}
public function setRsaPrivateKey($rsaPrivateKey)
{
$this->rsaPrivateKey = $rsaPrivateKey;
}
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'=>'QUICK_WAP_WAY',
'total_amount'=>$this->totalFee, //單位 元
'subject'=>$this->orderName, //訂單標題
);
$commonConfigs = array(
//公共參數
'app_id' => $this->appId,
'method' => 'alipay.trade.wap.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;
}
}
~~~
### 使用:
> 然后在把下面的方法粘貼到您的控制器里面,在需要調用的地方直接通過$this->alipays('appid','跳轉url','通知url','訂單號','支付金額','RSA2','私鑰')`,注意使用要修改好參數即可
~~~
/**
* 支付寶付款
* @param $appid
* @param $returnUrl string 支付成功跳轉的URL
* @param $notifyUrl string 支付成功異步通知的URL
* @param $outTradeNo string 訂單號
* @param $payAmount string 支付金額
* @param $signType string 簽名類型
* @param $rsaPrivateKey string 支付寶私鑰
*/
public function alipays($appid, $returnUrl, $notifyUrl, $outTradeNo, $payAmount, $signType='RSA2', $rsaPrivateKey)
{
header('Content-type:text/html; Charset=utf-8');
$orderName = '商品購買訂單付款'; //訂單標題
/*** 配置結束 ***/
$aliPay = new Alipay();
$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;
}
~~~
- 概述
- 基礎
- 安裝
- 規范
- 目錄
- 環境
- 配置
- 全部配置
- 數據庫配置
- 緩存配置
- 框架配置
- 自定義配置
- 讀取配置
- 控制器
- 創建
- 規范
- 繼承
- 輸出
- 視圖
- 基本使用
- 渲染模板
- 賦值變量
- 獲取結果
- 模板
- 常用標簽
- if - 判斷
- foreach - 遍歷
- break - 停止循環
- continue - 跳過循環
- @index - 索引
- @iteration - 循環次數
- @first - 首次循環
- @last - 最后循環
- for - 循環
- var - 定義變量
- nocache - 禁用緩存
- assign - 變量賦值
- include - 引入文件
- 變量修飾
- default - 默認輸出
- capitalize - 首字母大寫
- lower - 字母轉小寫
- upper - 字符轉大寫
- count_characters - 統計字符長度
- count_words - 統計單詞數量
- date_format - 格式化日期
- Chapter - 文本實體化
- indent - 縮進文本
- nl2br - 轉義換行
- replace - 文本替換
- spacify - 插入文本
- string_format - 字符串格式化
- strip - 移除特殊字符
- truncate - 文本截取
- 保留變量
- 數據庫
- 配置
- 基本使用
- 數據處理
- 增加數據
- 刪除數據
- 修改數據
- 查詢數據
- 其他查詢
- 關鍵字
- field
- join
- where
- page
- limit
- orderby
- groupby
- 其他
- 調試
- 緩存
- 各個緩存服務安裝
- 基本使用
- 設定緩存
- 查詢緩存
- 刪除緩存
- 修改緩存
- 清空緩存
- 其他操作
- Session操作
- Cookie操作
- File緩存
- 輔助
- 功能列表
- helper助手
- 擴展
- Composer
- 自定義擴展
- 包擴展
- 訪問
- 入口文件
- 靜態化
- 路由
- 默認路由
- 傳統請求
- 規則
- 其他
- 上傳文件
- 寫出日志
- 展示狀態頁
- CLI模式運行
- 上線須知
- 獲取GET/POST
- 性能消耗
- 直接訪問靜態頁
- 內置常量
- 圖形驗證碼
- 安裝Composer
- 應用擴展
- 支付寶手機端支付
- 支付寶電腦端支付