19
19
*/
20
20
namespace Buckaroo \Magento2 \Model \Method ;
21
21
22
+ use Buckaroo \Magento2 \Exception ;
23
+
22
24
class Applepay extends AbstractMethod
23
25
{
24
26
/** Payment Code */
@@ -42,10 +44,34 @@ public function assignData(\Magento\Framework\DataObject $data)
42
44
parent ::assignData ($ data );
43
45
$ data = $ this ->assignDataConvertToArray ($ data );
44
46
47
+ /**
48
+ * @var \Buckaroo\Magento2\Model\ConfigProvider\Method\Applepay $applePayConfig
49
+ */
50
+ $ applePayConfig = $ this ->configProviderMethodFactory ->get ($ this ->_code );
51
+ $ integrationMode = $ applePayConfig ->getIntegrationMode ();
52
+
45
53
if (isset ($ data ['additional_data ' ]['applepayTransaction ' ])) {
46
- $ applepayEncoded = base64_encode ($ data ['additional_data ' ]['applepayTransaction ' ]);
54
+ $ transactionData = $ data ['additional_data ' ]['applepayTransaction ' ];
55
+
56
+ $ this ->validateApplePayTransactionData ($ transactionData );
47
57
58
+ $ applepayEncoded = base64_encode ($ transactionData );
48
59
$ this ->getInfoInstance ()->setAdditionalInformation ('applepayTransaction ' , $ applepayEncoded );
60
+
61
+ $ this ->logger2 ->addDebug (sprintf (
62
+ '[Apple Pay] Transaction data assigned for payment. Length: %d characters ' ,
63
+ strlen ($ transactionData )
64
+ ));
65
+ } else {
66
+ if ($ integrationMode ) {
67
+ $ this ->logger2 ->addError ('[Apple Pay SDK Mode] Missing applepayTransaction data in payment assignment - preventing order creation ' );
68
+
69
+ throw new Exception (
70
+ __ ('Apple Pay payment failed. No transaction data was received from your device. Please try again or use a different payment method. ' )
71
+ );
72
+ } else {
73
+ $ this ->logger2 ->addDebug ('[Apple Pay Redirect Mode] No transaction data in payment assignment - this is expected for redirect mode ' );
74
+ }
49
75
}
50
76
51
77
if (!empty ($ data ['additional_data ' ]['billingContact ' ])) {
@@ -58,42 +84,141 @@ public function assignData(\Magento\Framework\DataObject $data)
58
84
return $ this ;
59
85
}
60
86
87
+ /**
88
+ * Validate Apple Pay transaction data before order creation
89
+ *
90
+ * @param string $transactionData
91
+ * @throws Exception
92
+ */
93
+ private function validateApplePayTransactionData ($ transactionData )
94
+ {
95
+ /**
96
+ * @var \Buckaroo\Magento2\Model\ConfigProvider\Method\Applepay $applePayConfig
97
+ */
98
+ $ applePayConfig = $ this ->configProviderMethodFactory ->get ($ this ->_code );
99
+ $ integrationMode = $ applePayConfig ->getIntegrationMode ();
100
+
101
+ if ($ integrationMode ) {
102
+ $ this ->logger2 ->addDebug ('[Apple Pay SDK Mode] Validating client-side transaction data ' );
103
+
104
+ if (empty ($ transactionData ) || $ transactionData === 'null ' || trim ($ transactionData ) === '' ) {
105
+ $ this ->logger2 ->addError (sprintf (
106
+ '[Apple Pay SDK Mode] Invalid applepayTransaction data before order creation: %s ' ,
107
+ var_export ($ transactionData , true )
108
+ ));
109
+
110
+ throw new Exception (
111
+ __ ('Apple Pay payment failed. The transaction data from your device is invalid. Please try again or use a different payment method. ' )
112
+ );
113
+ }
114
+
115
+ $ decodedJson = json_decode ($ transactionData , true );
116
+ if (json_last_error () !== JSON_ERROR_NONE || empty ($ decodedJson ['paymentData ' ])) {
117
+ $ this ->logger2 ->addError (sprintf (
118
+ '[Apple Pay SDK Mode] Invalid JSON in applepayTransaction before order creation. Error: %s, Data: %s ' ,
119
+ json_last_error_msg (),
120
+ substr ($ transactionData , 0 , 100 ) . '... '
121
+ ));
122
+
123
+ throw new Exception (
124
+ __ ('Apple Pay payment failed. The transaction data format from your device is invalid. Please try again or use a different payment method. ' )
125
+ );
126
+ }
127
+
128
+ $ paymentData = $ decodedJson ['paymentData ' ];
129
+ if (empty ($ paymentData ['data ' ]) || empty ($ paymentData ['signature ' ]) || empty ($ paymentData ['header ' ])) {
130
+ $ this ->logger2 ->addError ('[Apple Pay SDK Mode] Missing required fields in paymentData before order creation ' );
131
+
132
+ throw new Exception (
133
+ __ ('Apple Pay payment failed. The transaction data from your device is incomplete. Please try again or use a different payment method. ' )
134
+ );
135
+ }
136
+
137
+ $ this ->logger2 ->addDebug (sprintf (
138
+ '[Apple Pay SDK Mode] Valid applepayTransaction data validated before order creation. Length: %d characters ' ,
139
+ strlen ($ transactionData )
140
+ ));
141
+ } else {
142
+ $ this ->logger2 ->addDebug ('[Apple Pay Redirect Mode] Skipping transaction data validation - payment will be processed on Buckaroo hosted page ' );
143
+
144
+ if (!empty ($ transactionData )) {
145
+ $ this ->logger2 ->addDebug ('[Apple Pay Redirect Mode] Unexpected transaction data present - this should be empty for redirect mode ' );
146
+ }
147
+ }
148
+ }
149
+
61
150
/**
62
151
* {@inheritdoc}
63
152
*/
64
153
public function getOrderTransactionBuilder ($ payment )
65
154
{
66
155
$ transactionBuilder = $ this ->transactionBuilderFactory ->get ('order ' );
67
156
68
- $ requestParameters = [
69
- [
70
- '_ ' => $ payment ->getAdditionalInformation ('applepayTransaction ' ),
71
- 'Name ' => 'PaymentData ' ,
72
- ]
73
- ];
74
-
75
- $ billingContact = $ payment ->getAdditionalInformation ('billingContact ' ) ?
76
- json_decode ($ payment ->getAdditionalInformation ('billingContact ' )) : null ;
77
- if ($ billingContact && !empty ($ billingContact ->givenName ) && !empty ($ billingContact ->familyName )) {
78
- $ requestParameters [] = [
79
- '_ ' => $ billingContact ->givenName . ' ' . $ billingContact ->familyName ,
80
- 'Name ' => 'CustomerCardName ' ,
157
+ /**
158
+ * @var \Buckaroo\Magento2\Model\ConfigProvider\Method\Applepay $applePayConfig
159
+ */
160
+ $ applePayConfig = $ this ->configProviderMethodFactory ->get ($ this ->_code );
161
+
162
+ $ integrationMode = $ applePayConfig ->getIntegrationMode ();
163
+
164
+ if ($ integrationMode ) {
165
+ $ applepayTransactionData = $ payment ->getAdditionalInformation ('applepayTransaction ' );
166
+
167
+ if (empty ($ applepayTransactionData )) {
168
+ $ this ->logger2 ->addError (sprintf (
169
+ '[Apple Pay SDK Mode] Missing PaymentData for order %s - Client-side transaction processing failed ' ,
170
+ $ payment ->getOrder ()->getIncrementId ()
171
+ ));
172
+
173
+ throw new Exception (
174
+ __ ('Apple Pay payment processing failed. The transaction data from your device could not be processed. Please try again or use a different payment method. ' )
175
+ );
176
+ }
177
+
178
+ $ requestParameters = [
179
+ [
180
+ '_ ' => $ applepayTransactionData ,
181
+ 'Name ' => 'PaymentData ' ,
182
+ ]
81
183
];
82
- }
83
184
84
- $ services = [
85
- 'Name ' => 'applepay ' ,
86
- 'Action ' => 'Pay ' ,
87
- 'Version ' => 0 ,
88
- 'RequestParameter ' => $ requestParameters ,
89
- ];
185
+ $ billingContact = $ payment ->getAdditionalInformation ('billingContact ' ) ?
186
+ json_decode ($ payment ->getAdditionalInformation ('billingContact ' )) : null ;
187
+ if ($ billingContact && !empty ($ billingContact ->givenName ) && !empty ($ billingContact ->familyName )) {
188
+ $ requestParameters [] = [
189
+ '_ ' => $ billingContact ->givenName . ' ' . $ billingContact ->familyName ,
190
+ 'Name ' => 'CustomerCardName ' ,
191
+ ];
192
+ }
90
193
91
- /**
92
- * @noinspection PhpUndefinedMethodInspection
93
- */
94
- $ transactionBuilder ->setOrder ($ payment ->getOrder ())
95
- ->setServices ($ services )
96
- ->setMethod ('TransactionRequest ' );
194
+ $ services = [
195
+ 'Name ' => 'applepay ' ,
196
+ 'Action ' => 'Pay ' ,
197
+ 'Version ' => 0 ,
198
+ 'RequestParameter ' => $ requestParameters ,
199
+ ];
200
+
201
+ $ transactionBuilder ->setOrder ($ payment ->getOrder ())
202
+ ->setServices ($ services )
203
+ ->setMethod ('TransactionRequest ' );
204
+
205
+ } else {
206
+ $ services = [
207
+ 'Name ' => 'applepay ' ,
208
+ 'Action ' => 'Pay ' ,
209
+ 'Version ' => 0 ,
210
+ 'RequestParameter ' => [],
211
+ ];
212
+
213
+ /**
214
+ * @noinspection PhpUndefinedMethodInspection
215
+ */
216
+ $ transactionBuilder ->setOrder ($ payment ->getOrder ())
217
+ ->setServices ($ services )
218
+ ->setMethod ('TransactionRequest ' );
219
+
220
+ $ transactionBuilder ->setCustomVars (['ContinueOnIncomplete ' => 'RedirectToHTML ' ]);
221
+ }
97
222
98
223
return $ transactionBuilder ;
99
224
}
0 commit comments