Service.php
4.64 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
<?php
namespace WebPlugin\Pay\Wechatqrcode;
use WebPlugin\Pay\PayAbstract;
use WebPlugin\Pay\Reqparams;
use WebPlugin\Pay\Rspparams;
use WebPlugin\Pay\Signature;
use WebPlugin\Pay\weixin\lib\WxPayApi;
use WebPlugin\Pay\weixin\lib\WxPayOrderQuery;
use WebPlugin\PhpLog;
use WebPlugin\Helpers;
class Service extends PayAbstract
{
private $config;
private $orderCode;
private $paymentCode;
private $appKey;
private $privateKey;
private $payCurl;
private $log;
public function __construct(array $paymentParams)
{
$this->logProjectPrefix = 'wechatpay';
$this->config = new Config();
$environParam = 'pay_' . APPLICATION_ENV . '_url';
$this->payCurl = $this->config->$environParam;
$this->appKey = $this->config->app_key;
$this->privateKey = $this->config->private_key;
$this->paymentCode = $paymentParams['id'];
$this->log = new PhpLog($this->config->logDir, 'PRC', $this->config->logLevel);
}
public function getPayRequestPars(Reqparams $params)
{
$this->orderCode = $params->orderCode;
$result = array(
'pay_url' => Helpers::url('/pay/wechatqrcode'),
'pars' => 'order_code=' . $this->orderCode . '&payment_code=' . $this->paymentCode,
'reqType' => 'get'
);
return $result;
}
/**
* 微信支付页
* @param array $orderInfo
* @return string
* @internal param array $orderCode
*/
public function pay(array $orderInfo)
{
$this->log->LogInfo("===开始处理微信扫码支付的请求参数===");
$this->log->LogInfo("-----请求参数为---");
$this->log->LogInfo(var_export($orderInfo, true));
//生成签名
$this->orderCode = $orderInfo['order_code'];
$payParams = array(
'order_code' => $this->orderCode,
'app_key' => $this->appKey,
'payment_code' => $this->paymentCode,
'private_key' => $this->privateKey
);
$_params = Signature::packageSort($payParams);
$client_secret = Signature::makeSign($_params);
$parameter = array(
'order_code' => $this->orderCode,
'app_key' => $this->appKey,
//这里payment.yoho_pay表与payment.q_pay表数据需一致
'payment_code' => $this->paymentCode,
'client_secret' => $client_secret,
'client' => 'web'
);
$pars = '';
foreach ($parameter as $p_key => $p_val) {
$pars .= $p_key . '=' . urlencode($p_val) . '&';
}
$payUrlInfo = array(
'pay_url' => $this->payCurl,
'pars' => trim($pars, '&')
);
$this->log->LogInfo("===开始处理微信扫码支付的支付地址===");
$this->log->LogInfo("-----支付地址数据为---");
$this->log->LogInfo(var_export($payUrlInfo, true));
$payUrl = $payUrlInfo['pay_url'] . '?' . $payUrlInfo['pars'];
return array('pay_url' => $payUrl);
}
public function parseResponse(array $data)
{
/* 返回示例
* http://www.yohobuy.com/pay/notice/wechatqrcodereturn?ordercode=93465435
*/
$this->log->LogInfo("===开始查询微信扫码支付的结果===");
// 组装微信支付的订单号
$tradeNo = 'YOHOBuy_' . $data['orderCode'];
$input = new WxPayOrderQuery();
$input->SetOut_trade_no($tradeNo);
$result = WxPayApi::orderQuery($input);
$this->log->LogInfo("===查询微信扫码支付的结果结束,结果为===");
$this->log->LogInfo(var_export($result, true));
$rsp = new Rspparams();
// 支付成功
if (isset($result['trade_state']) && $result['trade_state'] === 'SUCCESS') {
$rsp->payResult = 200;
$rsp->bankName = "WX";
$rsp->orderCode = $data['orderCode'];
$rsp->payTime = $result["time_end"];
$rsp->totalFee = $result["total_fee"] / 100;
$rsp->resultMsg = '支付成功';
//添加支付订单号和交易号
$rsp->payOrderCode = $data["orderCode"];
$rsp->tradeNo = $result["out_trade_no"];
$rsp->bankBillNo = $result['bank_type'];
} else {
$this->log->LogInfo("===微信扫码支付失败===");
$rsp->payResult = -1;
}
$this->log->LogInfo("===微信扫码支付回调参数解析结果为===");
$this->log->LogInfo(var_export($rsp, true));
return $rsp;
}
}