支付宝最新接口对接php5.5+

下面以手机网站支付为例:

可先下载最新的支付宝对接 SDK https://docs.open.alipay.com/203/105910/
需 php5.5 以上环境。网站应用环境也必须要是 php5.5 以上。

下载后直接放入\ThinkPHP\Library\Vendor\Alipay 中。
特别注意的是密钥的生成 https://docs.open.alipay.com/common/104740
利用工具生成的公钥和查看的公钥是不一样的,需要特别注意。

新建支付服务类文件\Service\AlipayService.class.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
namespace Home\Service;

class AlipayService {
/**
* 支付宝配置信息
* 本使用包为最新SDK。php5.5+才支持
* 采用的RSA2加密方式(支付宝推荐),到https://docs.open.alipay.com/291/105971/去下载windows工具,生成公钥和私钥,将公钥在控制台上传设置,再查看其公钥填入以下公钥信息
*/
function __construct() {
$payConfig = array(
'app_id' => D('Config')->get('alipay_partner'), //应用ID,你的APPID。2017071907816658
'merchant_private_key' => '', //应用私钥,你的原始格式RSA私钥
'notify_url' => C('PROTOCOL') . $_SERVER['SERVER_NAME'] . U('Pay/alipay_notify_url'), //异步通知地址
'return_url' => C('PROTOCOL') . $_SERVER['SERVER_NAME'] . U('Pay/alipay_notify_url'), //同步跳转
'charset' => 'UTF-8', //编码格式
'sign_type' => 'RSA2', //签名方式
'gatewayUrl' => 'https://openapi.alipay.com/gateway.do', //支付宝网关
'alipay_public_key' => '', //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。设置和支付是两套内容。设置后学要到管理平台去查看应用公钥
'pay_log_txt' => __ROOT__ . '/Public/alipay_log_bef74b0ac451f8e6e5301453f947499899.txt', //支付日志(需要先建立此文件)
);
$this->payConfig = $payConfig;
}

/**
* 获取支付宝配置参数
* @return array
*/
public function getConfig() {
return $this->payConfig;
}

/**
* 设置支付配置参数
* @param $data
*/
public function setConfig($data) {
$payConfig = $this->payConfig;
foreach ($data as $k => $v) {
$payConfig[$k] = $v;
}
$this->payConfig = $payConfig;
}

/**
* 支付宝支付
* @param $data
*/
public function pay($data) {
Vendor('Alipay.wappay.service.AlipayTradeService');
Vendor('Alipay.wappay.buildermodel.AlipayTradeWapPayContentBuilder');
$config = $this->payConfig;
$out_trade_no = $data['out_trade_no']; //商户订单号,商户网站订单系统中唯一订单号,必填
$subject = $data['subject']; //订单名称,必填
$total_amount = $data['total_amount']; //付款金额,必填
$body = $data['body']; //商品描述,可空
$timeout_express = "1m"; //超时时间
$payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
$payRequestBuilder->setBody($body);
$payRequestBuilder->setSubject($subject);
$payRequestBuilder->setOutTradeNo($out_trade_no);
$payRequestBuilder->setTotalAmount($total_amount);
$payRequestBuilder->setTimeExpress($timeout_express);
$payResponse = new \AlipayTradeService($config);
$result = $payResponse->wapPay($payRequestBuilder, $config['return_url'], $config['notify_url']);
}

/**
* 记录支付异步记录
* @param $data
*/
public function setPaylog($data) {
$config = $this->payConfig;
$str = serialize($data) . "\r\n==================================================================================\r\n";;
import("Org.Util.File");
$file = new \File($config['pay_log_txt']);
$realfiel = $file->getRealFile();
file_put_contents($realfiel, $str, FILE_APPEND);
}
}

支付处理控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace Home\Controller;

use Think\Controller;
use Common\Controller\HomeController;

class PayController extends HomeController {
/**
* 支付宝支付
* @param $order_number 订单号
*/
public function alipay($order_number) {
$order = D('Order')->where(array('order_number' => $order_number))->find();
$orderDetail = D('OrderDetail')->where(array('oid' => $order['id']))->field('pid')->select();
$firstProduct = D('Products')->where(array('id' => $orderDetail[0]['pid']))->field('title')->find();
//支付数据
$payData = array(
'out_trade_no' => $order['order_number'],
'subject' => D('Config')->get('alipay_subject') . $order_number, //支付主题
'total_amount' => 0.01, //测试订单号0.01元支付$order['real_all_money']
'body' => $firstProduct['title'] . '等',
);
D('Alipay', 'Service')->pay($payData);
}

/**
* 支付宝回调地址
*/
public function alipay_notify_url() {
//记录日志,这里最好放在开头以防下面代码逻辑有误不能正常发现是否被调用
D('Alipay', 'Service')->setPaylog($logArr);

Vendor('Alipay.wappay.service.AlipayTradeService');
$payConfig = D('Alipay', 'Service')->getConfig();
$arr = $_POST;
$logArr = $arr;
$alipaySevice = new \AlipayTradeService($payConfig);
$alipaySevice->writeLog(var_export($_POST, true));
$result = $alipaySevice->check($arr);
if ($result) {
//验证成功
$out_trade_no = $_POST['out_trade_no']; //商户订单号
$trade_no = $_POST['trade_no']; //支付宝交易号
$trade_status = $_POST['trade_status']; //交易状态
if ($_POST['trade_status'] == 'TRADE_FINISHED') {
//交易结束,不可退款
} else if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
//交易支付成功
$re = D('Order')->setPay($out_trade_no, 1);
}
echo "success"; //请不要修改或删除
$logArr['result'] = "success";
} else {
//验证失败
echo "fail"; //请不要修改或删除
$logArr['result'] = "fail";
}
}

/**
* 支付成功跳转地址
*/
public function resultinfo() {
$ModelObj = D('Order');
$order_number = I('out_trade_no');
$order = $ModelObj->where(array('order_number' => $order_number))->find();
$this->order = $order;
$this->display('pay_ok');
}
}