2020-04-30 15:28:48 +03:00
|
|
|
import onApprove from '../OnApproveHandler/onApproveForContinue.js';
|
|
|
|
import {payerData} from "../Helper/PayerData";
|
2020-04-08 12:33:34 +03:00
|
|
|
|
2020-04-10 10:34:49 +03:00
|
|
|
class CartActionHandler {
|
2020-04-08 12:33:34 +03:00
|
|
|
|
|
|
|
constructor(config, errorHandler) {
|
|
|
|
this.config = config;
|
|
|
|
this.errorHandler = errorHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
configuration() {
|
|
|
|
const createOrder = (data, actions) => {
|
2020-04-28 09:14:05 +03:00
|
|
|
const payer = payerData();
|
2020-07-15 19:49:32 +03:00
|
|
|
const bnCode = typeof this.config.bn_codes[this.config.context] !== 'undefined' ?
|
|
|
|
this.config.bn_codes[this.config.context] : '';
|
2020-04-08 12:33:34 +03:00
|
|
|
return fetch(this.config.ajax.create_order.endpoint, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
nonce: this.config.ajax.create_order.nonce,
|
2020-04-09 19:15:28 +03:00
|
|
|
purchase_units: [],
|
2020-07-15 19:49:32 +03:00
|
|
|
bn_code:bnCode,
|
2020-04-28 09:14:05 +03:00
|
|
|
payer
|
2020-04-09 19:15:28 +03:00
|
|
|
}),
|
|
|
|
}).then(function(res) {
|
2020-04-08 12:33:34 +03:00
|
|
|
return res.json();
|
2020-04-09 19:15:28 +03:00
|
|
|
}).then(function(data) {
|
2020-04-08 12:33:34 +03:00
|
|
|
if (!data.success) {
|
2020-04-23 17:05:44 +03:00
|
|
|
throw Error(data.data);
|
2020-04-08 12:33:34 +03:00
|
|
|
}
|
|
|
|
return data.data.id;
|
|
|
|
});
|
2020-04-09 19:15:28 +03:00
|
|
|
};
|
|
|
|
|
2020-04-08 12:33:34 +03:00
|
|
|
return {
|
|
|
|
createOrder,
|
2020-07-15 11:15:07 +03:00
|
|
|
onApprove: onApprove(this, this.errorHandler),
|
2020-04-08 12:33:34 +03:00
|
|
|
onError: (error) => {
|
|
|
|
this.errorHandler.message(error);
|
2020-04-10 10:34:49 +03:00
|
|
|
}
|
2020-04-09 19:15:28 +03:00
|
|
|
};
|
2020-04-08 12:33:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 17:05:44 +03:00
|
|
|
export default CartActionHandler;
|