PayumWrapper::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Recca0120\LaravelPayum;
4
5
use Payum\Core\Payum;
6
use Illuminate\Http\Request;
7
use Payum\Core\Model\Payment;
8
use Payum\Core\Request\GetHumanStatus;
9
10
class PayumWrapper
11
{
12
    /**
13
     * $gatewayName.
14
     *
15
     * @var string
16
     */
17
    public $gatewayName;
18
    /**
19
     * $payum.
20
     *
21
     * @var \Payum\Core\Payum
22
     */
23
    protected $payum;
24
25
    /**
26
     * __construct.
27
     *
28
     * @param \Payum\Core\Payum $payum
29
     * @param string $gatewayName
30
     */
31 8
    public function __construct(Payum $payum, $gatewayName)
32
    {
33 8
        $this->payum = $payum;
34 8
        $this->gatewayName = $gatewayName;
35 8
    }
36
37
    /**
38
     * getPayum.
39
     *
40
     * @return \Payum\Core\Payum
41
     */
42 6
    public function getPayum()
43
    {
44 6
        return $this->payum;
45
    }
46
47
    /**
48
     * authorize.
49
     *
50
     * @param callable $callback
51
     * @param string $afterPath
52
     * @param array $afterParameters
53
     * @return string
54
     */
55 1
    public function authorize(callable $callback, $afterPath = 'payum.done', array $afterParameters = [])
56
    {
57 1
        return $this->send('authorize', $callback, $afterPath, $afterParameters);
58
    }
59
60
    /**
61
     * cancel.
62
     *
63
     * @param callable $callback
64
     * @param string $afterPath
65
     * @param array $afterParameters
66
     * @return string
67
     */
68 1
    public function cancel(callable $callback, $afterPath = 'payum.done', array $afterParameters = [])
69
    {
70 1
        return $this->send('cancel', $callback, $afterPath, $afterParameters);
71
    }
72
73
    /**
74
     * capture.
75
     *
76
     * @param callable $callback
77
     * @param string $afterPath
78
     * @param array $afterParameters
79
     * @return string
80
     */
81 1
    public function capture(callable $callback, $afterPath = 'payum.done', array $afterParameters = [])
82
    {
83 1
        return $this->send('capture', $callback, $afterPath, $afterParameters);
84
    }
85
86
    /**
87
     * refund.
88
     *
89
     * @param callable $callback
90
     * @param string $afterPath
91
     * @param array $afterParameters
92
     * @return string
93
     */
94 1
    public function refund(callable $callback, $afterPath = 'payum.done', array $afterParameters = [])
95
    {
96 1
        return $this->send('refund', $callback, $afterPath, $afterParameters);
97
    }
98
99
    /**
100
     * payout.
101
     *
102
     * @param callable $callback
103
     * @param string $afterPath
104
     * @param array $afterParameters
105
     * @return string
106
     */
107 1
    public function payout(callable $callback, $afterPath = 'payum.done', array $afterParameters = [])
108
    {
109 1
        return $this->send('payout', $callback, $afterPath, $afterParameters);
110
    }
111
112
    /**
113
     * done.
114
     *
115
     * @param \Illuminate\Http\Request $request
116
     * @param string $payumToken
117
     * @param callable $callback
118
     * @return mixed
119
     */
120 1
    public function done(Request $request, $payumToken, callable $callback)
121
    {
122 1
        $request->merge([
123 1
            'payum_token' => $payumToken,
124
        ]);
125 1
        $token = $this->getPayum()->getHttpRequestVerifier()->verify($request);
126 1
        $gateway = $this->getPayum()->getGateway($token->getGatewayName());
127 1
        $gateway->execute($status = new GetHumanStatus($token));
128
129 1
        return $callback($status, $status->getFirstModel(), $token, $gateway);
130
    }
131
132
    /**
133
     * payout.
134
     *
135
     * @param string $type
136
     * @param callable $callback
137
     * @param string $afterPath
138
     * @param array $afterParameters
139
     * @return string
140
     */
141 5
    protected function send($type, callable $callback, $afterPath, $afterParameters)
142
    {
143 5
        $storage = $this->getPayum()->getStorage(Payment::class);
144 5
        $payment = $storage->create();
145 5
        $callback($payment, $this->gatewayName);
146 5
        $storage->update($payment);
147 5
        $tokenFactory = $this->getPayum()->getTokenFactory();
148 5
        $token = call_user_func_array(
149 5
            [$tokenFactory, sprintf('create%sToken', ucfirst($type))],
150 5
            [$this->gatewayName, $payment, $afterPath, $afterParameters]
151
        );
152
153 5
        return $token->getTargetUrl();
154
    }
155
}
156