woocommerce-paypal-payments/modules.local/ppcp-button/resources/js/modules/ActionHandler/CartActionHandler.js

45 lines
1.3 KiB
JavaScript
Raw Normal View History

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
class CartActionHandler {
2020-04-08 12:33:34 +03:00
constructor(config, errorHandler) {
this.config = config;
this.errorHandler = errorHandler;
}
configuration() {
const createOrder = (data, actions) => {
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,
purchase_units: [],
2020-07-15 19:49:32 +03:00
bn_code:bnCode,
payer
}),
}).then(function(res) {
2020-04-08 12:33:34 +03:00
return res.json();
}).then(function(data) {
2020-04-08 12:33:34 +03:00
if (!data.success) {
throw Error(data.data);
2020-04-08 12:33:34 +03:00
}
return data.data.id;
});
};
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-08 12:33:34 +03:00
}
}
export default CartActionHandler;