Skip to content

Commit 8230f3e

Browse files
authored
Merge pull request #49 from Adyen/develop
Release 1.1.0
2 parents a45a9f9 + 2e0f116 commit 8230f3e

File tree

5 files changed

+299
-4
lines changed

5 files changed

+299
-4
lines changed

Magewire/Payment/Method/AdyenPaymentComponent.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,22 @@
2424

2525
abstract class AdyenPaymentComponent extends Component implements EvaluationInterface
2626
{
27+
const REQUIRED_ADDRESS_FIELDS = [
28+
'city',
29+
'country_id',
30+
'postcode',
31+
'street',
32+
'firstname',
33+
'lastname',
34+
'telephone'
35+
];
36+
2737
public bool $requiresShipping = true;
2838
public ?string $paymentResponse = null;
2939
public ?string $paymentStatus = null;
3040
public ?string $paymentDetails = null;
31-
41+
public ?string $billingAddress = null;
42+
public ?string $shippingAddress = null;
3243

3344
protected CheckoutStateDataValidator $checkoutStateDataValidator;
3445
protected Configuration $configuration;
@@ -126,6 +137,7 @@ public function refreshProperties(): void
126137
{
127138
$this->processRequiresShipping();
128139
$this->processPaymentResponse();
140+
$this->processQuoteAddresses();
129141
}
130142

131143
/**
@@ -215,4 +227,30 @@ private function processPaymentResponse(): void
215227
$this->logger->error('Could not collect Adyen payment methods response: ' . $e->getMessage());
216228
}
217229
}
230+
231+
private function processQuoteAddresses(): void
232+
{
233+
$billingAddress = $this->session->getQuote()->getBillingAddress();
234+
if (isset($billingAddress)) {
235+
$this->billingAddress = json_encode($this->filterAddressFields($billingAddress->toArray()));
236+
}
237+
238+
$shippingAddress = $this->session->getQuote()->getShippingAddress();
239+
if (isset($shippingAddress)) {
240+
$this->shippingAddress = json_encode($this->filterAddressFields($shippingAddress->toArray()));
241+
}
242+
}
243+
244+
private function filterAddressFields(array $address): array
245+
{
246+
$filteredAddress = [];
247+
248+
foreach($address as $fieldName => $value) {
249+
if (in_array($fieldName, self::REQUIRED_ADDRESS_FIELDS)) {
250+
$filteredAddress[$fieldName] = $value;
251+
}
252+
}
253+
254+
return $filteredAddress;
255+
}
218256
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "adyen/module-hyva-checkout",
33
"description": "Adyen Integration with Hyva Checkout",
44
"type": "magento2-module",
5-
"version": "1.0.0",
5+
"version": "1.1.0",
66
"license": "MIT",
77
"require": {
88
"adyen/module-payment": "^9.6.1",

etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<item name="adyen_paypal" xsi:type="string">adyen-paypal-method.phtml</item>
2424
<item name="adyen_cashapp" xsi:type="string">adyen-cashapp-method.phtml</item>
2525
<item name="adyen_affirm" xsi:type="string">adyen-affirm-method.phtml</item>
26+
<item name="adyen_amazonpay" xsi:type="string">adyen-amazonpay-method.phtml</item>
2627
</argument>
2728
<argument name="customMagewireClasses" xsi:type="array">
2829
<item name="adyen_cc" xsi:type="object">Adyen\Hyva\Magewire\Payment\Method\CreditCard</item>
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
<?php
2+
3+
/** @var AbstractPaymentMethodWire $magewire */
4+
/** @var Template $block */
5+
/** @var Escaper $escaper */
6+
7+
use Adyen\Hyva\Magewire\Payment\Method\AbstractPaymentMethodWire;
8+
use Magento\Framework\Escaper;
9+
use Magento\Framework\View\Element\Template;
10+
11+
?>
12+
13+
<div>
14+
<div id="<?= $magewire->getContainerName() ?>ActionContainer" wire:ignore></div>
15+
16+
<script>
17+
class amazonPayComponentHandler extends componentHandler {
18+
constructor(paymentMethodCode, wire, elementLabel) {
19+
super(paymentMethodCode, wire, elementLabel);
20+
21+
this.amazonPayStatus = null;
22+
}
23+
24+
buildConfiguration (paymentMethod, paymentMethodsExtraInfo) {
25+
let baseComponentConfiguration = super.buildConfiguration(paymentMethod, paymentMethodsExtraInfo);
26+
27+
let formattedShippingAddress = {};
28+
let formattedBillingAddress = {};
29+
30+
const shippingAddress = this.wire.get('shippingAddress');
31+
const billingAddress = this.wire.get('billingAddress');
32+
33+
if (shippingAddress) {
34+
formattedShippingAddress = this.getFormattedAddress(JSON.parse(shippingAddress));
35+
}
36+
37+
if (billingAddress) {
38+
formattedBillingAddress = this.getFormattedAddress(JSON.parse(billingAddress));
39+
}
40+
41+
baseComponentConfiguration.showPayButton = true;
42+
baseComponentConfiguration.onClick = function(resolve, reject) {
43+
resolve();
44+
}
45+
46+
baseComponentConfiguration.productType = 'PayAndShip';
47+
baseComponentConfiguration.checkoutMode = 'ProcessOrder';
48+
let url = new URL(location.href);
49+
url.searchParams.delete('amazonCheckoutSessionId');
50+
baseComponentConfiguration.returnUrl = url.href;
51+
52+
if (formattedShippingAddress &&
53+
formattedShippingAddress.telephone) {
54+
baseComponentConfiguration.addressDetails = {
55+
name: formattedShippingAddress.firstName +
56+
' ' +
57+
formattedShippingAddress.lastName,
58+
addressLine1: formattedShippingAddress.street,
59+
addressLine2: formattedShippingAddress.houseNumber,
60+
city: formattedShippingAddress.city,
61+
postalCode: formattedShippingAddress.postalCode,
62+
countryCode: formattedShippingAddress.country,
63+
phoneNumber: formattedShippingAddress.telephone
64+
};
65+
if (baseComponentConfiguration.addressDetails.countryCode === 'US') {
66+
baseComponentConfiguration.addressDetails.stateOrRegion = quote.shippingAddress().regionCode
67+
}
68+
} else if (formattedBillingAddress &&
69+
formattedBillingAddress.telephone) {
70+
baseComponentConfiguration.addressDetails = {
71+
name: formattedBillingAddress.firstName +
72+
' ' +
73+
formattedBillingAddress.lastName,
74+
addressLine1: formattedBillingAddress.street,
75+
addressLine2: formattedBillingAddress.houseNumber,
76+
77+
city: formattedBillingAddress.city,
78+
postalCode: formattedBillingAddress.postalCode,
79+
countryCode: formattedBillingAddress.country,
80+
phoneNumber: formattedBillingAddress.telephone
81+
};
82+
if (baseComponentConfiguration.addressDetails.countryCode === 'US') {
83+
baseComponentConfiguration.addressDetails.stateOrRegion = quote.billingAddress().regionCode
84+
}
85+
}
86+
87+
return baseComponentConfiguration;
88+
}
89+
90+
mountComponent(checkoutComponent, paymentMethodType, configuration, result = undefined) {
91+
let self = this;
92+
const containerId = "<?= $magewire->getContainerName() ?>ActionContainer";
93+
94+
let url = new URL(location.href);
95+
// Handles the redirect back to checkout page with amazonSessionKey in url
96+
// If previous attempt has failed, component is mounted again and session ID is ignored.
97+
if (this.amazonPayStatus !== false && url.searchParams.has('amazonCheckoutSessionId')) {
98+
let componentConfig = {
99+
amazonCheckoutSessionId: url.searchParams.get('amazonCheckoutSessionId'),
100+
showOrderButton: false,
101+
amount: {
102+
currency: configuration.amount.currency,
103+
value: configuration.amount.value
104+
},
105+
showChangePaymentDetailsButton: false,
106+
onSubmit: async (state, amazonPayComponent) => {
107+
self.placeOrder(state.data, null, amazonPayComponent);
108+
},
109+
onError: (error) => {
110+
unmountAdyenComponent();
111+
super.mountComponent(checkoutComponent, paymentMethodType, configuration, result);
112+
}
113+
}
114+
try {
115+
let amazonPayComponent = checkoutComponent.create(
116+
paymentMethodType,
117+
componentConfig
118+
);
119+
120+
amazonPayComponent.mount('#' + containerId);
121+
amazonPayComponent.submit();
122+
} catch (err) {
123+
console.log(err);
124+
}
125+
} else {
126+
super.mountComponent(checkoutComponent, paymentMethodType, configuration, result);
127+
}
128+
}
129+
130+
placeOrder(data, publicHash = null, amazonPayComponent) {
131+
let self = this;
132+
let wire = self.wire;
133+
134+
wire.placeOrder({
135+
stateData: data,
136+
ccType: this.getCreditCardType(),
137+
publicHash: publicHash
138+
})
139+
.then(() => {
140+
let paymentStatus = JSON.parse(wire.get('paymentStatus'));
141+
self.handleAdyenResult(paymentStatus, amazonPayComponent);
142+
}).catch(() => {
143+
console.log('Error occurred during order placement')
144+
});
145+
}
146+
147+
handleAdyenResult(responseJSON, amazonPayComponent) {
148+
let self = this;
149+
150+
if (responseJSON.isRefused) {
151+
let message = "<?= __("The Payment is Refused") ?>";
152+
153+
this.amazonPayStatus = false;
154+
155+
self.renderMessage(message);
156+
setTimeout(() => {
157+
self.clearMessage();
158+
}, 4000);
159+
160+
amazonPayComponent.handleDeclineFlow();
161+
} else {
162+
super.handleAdyenResult(responseJSON);
163+
}
164+
}
165+
}
166+
167+
window.addEventListener('checkout:payment:method-list-boot', async (event) => {
168+
unmountAdyenComponent();
169+
await init(event.detail.method);
170+
});
171+
172+
window.addEventListener('checkout:payment:method-activate', async (event) => {
173+
await init(event.detail.method);
174+
});
175+
176+
async function init(methodCode) {
177+
try {
178+
let wire = Magewire.find('checkout.payment.methods.' + methodCode);
179+
wire.refreshProperties()
180+
.then(() => {
181+
let amazonPayHandler = new amazonPayComponentHandler(
182+
methodCode,
183+
wire,
184+
'<?= $magewire->getContainerName() ?>ActionContainer'
185+
);
186+
187+
window.AdyenPaymentHandler = amazonPayHandler;
188+
189+
if (methodCode !== 'adyen_amazonpay') {
190+
amazonPayHandler.renderMethodUnavailableMessage();
191+
return;
192+
}
193+
194+
if (wire.get('requiresShipping')) {
195+
amazonPayHandler.renderMessage('Please select shipping method.');
196+
} else {
197+
let rawResponse = wire.get('paymentResponse');
198+
let paymentMethods = JSON.parse(rawResponse);
199+
amazonPayHandler.activatePaymentMethod(methodCode, paymentMethods);
200+
hidePrimaryButton();
201+
}
202+
}).catch(() => {
203+
console.log('Error occurred during Amazon Pay component load')
204+
});
205+
} catch (e) {
206+
}
207+
}
208+
</script>
209+
</div>

view/frontend/templates/payment/model/adyen-payment-method.phtml

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ use Adyen\Hyva\Block\PaymentMethod;
1212

1313
$modalViewModel = $viewModels->require(Modal::class);
1414
$availableMethods = json_encode(array_keys($block->getAvailableMethods()));
15-
$environment = $block->getConfiguration()->getValue('adyen/checkoutEnvironment');
15+
$environment = $block->getConfiguration()->getValue('adyen/checkoutEnvironment');
16+
$houseNumberStreetLine = $block->getConfiguration()->getValue('adyen/houseNumberStreetLine');
1617
$shippingAddress = $block->getQuoteShippingAddress();
17-
$billingAddress = $block->getQuoteBillingAddress();
18+
$billingAddress = $block->getQuoteBillingAddress();
1819

1920
?>
2021
<div x-data="{...hyva.modal(), isLoading: false}" @adyen-modal-hide.window="hide()" @adyen-loading.window="isLoading = $event.detail">
@@ -448,6 +449,52 @@ $billingAddress = $block->getQuoteBillingAddress();
448449
messageContainerElement.remove();
449450
}
450451
}
452+
453+
getFormattedAddress(address) {
454+
let city = '';
455+
let country = '';
456+
let postalCode = '';
457+
let street = '';
458+
let houseNumber = '';
459+
460+
let getHouseNumberStreetLineConfig = "<?= $houseNumberStreetLine ?>";
461+
462+
city = address.city;
463+
country = address.country_id;
464+
postalCode = address.postcode;
465+
466+
street = address.street.split("\n");
467+
468+
// address contains line items as an array, otherwise if string just pass along as is
469+
if (Array.isArray(street)) {
470+
// getHouseNumberStreetLine > 0 implies the street line that is used to gather house number
471+
if (getHouseNumberStreetLineConfig > 0) {
472+
// removes the street line from array that is used to contain house number
473+
houseNumber = street.splice(getHouseNumberStreetLineConfig - 1, 1);
474+
} else if (street.length > 1) { // getHouseNumberStreetLine = 0 uses the last street line as house number
475+
// in case there is more than 1 street lines in use, removes last street line from array that should be used to contain house number
476+
houseNumber = street.pop();
477+
}
478+
479+
// Concat street lines except house number
480+
street = street.join(' ');
481+
}
482+
483+
let firstName = address.firstname;
484+
let lastName = address.lastname;
485+
let telephone = address.telephone;
486+
487+
return {
488+
city: city,
489+
country: country,
490+
postalCode: postalCode,
491+
street: street,
492+
houseNumber: houseNumber,
493+
firstName: firstName,
494+
lastName: lastName,
495+
telephone: telephone
496+
};
497+
}
451498
}
452499
</script>
453500

0 commit comments

Comments
 (0)