1.下載官方sdk
https://docs.open.alipay.com/194/105201/
2.導入官方文件包 如圖
3 alipayAction.class.php 是封裝的支付接口
<?php
namespace action\pay;
header("Content-type: text/html; charset=utf-8");
/**
* 支付寶支付
*/
class alipayAction extends \action
{
public function qrpay($data){
$subject = "短信服務支付";
// 支付超時,線下掃碼交易定義為5分鐘
$timeExpress = "5m";
// 創建請求builder,設置請求參數 路徑根據自己實際情況 寫絕對路徑
require_once ROOT.'cube/action/pay/f2fpay/model/builder/AlipayTradePrecreateContentBuilder.php';
require_once ROOT.'cube/action/pay/f2fpay/service/AlipayTradeService.php';
require_once ROOT.'cube/action/pay/f2fpay/config/config.php';
$qrPayRequestBuilder = new \AlipayTradePrecreateContentBuilder();
$qrPayRequestBuilder->setOutTradeNo($data['outTradeNo']);
$qrPayRequestBuilder->setTotalAmount($data['totalAmount']);
$qrPayRequestBuilder->setTimeExpress($timeExpress);
$qrPayRequestBuilder->setSubject($subject);
// 調用qrPay方法獲取當面付應答
$qrPay = new \AlipayTradeService($config);
$qrPayResult = $qrPay->qrPay($qrPayRequestBuilder);
return $qrPayResult->getResponse();
}
public function notify_alipay($out_trade_no){
require_once ROOT.'cube/action/pay/f2fpay/service/AlipayTradeService.php';
require_once ROOT.'cube/action/pay/f2fpay/config/config.php';
//構造查詢業務請求參數對象
$queryContentBuilder = new \AlipayTradeQueryContentBuilder();
$queryContentBuilder->setOutTradeNo($out_trade_no);
//初始化類對象,調用queryTradeResult方法獲取查詢應答
$queryResponse = new \AlipayTradeService($config);
$queryResult = $queryResponse->queryTradeResult($queryContentBuilder);
//根據查詢返回結果狀態進行業務處理
return $queryResult->getResponse();
}
}
調用方式參考
<?php
namespace action\admin;
/**
* 短信服務
*/
class messageAction extends \action
{
public function sms_service(){
//操作類型 0 充值 1開通
if(!isset($_POST['type'])) return json_encode(array('RES'=>'ERR_POST','MSG'=>'操作類型為空'));
if(empty($_POST['total_row'])) return json_encode(array('RES'=>'ERR_POST','MSG'=>'短信數量為空'));
$data = $_POST;
$data['recharge_price'] = (int)$_POST['total_row']*0.01;
$data['outTradeNo'] = A('strlib/basic/randstr',array(16,3));
$m = M();
$res = $m->ins('sms_recharge')->values($data)->exe();
$order['outTradeNo'] = $data['outTradeNo'];
$order['totalAmount'] = $data['recharge_price'];
$result = $this->qrpay($order);
$order['result'] = $result;
$order['qrcode'] = "/index.php?m=admin&c=message&a=qrcode&qrcode=".$result['qr_code'];
$order['id'] = $res;
return json_encode($order);
}
public function qrpay($data){
$result = A('pay/alipay/qrpay',array($data));
$result = json_decode(json_encode($result),true);
return $result;
}
//生成登錄二維碼
public function qrcode() {
if(empty($_GET['qrcode'])) return;
//引入phpqrcode庫文件
include(ROOT.'cube/action/phpqrcode/phpqrcode/phpqrcode.php');
$data = $_GET['qrcode'];
// 糾錯級別:L、M、Q、H
$errorCorrectionLevel = 'L';
//輸入二維碼到瀏覽器
\QRcode::png($data,false,$errorCorrectionLevel,5);
}
//輪詢查詢支付狀態
public function paystatu(){
if(empty($_POST['outTradeNo'])) return json_encode(array('RES'=>'ERR_POST'));
if(empty($_POST['id'])) return json_encode(array('RES'=>'ERR_POST'));
$result = A('pay/alipay/notify_alipay',array($_POST['outTradeNo']));
$result = json_decode(json_encode($result),true);
if($result['msg']=='Success'){
switch ($result['trade_status']) {
case 'TRADE_SUCCESS':
$res = M()->sel('phone,recharge_price,total_row,type')->from('sms_recharge')->where("id = {$_POST['id']}")->exe();
if(!$res) return json_encode(array('RES'=>'ERRSEL','MSG'=>'記錄不存在!'));
$data['domain'] = $_SERVER['HTTP_HOST'];
$data['phone'] = $res['phone'];
$data['price'] = $res['recharge_price'];
$data['number'] = $res['total_row'];
$data['type'] = $res['type'];
//總站添加充值記錄表
//http://dev.fushuishop.com/index.php?m=sms&c=agencySms&a=postApplyMsg
A('sms/agencySms/postApplyMsg',array($data));
$res = M()->upd('sms_recharge')->set('pay_state=1')->where("id = {$_POST['id']}")->exe();
return empty($res)?json_encode(array('RES'=>'ERRUPD','MSG'=>'支付失敗')):json_encode(array('RES'=>'SUCCESS','MSG'=>'支付成功'));
break;
case 'WAIT_BUYER_PAY':
return json_encode(array('RES'=>'WAIT_BUYER_PAY','MSG'=>'等待用戶支付'));
break;
case 'TRADE_CLOSED':
return json_encode(array('RES'=>'TRADE_CLOSED','MSG'=>'交易關閉'));
break;
default:
return json_encode(array('RES'=>'ERROR','MSG'=>'支付失?。?));
break;
}
}else{
return json_encode(array('RES'=>'FAILD','MSG'=>'請求失敗'));
}
}
}
浙公網安備 33010602011771號