MpGateway.php
1.5 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
<?php
namespace Yansongda\Pay\Gateways\Wechat;
use Exception;
use Yansongda\Pay\Events;
use Yansongda\Pay\Exceptions\GatewayException;
use Yansongda\Pay\Exceptions\InvalidArgumentException;
use Yansongda\Pay\Exceptions\InvalidSignException;
use Yansongda\Supports\Collection;
use Yansongda\Supports\Str;
class MpGateway extends Gateway
{
/**
* @var bool
*/
protected $payRequestUseSubAppId = false;
/**
* Pay an order.
*
* @author yansongda <me@yansongda.cn>
*
* @param string $endpoint
*
* @throws GatewayException
* @throws InvalidArgumentException
* @throws InvalidSignException
* @throws Exception
*/
public function pay($endpoint, array $payload): Collection
{
$payload['trade_type'] = $this->getTradeType();
$pay_request = [
'appId' => !$this->payRequestUseSubAppId ? $payload['appid'] : $payload['sub_appid'],
'timeStamp' => strval(time()),
'nonceStr' => Str::random(),
'package' => 'prepay_id='.$this->preOrder($payload)->get('prepay_id'),
'signType' => 'MD5',
];
$pay_request['paySign'] = Support::generateSign($pay_request);
Events::dispatch(new Events\PayStarted('Wechat', 'JSAPI', $endpoint, $pay_request));
return new Collection($pay_request);
}
/**
* Get trade type config.
*
* @author yansongda <me@yansongda.cn>
*/
protected function getTradeType(): string
{
return 'JSAPI';
}
}