|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nilnice\Payment\Wechat; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Config\Repository; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Nilnice\Payment\Constant; |
|
8
|
|
|
use Nilnice\Payment\PaymentInterface; |
|
9
|
|
|
use Nilnice\Payment\Wechat\Traits\RequestTrait; |
|
10
|
|
|
use Nilnice\Payment\Wechat\Traits\SecurityTrait; |
|
11
|
|
|
|
|
12
|
|
|
abstract class AbstractWechat implements PaymentInterface |
|
13
|
|
|
{ |
|
14
|
|
|
use SecurityTrait; |
|
15
|
|
|
use RequestTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var \Illuminate\Config\Repository |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $config; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* AbstractWechat constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param \Illuminate\Config\Repository $config |
|
26
|
|
|
*/ |
|
27
|
3 |
|
public function __construct(Repository $config) |
|
28
|
|
|
{ |
|
29
|
3 |
|
$this->config = $config; |
|
30
|
3 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Pregenerating order. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $gateway |
|
36
|
|
|
* @param array $payload |
|
37
|
|
|
* |
|
38
|
|
|
* @return \Illuminate\Support\Collection |
|
39
|
|
|
* @throws \Nilnice\Payment\Exception\GatewayException |
|
40
|
|
|
* @throws \InvalidArgumentException |
|
41
|
|
|
* @throws \Nilnice\Payment\Exception\InvalidKeyException |
|
42
|
|
|
* @throws \Nilnice\Payment\Exception\InvalidSignException |
|
43
|
|
|
* @throws \RuntimeException |
|
44
|
|
|
*/ |
|
45
|
3 |
|
protected function prepare(string $gateway, array $payload) : Collection |
|
46
|
|
|
{ |
|
47
|
3 |
|
$env = $this->config->get('env', 'pro'); |
|
48
|
3 |
|
$key = $this->config->get('key'); |
|
49
|
3 |
|
$payload['sign'] = self::generateSign($payload, $key); |
|
50
|
|
|
$gateway = self::getGatewayUrl($env) . $gateway; |
|
51
|
|
|
|
|
52
|
|
|
return $this->send($gateway, $payload, $key); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get gateway url. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $env |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
private static function getGatewayUrl($env = '') : string |
|
63
|
|
|
{ |
|
64
|
|
|
$uri = ''; |
|
65
|
|
|
switch ($env) { |
|
66
|
|
|
case 'pro': |
|
67
|
|
|
$uri = Constant::WX_PAY_PRO_URI; |
|
68
|
|
|
break; |
|
69
|
|
|
case 'dev': |
|
70
|
|
|
$uri = Constant::WX_PAY_DEV_URI; |
|
71
|
|
|
break; |
|
72
|
|
|
case 'hk': |
|
73
|
|
|
$uri = Constant::WX_PAY_PRO_HK_URI; |
|
74
|
|
|
break; |
|
75
|
|
|
default: |
|
76
|
|
|
break; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $uri; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|