WeChatRepository.php
9.63 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
namespace App\Repositories\Customers;
use App\Common\Enums\HtTab;
use App\Exceptions\Api\ApiException;
use App\Modules\Enums\ErrorCode;
use App\Modules\Models\Customer\Customer;
use App\Modules\Models\Machine\Machine;
use App\Modules\Models\Rent\Rent;
use App\Modules\Models\Spot\Spot;
use App\Modules\Repositories\BaseRepository;
use Yansongda\LaravelPay\Facades\Pay;
use Yansongda\Pay\Exceptions\GatewayException;
use Yansongda\Pay\Exceptions\InvalidConfigException;
use Yansongda\Pay\Exceptions\InvalidSignException;
use Illuminate\Support\Facades\Log;
/**
* Class EloquentHistoryRepository.
*/
class WeChatRepository extends BaseRepository
{
public function submitOrderToWeChat($type, $order_obj, $openid)
{
$order = [];
switch ($type) {
case 1:
$order['out_trade_no'] = $order_obj->rent_no;
$order['body'] = '灰兔智能租借押金';
$order['total_fee'] = (int)($order_obj->deposit * $order_obj->number);
$order['openid'] = $openid;
$order['attach'] = json_encode(['type' => $type]);
break;
case 2:
$order['out_trade_no'] = $order_obj->guide_no;
$order['body'] = '微导览服务费';
$order['total_fee'] = $order_obj->real_total;
$order['openid'] = $openid;
$order['attach'] = json_encode(['type' => $type]);
break;
default :
break;
}
try {
// $pay = Pay::wechat()->miniapp($order, ['notify_url' => 'https://api.playxx.cn/api/v1/customers/weGuidePayCallBack']);
// $pay = Pay::wechat()->miniapp($order, ['notify_url' => config('wechat.payment.default.notify_url')]);
$pay = Pay::wechat()->miniapp($order, ['notify_url' => 'https://api.ssw-htzn.com/api/v1/customers/weGuidePayCallBack']);
Log::info("[Pay::wechat()->miniapp]" . $pay);
} catch (GatewayException $e) {
throw new ApiException(ErrorCode::WE_MINIAPP_PAY_FAIL, $e->getMessage());
} catch (InvalidSignException $e) {
throw new ApiException(ErrorCode::WE_MINIAPP_PAY_FAIL, $e->getMessage());
} catch (InvalidConfigException $e) {
throw new ApiException(ErrorCode::WE_MINIAPP_PAY_FAIL, $e->getMessage());
}
$res = [
'timeStamp' => $pay->timeStamp,
'nonceStr' => $pay->nonceStr,
'package' => $pay->package,
'paySign' => $pay->paySign,
'order_no' => $order['out_trade_no'],
];
$order_obj->prepay_id = (string)explode('=', $pay->package)[1];
$order_obj->save();
return $res;
}
public function rentRefund($rent_obj)
{
$order = [
'out_refund_no' => $this->createRefoundNo(),
'out_trade_no' => $rent_obj->rent_no,
'total_fee' => $rent_obj->deposit * $rent_obj->number,
'refund_fee' => $rent_obj->deposit * $rent_obj->number,
];
$this->doRefund($order);
return $order['out_refund_no'];
}
public function productOutFailRefund($rent_obj, $refund_no)
{
$data = [
'out_refund_no' => $refund_no,
'out_trade_no' => $rent_obj->rent_no,
'total_fee' => $rent_obj->deposit * $rent_obj->number,
'refund_fee' => $rent_obj->deposit,
];
$this->doRefund($data);
}
/**
* @param $data
* @return bool
* @throws ApiException
*/
public function doRefund($data)
{
$order = [
'out_refund_no' => $data['out_refund_no'],
'out_trade_no' => $data['out_trade_no'],
'total_fee' => $data['total_fee'],
'refund_fee' => $data['refund_fee'],
'type' => 'miniapp',
];
try {
Log::info("[doRefund] start >>>>");
$re = Pay::wechat()->refund($order);
Log::info("[doRefund] params:" . json_encode($re));
} catch (GatewayException $e) {
throw new ApiException(ErrorCode::WE_REfUND_FAIL, $e->getMessage());
} catch (InvalidSignException $e) {
throw new ApiException(ErrorCode::WE_REfUND_FAIL, $e->getMessage());
}
return true;
}
/**
* 生成订单单号
*/
function createRefoundNo()
{
return HtTab::RENT_REFUND . date('Ymd') . str_pad(mt_rand(1, 9999999999), 10, '0', STR_PAD_LEFT);
}
function getCustomer($id)
{
$customer = Customer::where('id', $id)->first();
if ($customer == null) {
throw new ApiException(ErrorCode::USER_NOT_EXIST, trans("api.error.user_not_exist"));
}
return $customer;
}
function rentSuccessMes($rent_obj)
{
$customer = $this->getCustomer($rent_obj->customer_id);
$spot = Spot::where('id', $rent_obj->spot_id)->first();
$app = app('wechat.mini_program');
$app->template_message->send([
'touser' => $customer->mini_program_open_id,
'template_id' => 'ivyzt64NFH5t_0TJGq8s632yob7YPNHez-kApQNrLeg',
'form_id' => $rent_obj->prepay_id,
'data' => [
'keyword1' => $rent_obj->pay_time,
'keyword2' => $spot->spotname,
'keyword3' => $rent_obj->rent_no,
'keyword4' => HtTab::RENT_PRODUCTION_NAME,
'keyword5' => round($rent_obj->deposit / 100, 2) . '/个',
'keyword6' => round($rent_obj->deposit * $rent_obj->number / 100, 2) . '元',
'keyword7' => feeMes($rent_obj->free_time, $rent_obj->one_day_price),
],
]);
}
function subscribeSuccessMes($rent_obj)
{
$customer = $this->getCustomer($rent_obj->customer_id);
$spot = Spot::where('id', $rent_obj->spot_id)->first();
$app = app('wechat.mini_program');
$app->template_message->send([
'touser' => $customer->mini_program_open_id,
'template_id' => 'XtZN0uijQ1AqQ-p2dtZ60JURu3w3oRtTDfdMxm0ayF4',
'form_id' => $rent_obj->prepay_id,
'data' => [
'keyword1' => HtTab::RENT_PRODUCTION_NAME,
'keyword2' => $rent_obj->rent_no,
'keyword3' => $spot->spotname,
'keyword4' => round($rent_obj->deposit * $rent_obj->number / 100, 2) . '元(押金)',
'keyword5' => $rent_obj->pay_time,
'keyword6' => feeMes($rent_obj->free_time, $rent_obj->one_day_price),
'keyword7' => $rent_obj->number,
],
]);
}
function rentFailMes($rent_obj)
{
$customer = $this->getCustomer($rent_obj->customer_id);
$spot = Spot::where('id', $rent_obj->spot_id)->first();
$app = app('wechat.mini_program');
$app->template_message->send([
'touser' => $customer->mini_program_open_id,
'template_id' => 'iwn_y0-HhxTsEIZQghh6KaDEuXImeJsaQrCseapjBO4',
'form_id' => $rent_obj->prepay_id,
'data' => [
'keyword1' => $rent_obj->rent_no,
'keyword2' => $rent_obj->pay_time,
'keyword3' => $spot->spotname,
'keyword4' => '押金已按原微信支付路径退回',
],
]);
}
function subscribeFailMes($rent_obj)
{
$customer = $this->getCustomer($rent_obj->customer_id);
$spot = Spot::where('id', $rent_obj->spot_id)->first();
$app = app('wechat.mini_program');
$app->template_message->send([
'touser' => $customer->mini_program_open_id,
'template_id' => '8eGgkk8Wxud8_pgkkYQWLELhBvB8-R68A1GQzXx6Iqk',
'form_id' => $rent_obj->prepay_id,
'data' => [
'keyword1' => HtTab::RENT_PRODUCTION_NAME,
'keyword2' => $rent_obj->rent_no,
'keyword3' => $spot->spotname,
'keyword4' => round($rent_obj->deposit * $rent_obj->number / 100, 2) . '元',
'keyword5' => $rent_obj->pay_time,
'keyword6' => '刚被预约完',
],
]);
}
function shipmentMes($rent_obj, $success_hatchs)
{
$customer = $this->getCustomer($rent_obj->customer_id);
$spot = Spot::where('id', $rent_obj->spot_id)->first();
$app = app('wechat.mini_program');
$app->template_message->send([
'touser' => $customer->mini_program_open_id,
'template_id' => '1qugdEt19bGstbESUBsB8AORgVMAmpcT5DaomMqhPG0',
'form_id' => $rent_obj->prepay_id,
'data' => [
'keyword1' => $rent_obj->rent_no,
'keyword2' => $spot->spotname,
'keyword3' => HtTab::RENT_PRODUCTION_NAME . '*' . count($success_hatchs),
],
]);
}
function returnAllMes($rent_obj)
{
if(!$rent_obj->is_over){
return false;
}
$customer = $this->getCustomer($rent_obj->customer_id);
$spot = Spot::where('id', $rent_obj->spot_id)->first();
$app = app('wechat.mini_program');
$app->template_message->send([
'touser' => $customer->mini_program_open_id,
'template_id' => 'J7OrbZULpeSi5Ldnao0Pf5v9MZDBLlwZCAZkcuwxUNo',
'form_id' => $rent_obj->prepay_id,
'data' => [
'keyword1' => HtTab::RENT_PRODUCTION_NAME,
'keyword2' => $rent_obj->rent_no,
'keyword3' => $spot->spotname,
'keyword4' => round($rent_obj->real_total / 100, 2) . '元',
'keyword5' => feeMes($rent_obj->free_time, $rent_obj->one_day_price),
],
]);
return true;
}
}