Service.php
5.65 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
151
152
153
154
155
156
157
158
<?php
namespace WebPlugin\Pay\Shengpay;
use WebPlugin\Pay\PayAbstract;
use WebPlugin\Pay\Reqparams;
use WebPlugin\Pay\Rspparams;
use WebPlugin\UdpLog;
class Service extends PayAbstract
{
public $config;
public function __construct(array $paymentParams)
{
$this->logProjectPrefix = 'shengpay';
$this->config = new Config();
$myConfig = json_decode($paymentParams["pay_params"]);
$this->config->merchant_no = $myConfig->merchant_id;
$this->config->merchant_key = $myConfig->merchant_key;
}
/**
* @param Reqparams $params
* @return array|void
*/
public function getPayRequestPars(Reqparams $params)
{
UdpLog::info("【{$this->logProjectPrefix}-支付】,function:getPayRequestPars,参数", $params);
$bankCode = $params->paymentParameter == 'platform' ? '' : $params->paymentParameter;
$baseUrl = $this->getBaseNoticeUrl($params->isTest);
$parameters = array(
'Version' => $this->config->version,
'Amount' => sprintf("%.2f", $params->totalFee * 0.01),
'OrderNo' => $params->orderCode,
'MerchantNo' => $this->config->merchant_no,
'PayChannel' => $this->config->pay_channel,
'PostBackUrl' => $baseUrl . $this->config->post_back_url,
'NotifyUrl' => $baseUrl . $this->config->notify_url,
'BackUrl' => $this->config->back_url,
'OrderTime' => date('YmdHis', $params->orderTime),
'CurrencyType' => $this->config->currency_type,
'NotifyUrlType' => $this->config->notify_url_type,
'SignType' => $this->config->sign_type,
'BankCode' => $bankCode //银行代码
);
$parameters["MAC"] = $this->getSign($parameters);
$isDirect = empty($bankCode) ? false : TRUE;
$result = array(
'pay_url' => $this->getPayUrl($isDirect, $params->isTest),
'pars' => $parameters,
'reqType' => 'post'
);
UdpLog::info("【{$this->logProjectPrefix}-支付】,orderCode:{$params->orderCode},function:getPayRequestPars,处理结果", $result);
return $result;
}
/**
* 获取支付地址
* Enter description here ...
* @param bool $isDirect 是否为直连
* @param bool $isTest 是否为测试环境
* @return string
*/
public function getPayUrl($isDirect, $isTest)
{
if ($isDirect) {
if ($isTest) {
return $this->config->direct_pay_url_test;
} else {
return $this->config->direct_pay_url;
}
} else {
if ($isTest) {
return $this->config->pay_url_test;
} else {
return $this->config->pay_url;
}
}
}
/**
* 获取签名
* @param array $pars
* @return string
*/
public function getSign(array $pars)
{
$strPars = $pars['Version'] . $pars['Amount'] . $pars['OrderNo'] . $pars['MerchantNo'] . $pars['PayChannel']
. $pars['PostBackUrl'] . $pars['NotifyUrl'] . $pars['BackUrl'] . $pars['OrderTime'] . $pars['CurrencyType']
. $pars['NotifyUrlType'] . $pars['SignType'] . $pars['BankCode'];
return md5($strPars . $this->config->merchant_key);
}
/**
* 解析结果
* @param array $arrResponse
* @return void|Rspparams
*/
function parseResponse(Array $arrResponse)
{
UdpLog::info("【{$this->logProjectPrefix}-支付】,function:parseResponse,参数", $arrResponse);
$rsp = new Rspparams();
if (!$this->checkResponse($arrResponse)) {
//验证不成功
$rsp->payResult = -1;
} else {
$rsp->bankName = "";
$rsp->orderCode = $arrResponse["OrderNo"];
$rsp->payResult = $this->convertResult($arrResponse["Status"]);
$rsp->payTime = time();
$rsp->totalFee = $arrResponse["Amount"];
$rsp->resultMsg = $arrResponse["ExInfo"];
//添加支付订单号和交易号
$rsp->payOrderCode = $arrResponse["orderNo"];
$rsp->tradeNo = "";
$rsp->bankBillNo = "";
}
UdpLog::info("【{$this->logProjectPrefix}-支付】,function:parseResponse,处理结果", $rsp);
return $rsp;
}
/**
* 验证回复的正确性
* @param array $arrResponse
* @return bool|void
*/
protected function checkResponse(array $arrResponse)
{
$strPars = $arrResponse["Amount"] . '|' . $arrResponse['PayAmount'] . '|' . $arrResponse['OrderNo'] . '|' . $arrResponse['serialno'] .
'|' . $arrResponse['Status'] . '|' . $arrResponse['MerchantNo'] . '|' . $arrResponse['PayChannel'] . '|' . $arrResponse['Discount']
. '|' . $arrResponse["SignType"] . '|' . $arrResponse['PayTime'] . '|' . $arrResponse['CurrencyType'] . '|' . $arrResponse['ProductNo']
. '|' . $arrResponse['ProductDesc'] . '|' . $arrResponse['Remark1'] . '|' . $arrResponse['Remark2'] . '|' . $arrResponse['ExInfo'];
$vaildSign = md5($strPars . '|' . $this->config->merchant_key);
if (strtoupper($vaildSign) == $arrResponse["MAC"]) {
return true;
}
return false;
}
/**
* @param $resultCode
* @return int|void
*/
protected function convertResult($resultCode)
{
if ($resultCode == "01") //20为支付成功,30为支付失败
{
return 200;
}
return $resultCode;
}
}