Service.php
4.69 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
<?php
namespace WebPlugin\Pay\Tenpay;
use WebPlugin\Pay\PayAbstract;
use WebPlugin\Pay\Reqparams;
use WebPlugin\Pay\Rspparams;
use WebPlugin\PhpLog;
class Service extends PayAbstract
{
public $config;
private $log;
/**
* Service constructor.
* @param array $paymentParams
*/
public function __construct(array $paymentParams)
{
$this->logProjectPrefix = 'tenpay';
$this->config = new Config();
$myConfig = json_decode($paymentParams["pay_params"]);
$this->config->bargainor_id = $myConfig->merchant_id;
$this->config->sp_key = $myConfig->merchant_key;
$this->log = new PhpLog($this->config->logDir, 'PRC', $this->config->logLevel);
}
/**
* @param Reqparams $params
* @return array
*/
public function getPayRequestPars(Reqparams $params)
{
$this->log->LogInfo("===开始处理财付通的请求参数===");
$this->log->LogInfo("-----请求参数为---");
$this->log->LogInfo(var_export($params, true));
parent::getPayRequestPars($params);
$baseUrl = $this->getBaseNoticeUrl($params->isTest);
$reqData = array();
$sign_text = "cmdno=" . $this->config->cmdno;
$sign_text .= "&date=" . date('Ymd');
// $sign_text .= "&bank_type=0";
$goodName = urlencode(iconv("UTF-8", "GB2312", $params->goodsName));
$sign_text .= "&bargainor_id=" . $this->config->bargainor_id;
$sign_text .= "&transaction_id=" . $this->config->bargainor_id . date('Ymd') . date('mdHis');
$sign_text .= "&sp_billno=" . $params->orderCode; //内部订单号
$sign_text .= "&total_fee=" . $params->totalFee;
$sign_text .= "&fee_type=" . $this->config->fee_type;
$sign_text .= "&return_url=" . $baseUrl . $this->config->return_url;
$sign_text .= "&attach=" . $params->orderCode;
if (!empty($params->spbill_create_ip)) {
$sign_text .= "&spbill_create_ip=" . $params->spbill_create_ip;
}
$sign = strtoupper(md5($sign_text . "&key=" . $this->config->sp_key));
$sign_text = $sign_text . "&sign=" . $sign . "&desc=" . $goodName . "&bank_type=0&cs=gb2312";
$result = array(
'pay_url' => $this->config->pay_url,
'pars' => $sign_text,
'reqType' => 'get'
);
return $result;
}
/**
* 解析返回的参数
* @param Array $arrResponse
* @return void|Rspparams
*/
public function parseResponse(array $arrResponse)
{
$this->log->LogInfo("===开始处理财付通的回调参数===");
$this->log->LogInfo("-----回调参数为---");
$this->log->LogInfo(var_export($arrResponse, true));
$rsp = new Rspparams();
if (!$this->checkResponse($arrResponse)) {
//验证不成功
$rsp->payResult = 400;
} else {
$rsp->bankName = "";
$rsp->orderCode = $arrResponse["sp_billno"];
$rsp->payResult = $this->convertResult($arrResponse["pay_result"]);
$rsp->payTime = $arrResponse["pay_time"];
$rsp->totalFee = round($arrResponse["total_fee"], 2) / 100; //返回的是分,要转化成元
$rsp->resultMsg = $arrResponse['pay_info'];
//添加支付订单号和交易号
$rsp->payOrderCode = $arrResponse["sp_billno"];
$rsp->tradeNo = "";
$rsp->bankBillNo = "";
}
return $rsp;
}
/**
* @param $resultCode
* @return int|void
*/
protected function convertResult($resultCode)
{
if ($resultCode == 0) {
return 200;
}
return $resultCode;
}
/**
* 验证回复的正确性
* @param array $arrResponse
* @return bool|void
*/
protected function checkResponse(array $arrResponse)
{
$strResponseText = "cmdno=" . $arrResponse["cmdno"];
$strResponseText .= "&pay_result=" . $arrResponse["pay_result"];
$strResponseText .= "&date=" . $arrResponse["date"];
$strResponseText .= "&transaction_id=" . $arrResponse["transaction_id"];
$strResponseText .= "&sp_billno=" . $arrResponse["sp_billno"];
$strResponseText .= "&total_fee=" . $arrResponse["total_fee"];
$strResponseText .= "&fee_type=" . $arrResponse["fee_type"];
$strResponseText .= "&attach=" . $arrResponse["attach"];
$strResponseText .= "&key=" . $this->config->sp_key;
if (strtoupper(md5($strResponseText)) == $arrResponse["sign"]) {
return true;
}
return false;
}
}