Completed
Push — master ( bbcd34...fabc9b )
by i
03:49
created

PubPayment::toPay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 2
dl 0
loc 17
ccs 0
cts 15
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Nilnice\Payment\Wechat;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use Nilnice\Payment\Constant;
8
use Nilnice\Payment\Log;
9
10
class PubPayment extends AbstractWechat
11
{
12
    /**
13
     * Use pub to pay for order.
14
     *
15
     * @param string $gateway
16
     * @param array  $payload
17
     *
18
     * @return \Illuminate\Support\Collection
19
     *
20
     * @throws \InvalidArgumentException
21
     * @throws \Nilnice\Payment\Exception\GatewayException
22
     * @throws \Nilnice\Payment\Exception\InvalidKeyException
23
     * @throws \Nilnice\Payment\Exception\InvalidSignException
24
     */
25
    public function toPay(string $gateway, array $payload) : Collection
26
    {
27
        $payload['trade_type'] = Constant::WX_PAY_PUB_TYPE;
28
        $key = $this->config->get('key');
29
        $object = $this->prepare(Constant::WX_PAY_PREPARE, $payload);
30
        $parameter = [
31
            'appId'     => $payload['appid'],
32
            'timeStamp' => (string)time(),
33
            'nonceStr'  => Str::random(),
34
            'package'   => 'prepay_id=' . $object->get('prepay_id'),
35
            'signType'  => 'MD5',
36
        ];
37
        $parameter['paySign'] = self::generateSign($parameter, $key);
38
39
        Log::debug('Pub order:', [$gateway, $payload, $parameter]);
40
41
        return new Collection($parameter);
42
    }
43
}
44