Skip to content

Commit a8a80f1

Browse files
committed
Merge branch 'main' of github.com:MultiSafepay/magento2-react-checkout-multisafepay
2 parents 30f8ce9 + 87d09a5 commit a8a80f1

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import React, { useEffect, useContext } from 'react';
2+
import { func, shape } from 'prop-types';
3+
import { __ } from '@hyva/react-checkout/i18n';
4+
import RadioInput from '@hyva/react-checkout/components/common/Form/RadioInput';
5+
import Card from '@hyva/react-checkout/components/common/Card';
6+
import SelectInput from '@hyva/react-checkout/components/common/Form/SelectInput';
7+
import CheckoutFormContext from '@hyva/react-checkout/context/Form/CheckoutFormContext';
8+
import { PAYMENT_METHOD_FORM } from '@hyva/react-checkout/config';
9+
import useMultiSafepayPaymentMethodContext from '../../hooks/useMultiSafepayPaymentMethodContext';
10+
import useMultiSafepayIdeal from './hooks/useMultiSafepayIdeal';
11+
import idealConfig from './idealConfig';
12+
import { paymentMethodShape } from '../../utility';
13+
14+
const issuersField = `${PAYMENT_METHOD_FORM}.multisafepay.ideal.issuersField`;
15+
16+
function IdealComponent({ method, selected, actions }) {
17+
const { formikData, setFieldValue } = useMultiSafepayPaymentMethodContext();
18+
const { registerPaymentAction } = useContext(CheckoutFormContext);
19+
const { placeOrderWithIdeal } = useMultiSafepayIdeal(method.code);
20+
const isSelected = method.code === selected.code;
21+
22+
useEffect(() => {
23+
setFieldValue(issuersField, '');
24+
}, [setFieldValue]);
25+
26+
useEffect(() => {
27+
registerPaymentAction(method.code, placeOrderWithIdeal);
28+
}, [method, registerPaymentAction, placeOrderWithIdeal]);
29+
30+
const idealRadioInput = (
31+
<RadioInput
32+
value={method.code}
33+
label={method.title}
34+
name="paymentMethod"
35+
checked={isSelected}
36+
onChange={actions.change}
37+
/>
38+
);
39+
40+
if (!isSelected) {
41+
return idealRadioInput;
42+
}
43+
44+
return (
45+
<div>
46+
<div>{idealRadioInput}</div>
47+
<div className="mx-4 my-4">
48+
<Card bg="darker">
49+
<div className="container flex flex-col justify-center w-4/5">
50+
<SelectInput
51+
formikData={formikData}
52+
name={issuersField}
53+
label={__('Choose your bank')}
54+
options={idealConfig.issuers}
55+
/>
56+
</div>
57+
</Card>
58+
</div>
59+
</div>
60+
);
61+
}
62+
63+
IdealComponent.propTypes = {
64+
method: paymentMethodShape.isRequired,
65+
selected: paymentMethodShape.isRequired,
66+
actions: shape({ change: func }).isRequired,
67+
};
68+
69+
export default IdealComponent;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useCallback } from 'react';
2+
import { get } from 'lodash';
3+
import { PAYMENT_METHOD_FORM } from '@hyva/react-checkout/config';
4+
import usePerformPlaceOrder from '../../../hooks/usePerformPlaceOrder';
5+
6+
const issuersField = `${PAYMENT_METHOD_FORM}.multisafepay.ideal.issuersField`;
7+
8+
export default function useMultiSafepayIdeal(paymentMethodCode) {
9+
const performPlaceOrder = usePerformPlaceOrder(paymentMethodCode);
10+
const placeOrderWithIdeal = useCallback(
11+
async (values) => {
12+
const issuerId = get(values, issuersField);
13+
const additionalData = { issuer_id: issuerId };
14+
15+
await performPlaceOrder(values, additionalData);
16+
},
17+
[performPlaceOrder]
18+
);
19+
20+
return { placeOrderWithIdeal };
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { get } from 'lodash';
2+
import { __ } from '@hyva/react-checkout/i18n';
3+
import RootElement from '@hyva/react-checkout/utils/rootElement';
4+
5+
const config = RootElement.getPaymentConfig();
6+
const ideal = get(config, 'multisafepay_ideal');
7+
const issuers = get(ideal, 'issuers', []);
8+
9+
const idealConfig = {
10+
issuers: issuers.map(({ code, description }) => ({
11+
value: code,
12+
label: __(description),
13+
})),
14+
};
15+
16+
export default idealConfig;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import IdealComponent from './IdealComponent';
2+
3+
export default IdealComponent;

0 commit comments

Comments
 (0)