1
+ <?php
2
+ /**
3
+ * PostFinance Checkout Magento 2
4
+ *
5
+ * This Magento 2 extension enables to process payments with PostFinance Checkout (https://postfinance.ch/en/business/products/e-commerce/postfinance-checkout-all-in-one.html/).
6
+ *
7
+ * @package PostFinanceCheckout_Payment
8
+ * @author wallee AG (http://www.wallee.com/)
9
+ * @license http://www.apache.org/licenses/LICENSE-2.0 Apache Software License (ASL 2.0)
10
+ */
11
+ namespace PostFinanceCheckout \Payment \Model \Resolver ;
12
+
13
+ use Magento \Checkout \Model \Session as CheckoutSession ;
14
+ use Magento \CustomerGraphQl \Model \Customer \GetCustomer ;
15
+ use Magento \Framework \Exception \LocalizedException ;
16
+ use Magento \Framework \Exception \NoSuchEntityException ;
17
+ use Magento \Framework \GraphQl \Config \Element \Field ;
18
+ use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
19
+ use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
20
+ use Magento \Framework \GraphQl \Query \ResolverInterface ;
21
+ use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
22
+ use Magento \Customer \Model \Session ;
23
+ use Magento \GraphQl \Model \Query \ContextInterface ;
24
+ use Magento \Sales \Api \OrderRepositoryInterface ;
25
+ use Psr \Log \LoggerInterface ;
26
+ use PostFinanceCheckout \Payment \Model \Service \Quote \TransactionService as TransactionQuoteService ;
27
+ use PostFinanceCheckout \Payment \Model \Service \Order \TransactionService as TransactionOrderService ;
28
+
29
+ class CustomerOrderTransactionSettings implements ResolverInterface
30
+ {
31
+ /**
32
+ *
33
+ * @var Session
34
+ */
35
+ private $ customerSession ;
36
+
37
+ /**
38
+ *
39
+ * @var CheckoutSession
40
+ */
41
+ private $ checkoutSession ;
42
+
43
+ /**
44
+ *
45
+ * @var GetCustomer
46
+ */
47
+ private $ getCustomer ;
48
+
49
+ /**
50
+ *
51
+ * @var OrderRepositoryInterface
52
+ */
53
+ private $ orderRepository ;
54
+
55
+ /**
56
+ *
57
+ * @var TransactionQuoteService
58
+ */
59
+ private $ transactionQuoteService ;
60
+
61
+ /**
62
+ *
63
+ * @var TransactionOrderService
64
+ */
65
+ private $ transactionOrderService ;
66
+
67
+ /**
68
+ *
69
+ * @var LoggerInterface
70
+ */
71
+ private $ logger ;
72
+
73
+
74
+ public function __construct (
75
+ Session $ customerSession ,
76
+ CheckoutSession $ checkoutSession ,
77
+ GetCustomer $ getCustomer ,
78
+ OrderRepositoryInterface $ orderRepository ,
79
+ TransactionQuoteService $ transactionQuoteService ,
80
+ TransactionOrderService $ transactionOrderService ,
81
+ LoggerInterface $ logger
82
+ ) {
83
+ $ this ->customerSession = $ customerSession ;
84
+ $ this ->checkoutSession = $ checkoutSession ;
85
+ $ this ->getCustomer = $ getCustomer ;
86
+ $ this ->logger = $ logger ;
87
+ $ this ->transactionQuoteService = $ transactionQuoteService ;
88
+ $ this ->transactionOrderService = $ transactionOrderService ;
89
+ $ this ->orderRepository = $ orderRepository ;
90
+ }
91
+
92
+ public function resolve (Field $ field , $ context , ResolveInfo $ info , array $ value = null , array $ args = null )
93
+ {
94
+ //only perform validations if the user is anonymous.
95
+ if ($ this ->checkoutSession ->getQuote ()->getCustomerId ()) {
96
+ /** @var ContextInterface $context */
97
+ if (false === $ context ->getExtensionAttributes ()->getIsCustomer ()) {
98
+ throw new GraphQlAuthorizationException (__ ('The current customer isn \'t authorized. ' ));
99
+ }
100
+
101
+ $ customer = $ this ->getCustomer ->execute ($ context );
102
+ if (!empty ($ this ->customerSession ) && $ customer ->getId () !== $ this ->customerSession ->getCustomer ()->getId ()) {
103
+ throw new GraphQlAuthorizationException (__ ('The current customer isn \'t authorized. ' ));
104
+ }
105
+ }
106
+
107
+ try {
108
+ $ orderId = $ args ['order_id ' ];
109
+ $ integrationType = $ args ['integration_type ' ];
110
+ return $ this ->getTransactionSettings ($ orderId , $ integrationType );
111
+ } catch (NoSuchEntityException $ e ) {
112
+ $ this ->logger ->critical ($ e );
113
+ throw new GraphQlNoSuchEntityException (__ ($ e ->getMessage ()));
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Gets the transaction settings to use their custom payment integration
119
+ *
120
+ * @return array
121
+ * @throws NoSuchEntityException
122
+ * @throws LocalizedException
123
+ */
124
+ private function getTransactionSettings (int $ orderId , string $ integrationType )
125
+ {
126
+ /** @var \Magento\Sales\Model\Order $order */
127
+ $ order = $ this ->orderRepository ->get ($ orderId );
128
+ $ transaction = $ this ->transactionQuoteService ->getTransaction (
129
+ $ order ->getPostfinancecheckoutSpaceId (),
130
+ $ order ->getPostfinancecheckoutTransactionId ()
131
+ );
132
+ $ url = $ this ->transactionOrderService ->getTransactionPaymentUrl ($ order , $ integrationType );
133
+
134
+ return [
135
+ 'order_id ' => $ order ->getId (),
136
+ 'transaction_id ' => $ transaction ->getId (),
137
+ 'transaction_state ' => $ transaction ->getState (),
138
+ 'payment_url ' => $ url ,
139
+ 'integration_type ' => $ integrationType
140
+ ];
141
+ }
142
+ }
0 commit comments