1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of gpupo/adyen-sdk |
5
|
|
|
* |
6
|
|
|
* (c) Gilmar Pupo <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* For more information, see |
12
|
|
|
* <http://www.g1mr.com/adyen-sdk/>. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Gpupo\AdyenSdk\Payment\Request; |
16
|
|
|
|
17
|
|
|
use Gpupo\AdyenSdk\Factory; |
18
|
|
|
use Gpupo\AdyenSdk\Payment\Response\Decorator\ErrorDecorator; |
19
|
|
|
use Gpupo\AdyenSdk\Payment\Response\Decorator\ProblematicDecorator; |
20
|
|
|
use Gpupo\Common\Interfaces\OptionsInterface; |
21
|
|
|
use Gpupo\Common\Traits\OptionsTrait; |
22
|
|
|
use Gpupo\CommonSdk\Entity\EntityInterface; |
23
|
|
|
use Gpupo\CommonSdk\Entity\ManagerAbstract; |
24
|
|
|
use Gpupo\CommonSdk\Response; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Gerenciamento de Transações Adyen. |
28
|
|
|
*/ |
29
|
|
|
class Manager extends ManagerAbstract implements OptionsInterface |
30
|
|
|
{ |
31
|
|
|
use OptionsTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @internal |
35
|
|
|
*/ |
36
|
|
|
public function update(EntityInterface $entity, EntityInterface $existent) |
37
|
|
|
{ |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected $entity = 'Request'; |
41
|
|
|
|
42
|
|
|
protected $maps = [ |
43
|
|
|
'submit' => ['POST', '/authorise'], |
44
|
|
|
'capture' => ['POST', '/capture'], |
45
|
|
|
'cancelOrRefund' => ['POST', '/cancelOrRefund'], |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
protected function preExecute(Request $request) |
49
|
|
|
{ |
50
|
|
|
$request->setMerchantAccount($this->getOptions()->get('merchant_account')); |
51
|
|
|
|
52
|
|
|
return $request; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function call(Request $request, $route) |
56
|
|
|
{ |
57
|
|
|
$response = $this->execute($this->factoryMap($route), $request->toJson()); |
58
|
|
|
|
59
|
|
|
return $this->processExecute($request, $response); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function blow(Request $request, $route) |
63
|
|
|
{ |
64
|
|
|
$request = $this->preExecute($request); |
65
|
|
|
try { |
66
|
|
|
return $this->call($request, $route); |
67
|
|
|
} catch (\Exception $exception) { |
68
|
|
|
return new ErrorDecorator($exception); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function submit(Request $request) |
73
|
|
|
{ |
74
|
|
|
return $this->blow($request, 'submit'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function capture(Request $request) |
78
|
|
|
{ |
79
|
|
|
$request->setType('capture'); |
80
|
|
|
|
81
|
|
|
return $this->blow($request, 'capture'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function cancelOrRefund(Request $request) |
85
|
|
|
{ |
86
|
|
|
$request->setType('cancelOrRefund'); |
87
|
|
|
|
88
|
|
|
return $this->blow($request, 'cancelOrRefund'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function processExecute(Request $request, Response $response) |
92
|
|
|
{ |
93
|
|
|
if (300 > $response->getHttpStatusCode()) { |
94
|
|
|
$decorator = $this->resolveDecorator($request); |
95
|
|
|
} else { |
96
|
|
|
$decorator = $this->getFullyQualifiedDecoratorName('ProblematicDecorator'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$data = $response->getData()->toArray(); |
100
|
|
|
$instance = new $decorator($data); |
101
|
|
|
$instance->setCode($response->getHttpStatusCode()); |
102
|
|
|
|
103
|
|
|
return $instance; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function getFullyQualifiedDecoratorName($name) |
107
|
|
|
{ |
108
|
|
|
return Factory::PACKAGENAME . 'Payment\Response\Decorator\\' . $name; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function resolveDecorator(Request $request) |
112
|
|
|
{ |
113
|
|
|
$className = $this->getFullyQualifiedDecoratorName($request->getDecoratorName()); |
114
|
|
|
|
115
|
|
|
if ( ! class_exists($className)) { |
116
|
|
|
throw new \InvalidArgumentException('Response type [' . $request->getType() . '] not supported!'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $className; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: