AliwapService.php
5.57 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace Plugin\Pay\aliwap;
use Plugin\Pay\aliwap\AliwapConfig;
use Plugin\Pay\aliwap\AliwapReqparams;
use Plugin\Pay\aliwap\AliwapRspparams;
use Api\Yohobuy;
use Plugin\Helpers;
/**
* 支付宝手机网页支付
*/
class AliwapService
{
var $config;
var $alipay_config;
public function __construct()
{
$this->logProjectPrefix = 'aliwap';
$this->config = new AliwapConfig();
$this->alipay_config = array(
'partner' => $this->config->partner,
'key' => $this->config->alipay_key,
'sign_type' => 'MD5',
'input_charset' => 'utf-8',
'cacert' => dirname(__FILE__) . '/cacert.pem',
'transport' => 'http'
);
require_once("lib/alipay_core.function.php");
require_once("lib/alipay_rsa.function.php");
require_once("lib/alipay_md5.function.php");
require_once("lib/alipay_notify.class.php");
require_once("lib/alipay_submit.class.php");
}
/**
* 构造请求参数
* @see QPay_Utils_Interface::getPayRequestPars()
*/
public function getPayRequestPars(AliwapReqparams $params)
{
//构造要请求的参数数组,无需改动
$parameter = array(
'service' => $this->config->service,
'partner' => $this->config->partner,
'_input_charset' => $this->config->input_charset,
'notify_url' => Yohobuy::SERVICE_NOTIFY . $this->config->notify_url,
'return_url' => Helpers::transHttpsUrl(Helpers::url($this->config->return_url)),
/* 业务参数 */
'payment_type' => $this->config->payment_type,
'seller_id' => $this->config->partner,
'it_b_pay' => $params->payExpireMinute . 'm',
'payment_type' => $this->config->payment_type,
'out_trade_no' => $params->orderCode,
'subject' => $params->goodsName,
'total_fee' => $params->totalFee / 100, //单位为元
"show_url" => Helpers::transHttpsUrl(Helpers::url('')),
);
ksort($parameter);
reset($parameter);
$param = '';
$sign = '';
$parameter = Helpers::paraFilter($parameter);
foreach ($parameter AS $key => $val) {
$param .= "$key=" . urlencode($val) . "&";
$sign .= "$key=$val&";
}
$param = substr($param, 0, -1);
$sign = substr($sign, 0, -1) . $this->config->alipay_key;
$result = array(
'pay_url' => $this->config->pay_url,
'pars' => $param . "&sign=" . md5($sign) . "&sign_type=" . $this->config->sign_type,
'reqType' => 'get'
);
return $result;
}
/**
* 解析结果
*
* @see AliwapInterface::parseResponse()
*/
public function parseResponse(Array $arrResponse)
{
$rsp = new AliwapRspparams();
if ($arrResponse['type'] == 'return') {
//return 返回
$alipayNotify = new \AlipayNotify($this->alipay_config);
$verify_result = $alipayNotify->verifyReturn();
if ($verify_result) {
//商户订单号
$rsp->orderCode = $_GET['out_trade_no'];
$rsp->payResult = 200;
//支付宝交易号
$trade_no = $_GET['trade_no'];
} else {
$rsp->payResult = 400;
}
} else {
//服务器返回
$alipayNotify = new \AlipayNotify($this->alipay_config);
$verify_result = $alipayNotify->verifyNotify();
if ($verify_result) {
//解密(如果是RSA签名需要解密,如果是MD5签名则下面一行清注释掉)
// $notify_data = $alipayNotify->decrypt($_POST['notify_data']);
//解析notify_data
//注意:该功能PHP5环境及以上支持,需开通curl、SSL等PHP配置环境。建议本地调试时使用PHP开发软件
$doc = new \DOMDocument();
$doc->loadXML($_POST['notify_data']);
if (!empty($doc->getElementsByTagName("notify")->item(0)->nodeValue)) {
//商户订单号
$out_trade_no = $doc->getElementsByTagName("out_trade_no")->item(0)->nodeValue;
//支付宝交易号
$trade_no = $doc->getElementsByTagName("trade_no")->item(0)->nodeValue;
//交易状态
$trade_status = $doc->getElementsByTagName("trade_status")->item(0)->nodeValue;
if ($trade_status == 'TRADE_FINISHED' || $trade_status == 'TRADE_SUCCESS') {
$rsp->orderCode = $out_trade_no;
$rsp->payResult = 200;
} else {
$rsp->payResult = 400;
}
}
} else {
$rsp->payResult = 401;
}
}
return $rsp;
}
private function getTokenReqData($out_trade_no, $subject, $total_fee)
{
return '<direct_trade_create_req><notify_url>' . $this->config->notify_url .
'</notify_url><call_back_url>' . $this->config->return_url .
'</call_back_url><seller_account_name>' . $this->config->sellerMail .
'</seller_account_name><out_trade_no>' . $out_trade_no . '</out_trade_no><subject>' . 'yoho order:' . $out_trade_no .
'</subject><total_fee>' . $total_fee . '</total_fee><merchant_url>' . $this->config->merchant_url . $out_trade_no .
'</merchant_url></direct_trade_create_req>';
}
}