1.下载官方sdk
https://docs.open.alipay.com/194/105201/
2.导入官方文件包 如图
蓝色部分是官方sdk包
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'=>'请求失败'));
}
}
}