1 | <?php |
||
20 | abstract class Gateway |
||
21 | { |
||
22 | protected $sandbox; |
||
23 | |||
24 | protected $options; |
||
25 | |||
26 | private $responseCallback; |
||
27 | |||
28 | private $transactionToMethod = [ |
||
29 | Query::class => 'query', |
||
30 | Cancel::class => 'cancel', |
||
31 | Refund::class => 'refund', |
||
32 | Capture::class => 'capture', |
||
33 | Initial::class => 'initiate', |
||
34 | Transfer::class => 'transfer', |
||
35 | Purchase::class => 'purchase', |
||
36 | Retrieve::class => 'retrieve', |
||
37 | Authorize::class => 'authorize', |
||
38 | ThreedSecureAuthenticate::class => 'threedSecureAuthenticate', |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * Return whether the response is success or not. |
||
43 | * |
||
44 | * $response param contains all the elements of gateway response, |
||
45 | * parsed as associative array, including http status and headers. |
||
46 | * |
||
47 | * @param array $response |
||
48 | * @return bool |
||
49 | */ |
||
50 | abstract protected function success(array $response); |
||
51 | |||
52 | /** |
||
53 | * Returns the message from gateway response. |
||
54 | * |
||
55 | * $response param contains all the elements of gateway response, |
||
56 | * parsed as associative array, including http status and headers. |
||
57 | * |
||
58 | * @param array $response |
||
59 | * @return string |
||
60 | */ |
||
61 | abstract protected function message(array $response); |
||
62 | |||
63 | /** |
||
64 | * Returns th unique transaction id from gateway response. |
||
65 | * |
||
66 | * $response param contains all the elements of gateway response, |
||
67 | * parsed as associative array, including http status and headers. |
||
68 | * |
||
69 | * @param array $response |
||
70 | * @return string |
||
71 | */ |
||
72 | abstract protected function transactionId(array $response); |
||
73 | |||
74 | /** |
||
75 | * Returns error code from gateway if exists. |
||
76 | * |
||
77 | * $response param contains all the elements of gateway response, |
||
78 | * parsed as associative array, including http status and headers. |
||
79 | * |
||
80 | * @param array $response |
||
81 | * @return string|null |
||
82 | */ |
||
83 | abstract protected function errorCode(array $response); |
||
84 | |||
85 | /** |
||
86 | * Returns response code from card processing, if exists. |
||
87 | * @link https://arch.developer.visa.com/vpp/documents/xml/Request_and_Response.html Example of response codes |
||
88 | * |
||
89 | * $response param contains all the elements of gateway response, |
||
90 | * parsed as associative array, including http status and headers. |
||
91 | * |
||
92 | * @param array $response |
||
93 | * @return string|null |
||
94 | */ |
||
95 | abstract protected function responseCode(array $response); |
||
96 | |||
97 | final public function __construct(array $options = []) |
||
98 | { |
||
99 | $this->options = new ParamsBag($options); |
||
100 | |||
101 | $this->sandbox = $this->options->get('sandbox', false); |
||
102 | } |
||
103 | |||
104 | 6 | public function execute( |
|
105 | Transaction $transaction, |
||
106 | 6 | callable $responseCallback = null |
|
107 | ) { |
||
108 | $transaction->commit(); |
||
109 | |||
110 | 6 | $this->responseCallback = $responseCallback; |
|
111 | |||
112 | 6 | foreach ($this->transactionToMethod as $class => $method) { |
|
113 | 1 | if ($transaction instanceof $class) { |
|
114 | 1 | return $this->$method($transaction); |
|
115 | } |
||
116 | 1 | } |
|
117 | |||
118 | throw new \RuntimeException( |
||
119 | sprintf('Invalid transaction type `%s`', get_class($transaction)) |
||
120 | ); |
||
121 | 5 | } |
|
122 | |||
123 | protected function purchase(Purchase $transaction) |
||
|
|||
124 | { |
||
125 | throw GatewayException::notImplemented(__FUNCTION__); |
||
126 | } |
||
127 | |||
128 | protected function authorize(Authorize $transaction) |
||
129 | { |
||
130 | throw GatewayException::notImplemented(__FUNCTION__); |
||
131 | } |
||
132 | |||
133 | protected function capture(Capture $transaction) |
||
134 | { |
||
135 | throw GatewayException::notImplemented(__FUNCTION__); |
||
136 | } |
||
137 | |||
138 | 1 | protected function refund(Refund $transaction) |
|
142 | |||
143 | protected function cancel(Cancel $transaction) |
||
144 | { |
||
145 | throw GatewayException::notImplemented(__FUNCTION__); |
||
146 | } |
||
147 | |||
148 | protected function retrieve(Retrieve $transaction) |
||
152 | |||
153 | protected function initiate(Initial $transaction) |
||
157 | |||
158 | protected function query(Query $transaction) |
||
162 | |||
163 | protected function threedSecureAuthenticate(ThreedSecureAuthenticate $transaction) |
||
167 | |||
168 | protected function transfer(Transfer $transaction) |
||
172 | |||
173 | /** |
||
174 | * Creates and return the response from gateway. |
||
175 | * |
||
176 | * @param bool $success Whether response was success or not. |
||
177 | * @param string $message The message theat describe response status or |
||
178 | * reason. |
||
179 | * @param string $transactionId The unique identifier from transaction. |
||
180 | * @param string $errorCode The error code if transaction was failed. |
||
181 | * @param string $responseCode The ISO 8583 response code. Not always |
||
182 | * available. |
||
183 | * @param array $payload An associative array of the response from |
||
184 | * gateway including http status and headers. |
||
185 | * @param string $rawResponse The raw response of gateway. |
||
186 | * @param string $rawRequest The raw request to gateway. |
||
187 | * |
||
188 | * @return Larium\Pay\Response|mixed Response may be a user response if $responseCallback |
||
189 | * param is used in execute method |
||
190 | */ |
||
191 | 1 | protected function createResponse( |
|
224 | } |
||
225 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.