php實現支付寶在線支付和掃碼支付demo
背景:在做一個公眾號時增加了h5端,需要接入支付,非微信環境,選擇了支付寶,以下簡單記錄下實現過程,並做了簡單的封裝,拿來即可使用,注意:本項目只是基於官方demo修改的,需要接入自己項目的需要按需獲取到。
demo的github地址:https://github.com/alisleepy/alipay
目錄結構:
以下的文件目錄無需修改,只修改config.php配置文件和paytest.php和create_qrcode.php文件
/aop //核心庫
/img //二維碼中心的icon(自己引入的)
/lib //核心庫
/lotusphp_runtime //沒用到
/phpqrcode //生成二維碼的插件(掃碼支付時自己引入的,沒用官方的)
/service //官方demo的測試文件(沒用到)
AopSdk.php //demo項目入口文件,不用修改
config.php //重要,存放配置文件
create_qrcode.php //二維碼掃碼支付demo
notify_url.php //異步回調地址(只測試了支付,沒用到)
paytest.php //在線支付demo
return_url.php //同步跳轉地址(沒用到)
步驟:
- 申請支付寶開發者
- 創建沙箱應用,獲取到appId
- 獲取公鑰秘鑰等信息,修改config.php
- 修改文件實現支付功能
在線支付代碼:paytest.php
<?php
/**
* 功能:支付寶支付測試文件
* 版本:v1.0
* author:wangkk
* 以下部分就是具體的支付過程,只需要引入自己的配置文件$config數組信息,同時需要獲取訂單信息即可使用
*/
//引入sdk文件
require_once dirname ( __FILE__ ).DIRECTORY_SEPARATOR.'aop/AopClient.php';
require_once dirname ( __FILE__ ).DIRECTORY_SEPARATOR.'aop/request/AlipayTradeWapPayRequest.php';
//引入配置文件信息
require_once dirname ( __FILE__ ).DIRECTORY_SEPARATOR.'config.php';
/**
* 支付寶支付類的封裝
*/
class Alipay{
//配置文件數據
public $alipay_config;
//構造函數,獲取數據
public function __construct($alipay_config){
//配置項
$this->gateway_url = $alipay_config['gatewayUrl'];
$this->appid = $alipay_config['app_id'];
$this->private_key = $alipay_config['merchant_private_key'];
$this->alipay_public_key = $alipay_config['alipay_public_key'];
$this->charset = $alipay_config['charset'];
$this->signtype = $alipay_config['sign_type'];
$this->notify_url = $alipay_config['notify_url'];
$this->return_url = $alipay_config['return_url'];
if(empty($this->appid) || trim($this->appid) == ""){
throw new Exception("appid不能為空!");
}
if(empty($this->private_key) || trim($this->private_key) == ""){
throw new Exception("商戶密鑰不能為空!");
}
if(empty($this->alipay_public_key) || trim($this->alipay_public_key) == ""){
throw new Exception("商戶公鑰不能為空!");
}
if(empty($this->charset) || trim($this->charset)== "" ){
throw new Exception("編碼格式不能為空");
}
if(empty($this->gateway_url) || trim($this->gateway_url) == ""){
throw new Exception("支付網關地址不能為空!");
}
if(empty($this->notify_url) || trim($this->notify_url) == ""){
throw new Exception("異步回調地址不能為空!");
}
}
public function pay(){
//訂單號,自定義,唯一
$out_trade_no = $_GET['out_trade_no'];
/** --------------------------------以下部分需要修改:獲取訂單信息 start--------------------------------- **/
//通過訂單號獲取到訂單信息
// $orderInfo = M('order')->where(['out_trade_no'=>$out_trade_no])->find();
// if(empty($orderInfo)){
// throw new Exception("查無此訂單");
// }
// //參數列表
// $body = $orderInfo['body']; //商品描述,可為空
// $subject = $orderInfo['subject']; //訂單標題,必填
// $out_trade_no = $orderInfo['out_trade_no']; //訂單號,必填
// $total_amount = $orderInfo['total_amount']; //訂單金額,必填
/** --------------------------------以上部分需要修改:獲取訂單信息 end--------------------------------- **/
//訂單測試信息,真實項目把以下幾行刪除,使用上邊的真實數據
$body = '商品描述'; //商品描述,可為空
$subject = '訂單標題'; //訂單標題,必填
$out_trade_no = rand(10000,99999); //訂單號,必填
$total_amount = rand(1,5); //訂單金額,必填
$timeout_express = '1m'; //超時,1分鍾
$product_code = 'QUICK_WAP_WAY'; //手機端支付寶
if(empty($subject) || trim($subject) == ""){
throw new Exception("訂單標題不能為空");
}
if(empty($total_amount) || trim($total_amount) == ""){
throw new Exception("訂單金額不能為空");
}
//組裝訂單數據
$bizContentarr = array(
'body' => $body ? $body : '', //商品描述,可以為空
'subject' => $subject,
'out_trade_no' => $out_trade_no,
'total_amount' => $total_amount,
'timeout_express' => $timeout_express,
'product_code' => $product_code,
);
$bizContent = json_encode($bizContentarr,JSON_UNESCAPED_UNICODE);
//設置數據
$aopObj = new \AopClient();
$aopObj->gatewayUrl = $this->gateway_url;
$aopObj->appId = $this->appid;
$aopObj->rsaPrivateKey = $this->private_key;
$aopObj->alipayrsaPublicKey = $this->alipay_public_key;
$aopObj->apiVersion = '1.0';
$aopObj->postCharset = $this->charset;
$aopObj->format = 'json';
$aopObj->signType = $this->signtype;
//設置請求的數據
$request = new \AlipayTradeWapPayRequest ();
$request->setBizContent($bizContent);
$request->setNotifyUrl($this->notify_url);
$request->setReturnUrl($this->return_url);
$result = $aopObj->pageExecute($request);
echo $result;
}
}
//獲取到配置文件,框架里的話直接放在配置文件中,通過框架方法去獲取
$configInfo = $config;
$AlipayObj = new Alipay($configInfo);
$AlipayObj->pay();
掃碼支付代碼:create_qrcode.php
<?php
/**
* 功能:支付寶生成二維碼
* 版本:v1.0
* author:wangkk
* 以下部分就是具體的生成二維碼過程,只需要引入自己的配置文件$config數組信息,同時需要獲取訂單信息即可使用
*/
//引入sdk文件
require_once dirname ( __FILE__ ).DIRECTORY_SEPARATOR.'aop/AopClient.php';
require_once dirname ( __FILE__ ).DIRECTORY_SEPARATOR.'aop/request/AlipayTradePrecreateRequest.php';
//引入配置文件信息
require_once dirname ( __FILE__ ).DIRECTORY_SEPARATOR.'config.php';
//引入生成二維碼的插件
require_once dirname ( __FILE__ ).DIRECTORY_SEPARATOR.'phpqrcode/phpqrcode.php';
class CreateQrcode{
public function __construct($alipay_config){
//配置項
$this->gateway_url = $alipay_config['gatewayUrl'];
$this->appid = $alipay_config['app_id'];
$this->private_key = $alipay_config['merchant_private_key'];
$this->alipay_public_key = $alipay_config['alipay_public_key'];
$this->charset = $alipay_config['charset'];
$this->signtype = $alipay_config['sign_type'];
$this->notify_url = $alipay_config['notify_url'];
$this->return_url = $alipay_config['return_url'];
if(empty($this->appid) || trim($this->appid) == ""){
throw new Exception("appid不能為空!");
}
if(empty($this->private_key) || trim($this->private_key) == ""){
throw new Exception("商戶密鑰不能為空!");
}
if(empty($this->alipay_public_key) || trim($this->alipay_public_key) == ""){
throw new Exception("商戶公鑰不能為空!");
}
if(empty($this->charset) || trim($this->charset)== "" ){
throw new Exception("編碼格式不能為空");
}
if(empty($this->gateway_url) || trim($this->gateway_url) == ""){
throw new Exception("支付網關地址不能為空!");
}
if(empty($this->notify_url) || trim($this->notify_url) == ""){
throw new Exception("異步回調地址不能為空!");
}
}
//支付
public function pay(){
//訂單號,自定義,唯一
$out_trade_no = $_GET['out_trade_no'];
/** --------------------------------以下部分需要修改:獲取訂單信息 start--------------------------------- **/
//通過訂單號獲取到訂單信息
// $orderInfo = M('order')->where(['out_trade_no'=>$out_trade_no])->find();
// if(empty($orderInfo)){
// throw new Exception("查無此訂單");
// }
// //參數列表
// $body = $orderInfo['body']; //商品描述,可為空
// $subject = $orderInfo['subject']; //訂單標題,必填
// $out_trade_no = $orderInfo['out_trade_no']; //訂單號,必填
// $total_amount = $orderInfo['total_amount']; //訂單金額,必填
/** --------------------------------以上部分需要修改:獲取訂單信息 end--------------------------------- **/
//訂單測試信息,真實項目把以下幾行刪除,使用上邊的真實數據
$body = '商品描述'; //商品描述,可為空
$subject = '訂單標題'; //訂單標題,必填
$out_trade_no = rand(10000,99999); //訂單號,必填
$total_amount = rand(1,5); //訂單金額,必填
$aopObj = new \AopClient ();
//設置值
$aopObj->gatewayUrl = $this->gateway_url;
$aopObj->appId = $this->appid;
$aopObj->rsaPrivateKey = $this->private_key;
$aopObj->alipayrsaPublicKey = $this->alipay_public_key;
$aopObj->apiVersion = '1.0';
$aopObj->postCharset = $this->charset;
$aopObj->format = 'json';
$aopObj->signType = $this->signtype;
$request = new AlipayTradePrecreateRequest();
//組裝訂單數據
$timeout_express = '5m'; //超時,1分鍾
$bizContentarr = array(
'body' => $body ? $body : '', //商品描述,可以為空
'subject' => $subject,
'out_trade_no' => $out_trade_no,
'total_amount' => $total_amount,
'timeout_express' => $timeout_express, //過期時間
);
$bizContent = json_encode($bizContentarr,JSON_UNESCAPED_UNICODE);
$request->setBizContent($bizContent);
$result = $aopObj->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if(!empty($resultCode) && $resultCode == 10000){
//成功,得到二維碼,在這不使用官方的方法,官方使用的是google的,牆內不ok
$qr_code_url = $result->$responseNode->qr_code;
$icon = './img/logo.png';//准備好的logo圖片
\QRcode::png($qr_code_url,false, 'H', 4, false);
$code = ob_get_clean();
$code = imagecreatefromstring($code);
$logo = imagecreatefrompng($icon);
$QR_width = imagesx($code);//二維碼圖片寬度
$QR_height = imagesy($code);//二維碼圖片高度
$logo_width = imagesx($logo);//logo圖片寬度
$logo_height = imagesy($logo);//logo圖片高度
$logo_qr_width = $QR_width / 4;
$scale = $logo_width/$logo_qr_width;
$logo_qr_height = $logo_height/$scale;
$from_width = ($QR_width - $logo_qr_width) / 2;
//重新組合圖片並調整大小
imagecopyresampled($code, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
header ( "Content-type: image/png" );
ImagePng($code);
echo $qrcode;die;
} else {
echo 'fail';die;
}
}
}
$alipay_config = $config;
$CreateQrcodeObj = new CreateQrcode($alipay_config);
$CreateQrcodeObj->pay();
總結
終點就是獲取公鑰秘鑰這部分一定不能錯,還有就是在真實項目中的話按需引入sdk文件,但aop,lotusphp_runtime這兩個文件夾是支付寶核心庫,必須引入,AopSdk.php是入口文件,必須引入