|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file has been created by developers from BitBag. |
|
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
|
6
|
|
|
* You can find more information about us on https://bitbag.io and write us |
|
7
|
|
|
* an email on [email protected]. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace BitBag\SyliusMolliePlugin\Action\Api; |
|
13
|
|
|
|
|
14
|
|
|
use BitBag\SyliusMolliePlugin\Logger\MollieLoggerActionInterface; |
|
15
|
|
|
use BitBag\SyliusMolliePlugin\Parser\Response\GuzzleNegativeResponseParserInterface; |
|
16
|
|
|
use BitBag\SyliusMolliePlugin\Request\Api\CreatePayment; |
|
17
|
|
|
use Mollie\Api\Exceptions\ApiException; |
|
18
|
|
|
use Mollie\Api\Resources\Payment; |
|
19
|
|
|
use Payum\Core\Bridge\Spl\ArrayObject; |
|
20
|
|
|
use Payum\Core\GatewayAwareTrait; |
|
21
|
|
|
use Payum\Core\Reply\HttpRedirect; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
23
|
|
|
|
|
24
|
|
|
final class CreatePaymentAction extends BaseApiAwareAction |
|
25
|
|
|
{ |
|
26
|
|
|
use GatewayAwareTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** @var MollieLoggerActionInterface */ |
|
29
|
|
|
private $loggerAction; |
|
30
|
|
|
|
|
31
|
|
|
/** @var GuzzleNegativeResponseParserInterface */ |
|
32
|
|
|
private $guzzleNegativeResponseParser; |
|
33
|
|
|
|
|
34
|
|
|
/** @var Session */ |
|
35
|
|
|
private $session; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct( |
|
38
|
|
|
MollieLoggerActionInterface $loggerAction, |
|
39
|
|
|
GuzzleNegativeResponseParserInterface $guzzleNegativeResponseParser, |
|
40
|
|
|
Session $session |
|
41
|
|
|
) { |
|
42
|
|
|
$this->loggerAction = $loggerAction; |
|
43
|
|
|
$this->guzzleNegativeResponseParser = $guzzleNegativeResponseParser; |
|
44
|
|
|
$this->session = $session; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function execute($request): void |
|
48
|
|
|
{ |
|
49
|
|
|
$details = ArrayObject::ensureArrayObject($request->getModel()); |
|
50
|
|
|
|
|
51
|
|
|
try { |
|
52
|
|
|
/** @var Payment $payment */ |
|
53
|
|
|
$payment = $this->mollieApiClient->payments->create([ |
|
54
|
|
|
'method' => $details['metadata']['molliePaymentMethods'] ?: '', |
|
55
|
|
|
'issuer' => $details['metadata']['selected_issuer'] ?? null, |
|
56
|
|
|
'cardToken' => $details['metadata']['cartToken'], |
|
57
|
|
|
'amount' => $details['amount'], |
|
58
|
|
|
'customerId' => $details['customerId'] ?? null, |
|
59
|
|
|
'description' => $details['description'], |
|
60
|
|
|
'redirectUrl' => $details['backurl'], |
|
61
|
|
|
'webhookUrl' => $details['webhookUrl'], |
|
62
|
|
|
'metadata' => $details['metadata'], |
|
63
|
|
|
]); |
|
64
|
|
|
} catch (ApiException $e) { |
|
65
|
|
|
$message = $this->guzzleNegativeResponseParser->parse($e); |
|
66
|
|
|
$this->loggerAction->addNegativeLog(sprintf('Error with create payment with: %s', $e->getMessage())); |
|
67
|
|
|
|
|
68
|
|
|
if (empty($message)) { |
|
69
|
|
|
throw new ApiException(sprintf('Error with create payment with: %s', $e->getMessage())); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$details['statusError'] = $message; |
|
73
|
|
|
|
|
74
|
|
|
$message = \sprintf('%s%s', 'bitbag_sylius_mollie_plugin.credit_cart_error.', $details['statusError']); |
|
75
|
|
|
$this->session->getFlashBag()->add('info', $message); |
|
76
|
|
|
|
|
77
|
|
|
return; |
|
78
|
|
|
} catch (\Exception $e) { |
|
79
|
|
|
$this->loggerAction->addNegativeLog(sprintf('Error with create payment with: %s', $e->getMessage())); |
|
80
|
|
|
|
|
81
|
|
|
throw new ApiException(sprintf('Error with create payment with: %s', $e->getMessage())); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$details['payment_mollie_id'] = $payment->id; |
|
85
|
|
|
|
|
86
|
|
|
$this->loggerAction->addLog(sprintf('Create payment in mollie with id: %s', $payment->id)); |
|
87
|
|
|
|
|
88
|
|
|
if (null === $payment->getCheckoutUrl()) { |
|
89
|
|
|
throw new HttpRedirect($details['backurl']); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
throw new HttpRedirect($payment->getCheckoutUrl()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function supports($request): bool |
|
96
|
|
|
{ |
|
97
|
|
|
return |
|
98
|
|
|
$request instanceof CreatePayment && |
|
99
|
|
|
$request->getModel() instanceof \ArrayAccess; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|