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
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
|
13
|
|
|
abstract class AbstractWechat implements PaymentInterface |
14
|
|
|
{ |
15
|
|
|
use RequestTrait; |
16
|
|
|
use SecurityTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var \Illuminate\Config\Repository |
20
|
|
|
*/ |
21
|
|
|
protected $config; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* AbstractWechat constructor. |
25
|
|
|
* |
26
|
|
|
* @param \Illuminate\Config\Repository $config |
27
|
|
|
*/ |
28
|
3 |
|
public function __construct(Repository $config) |
29
|
|
|
{ |
30
|
3 |
|
$this->config = $config; |
31
|
3 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
|
|
public function getClientIp() |
37
|
|
|
{ |
38
|
|
|
return Request::createFromGlobals()->server->get('SERVER_ADDR'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Unified order. |
43
|
|
|
* |
44
|
|
|
* @param string $gateway |
45
|
|
|
* @param array $payload |
46
|
|
|
* |
47
|
|
|
* @return \Illuminate\Support\Collection |
48
|
|
|
* |
49
|
|
|
* @throws \Nilnice\Payment\Exception\GatewayException |
50
|
|
|
* @throws \InvalidArgumentException |
51
|
|
|
* @throws \Nilnice\Payment\Exception\InvalidKeyException |
52
|
|
|
* @throws \Nilnice\Payment\Exception\InvalidSignException |
53
|
|
|
*/ |
54
|
3 |
|
protected function prepare(string $gateway, array $payload) : Collection |
55
|
|
|
{ |
56
|
3 |
|
if ($payload['trade_type'] === Constant::WX_PAY_APP_TYPE) { |
57
|
|
|
$key = $this->config->get('app_key'); |
58
|
|
|
} else { |
59
|
3 |
|
$key = $this->config->get('key'); |
60
|
|
|
} |
61
|
3 |
|
$payload['sign'] = self::generateSign($payload, $key); |
62
|
|
|
|
63
|
|
|
return $this->send($gateway, $payload, $key); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|