mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-18 07:52:23 +01:00
process payements with payzen
This commit is contained in:
parent
ef7dcd37d8
commit
dca2651fb3
@ -1,4 +1,4 @@
|
|||||||
import React, { FormEvent, useEffect } from 'react';
|
import React, { FormEvent, FunctionComponent, useEffect, useRef, useState } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import KRGlue from "@lyracom/embedded-form-glue";
|
import KRGlue from "@lyracom/embedded-form-glue";
|
||||||
import { CartItems } from '../../../models/payment';
|
import { CartItems } from '../../../models/payment';
|
||||||
@ -6,6 +6,7 @@ import { User } from '../../../models/user';
|
|||||||
import SettingAPI from '../../../api/setting';
|
import SettingAPI from '../../../api/setting';
|
||||||
import { SettingName } from '../../../models/setting';
|
import { SettingName } from '../../../models/setting';
|
||||||
import PayzenAPI from '../../../api/payzen';
|
import PayzenAPI from '../../../api/payzen';
|
||||||
|
import { Loader } from '../../base/loader';
|
||||||
|
|
||||||
interface PayzenFormProps {
|
interface PayzenFormProps {
|
||||||
onSubmit: () => void,
|
onSubmit: () => void,
|
||||||
@ -26,6 +27,8 @@ interface PayzenFormProps {
|
|||||||
export const PayzenForm: React.FC<PayzenFormProps> = ({ onSubmit, onSuccess, onError, children, className, paymentSchedule = false, cartItems, customer, operator, formId }) => {
|
export const PayzenForm: React.FC<PayzenFormProps> = ({ onSubmit, onSuccess, onError, children, className, paymentSchedule = false, cartItems, customer, operator, formId }) => {
|
||||||
|
|
||||||
const { t } = useTranslation('shared');
|
const { t } = useTranslation('shared');
|
||||||
|
const PayZenKR = useRef(null);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const api = new SettingAPI();
|
const api = new SettingAPI();
|
||||||
@ -34,25 +37,39 @@ export const PayzenForm: React.FC<PayzenFormProps> = ({ onSubmit, onSuccess, onE
|
|||||||
KRGlue.loadLibrary(settings.get(SettingName.PayZenEndpoint), settings.get(SettingName.PayZenPublicKey)) /* Load the remote library */
|
KRGlue.loadLibrary(settings.get(SettingName.PayZenEndpoint), settings.get(SettingName.PayZenPublicKey)) /* Load the remote library */
|
||||||
.then(({ KR }) =>
|
.then(({ KR }) =>
|
||||||
KR.setFormConfig({
|
KR.setFormConfig({
|
||||||
/* set the minimal configuration */
|
|
||||||
formToken: formToken.formToken,
|
formToken: formToken.formToken,
|
||||||
"kr-language": "en-US" /* to update initialization parameter */
|
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
.then(({ KR }) =>
|
.then(({ KR }) => KR.addForm("#payzenPaymentForm"))
|
||||||
KR.addForm("#payzenPaymentForm")
|
.then(({ KR, result }) => KR.showForm(result.formId))
|
||||||
) /* add a payment form to myPaymentForm div*/
|
.then(({ KR }) => KR.onFormReady(handleFormReady))
|
||||||
.then(({ KR, result }) =>
|
.then(({ KR }) => PayZenKR.current = KR);
|
||||||
KR.showForm(result.formId)
|
}).catch(error => onError(error));
|
||||||
); /* show the payment form */
|
});
|
||||||
}).catch(error => console.error(error));
|
}, [cartItems, paymentSchedule, customer]);
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
const submitEmbeddedPayzenForm = (): void => {
|
/**
|
||||||
const button: HTMLButtonElement = document.querySelector('button.kr-payment-button');
|
* Callback triggered on PayZen successful payments
|
||||||
button.setAttribute('type', 'button');
|
*/
|
||||||
button.click()
|
const onPaid = (event) => {
|
||||||
|
if (event.clientAnswer.orderStatus === 'PAID') {
|
||||||
|
PayZenKR.current.removeForms();
|
||||||
|
onSuccess(event.clientAnswer);
|
||||||
|
} else {
|
||||||
|
onError(event.clientAnswer);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleFormReady = () => {
|
||||||
|
setLoading(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback triggered when the PayZen payment was refused
|
||||||
|
*/
|
||||||
|
const handleError = (answer) => {
|
||||||
|
const message = `${answer.errorMessage}. ${answer.detailedErrorMessage ? answer.detailedErrorMessage : ''}`;
|
||||||
|
onError(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -62,20 +79,30 @@ export const PayzenForm: React.FC<PayzenFormProps> = ({ onSubmit, onSuccess, onE
|
|||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
onSubmit();
|
onSubmit();
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
submitEmbeddedPayzenForm();
|
const { result } = await PayZenKR.current.validateForm();
|
||||||
onSuccess(null);
|
if (result === null) {
|
||||||
|
PayZenKR.current.onSubmit(onPaid);
|
||||||
|
PayZenKR.current.onError(handleError);
|
||||||
|
await PayZenKR.current.submit();
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// catch api errors
|
// catch api errors
|
||||||
onError(err);
|
onError(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const Loader: FunctionComponent = () => {
|
||||||
|
return (
|
||||||
|
<div className="fa-3x loader">
|
||||||
|
<i className="fas fa-circle-notch fa-spin" />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<form onSubmit={handleSubmit} id={formId} className={className ? className : ''}>
|
<form onSubmit={handleSubmit} id={formId} className={className ? className : ''}>
|
||||||
|
{loading && <Loader />}
|
||||||
<div className="container">
|
<div className="container">
|
||||||
<div id="payzenPaymentForm" />
|
<div id="payzenPaymentForm" />
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,13 +1,16 @@
|
|||||||
.payzen-modal {
|
.payzen-modal {
|
||||||
.payzen-form {
|
.payzen-form {
|
||||||
|
.loader {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
.container {
|
.container {
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
width: inherit;
|
width: inherit;
|
||||||
|
|
||||||
//.kr-payment-button {
|
.kr-payment-button {
|
||||||
// display: none;
|
display: none;
|
||||||
//}
|
}
|
||||||
|
|
||||||
.kr-form-error {
|
.kr-form-error {
|
||||||
display: none;
|
display: none;
|
||||||
|
@ -21,7 +21,7 @@ class PayZen::Helper
|
|||||||
require 'sha3'
|
require 'sha3'
|
||||||
|
|
||||||
content = { cart_items: cart_items, customer: customer }.to_json + DateTime.current.to_s
|
content = { cart_items: cart_items, customer: customer }.to_json + DateTime.current.to_s
|
||||||
SHA3::Digest.hexdigest(:sha256, content)
|
SHA3::Digest.hexdigest(:sha256, content)[0...9]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user