2021-11-22 17:51:50 +00:00
|
|
|
import { CartBaseController } from "./cart_base_controller";
|
2021-10-27 19:12:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Al pagar lo que podamos, primero hay que crear una orden y luego
|
|
|
|
* contactarse con la APIv2 para generar la variante con el precio que
|
|
|
|
* queramos agregar. Agregamos la variante al carrito y lanzamos el
|
|
|
|
* proceso de pago.
|
|
|
|
*/
|
|
|
|
export default class extends CartBaseController {
|
2021-11-22 17:51:50 +00:00
|
|
|
static targets = ["form"];
|
2021-10-27 19:12:18 +00:00
|
|
|
static values = {
|
|
|
|
variantId: Number,
|
|
|
|
currency: String,
|
|
|
|
price: Number,
|
2021-11-22 17:51:50 +00:00
|
|
|
firstname: String,
|
|
|
|
};
|
2021-10-27 19:12:18 +00:00
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
connect() {
|
2021-10-27 19:12:18 +00:00
|
|
|
this.paymentMethodByCurrency = {
|
2021-11-22 17:51:50 +00:00
|
|
|
ARS: "Spree::PaymentMethod::MercadoPago",
|
|
|
|
USD: "Spree::PaymentMethod::Paypal",
|
|
|
|
};
|
2021-10-27 19:12:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
store(event) {
|
|
|
|
const target = event.currentTarget || event.target;
|
2021-10-27 19:12:18 +00:00
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
this[`${target.dataset.name}Value`] = target.value;
|
2021-10-27 19:12:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
set formDisable(disable) {
|
|
|
|
this.formTarget.elements.forEach((x) => (x.disabled = disable));
|
2021-10-27 19:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Realiza todos los pasos:
|
|
|
|
*
|
|
|
|
* * Crear pedido
|
|
|
|
* * Crear variante con el monto y moneda
|
|
|
|
* * Agregar al pedido
|
|
|
|
* * Agregar dirección al pedido
|
|
|
|
* * Obtener métodos de envío
|
|
|
|
* * Obtener métodos de pago
|
|
|
|
* * Pagar
|
|
|
|
* * Reenviar a confirmación
|
|
|
|
* * Ejecutar el pago (si aplica)
|
|
|
|
*/
|
2021-11-22 17:51:50 +00:00
|
|
|
async pay(event = undefined) {
|
2021-10-27 19:12:18 +00:00
|
|
|
if (event) {
|
2021-11-22 17:51:50 +00:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2021-10-27 19:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.formTarget.checkValidity()) {
|
2021-11-22 17:51:50 +00:00
|
|
|
this.formTarget.classList.add("was-validated");
|
|
|
|
return;
|
2021-10-27 19:12:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
this.formDisable = true;
|
2021-10-27 19:12:18 +00:00
|
|
|
|
|
|
|
// Crear pedido. Todos los pedidos van a ser hechos desde
|
|
|
|
// Argentina, no hay forma de cambiar esto.
|
2021-11-22 17:51:50 +00:00
|
|
|
const orderToken = await this.tempCartCreate();
|
|
|
|
const quantity = 1;
|
|
|
|
const include = "line_items";
|
|
|
|
const currency = this.currencyValue;
|
|
|
|
const price = this.priceValue;
|
|
|
|
const email = "noreply@sutty.nl";
|
|
|
|
const firstname = this.firstnameValue;
|
|
|
|
const lastname = "-";
|
|
|
|
const address1 = "-";
|
|
|
|
const country_id = 250; // XXX: Internet
|
|
|
|
const city = "-";
|
|
|
|
const phone = "11111111";
|
|
|
|
const zipcode = "1111";
|
|
|
|
const ship_address_attributes = {
|
|
|
|
firstname,
|
|
|
|
lastname,
|
|
|
|
address1,
|
|
|
|
city,
|
|
|
|
country_id,
|
|
|
|
zipcode,
|
|
|
|
phone,
|
|
|
|
};
|
|
|
|
const bill_address_attributes = ship_address_attributes;
|
|
|
|
const confirmation_delivered = true;
|
|
|
|
const custom_return_url = this.customReturnUrl();
|
|
|
|
|
|
|
|
let variant_id = this.variantIdValue;
|
2021-10-27 19:12:18 +00:00
|
|
|
|
|
|
|
// Crear la variante
|
2021-11-22 17:51:50 +00:00
|
|
|
const payWhatYouCanResponse = await this.spree.sutty.payWhatYouCan(
|
|
|
|
{ orderToken },
|
|
|
|
{ variant_id, price, currency, quantity }
|
|
|
|
);
|
2021-10-27 19:12:18 +00:00
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
variant_id = payWhatYouCanResponse.data.id;
|
2021-10-27 19:12:18 +00:00
|
|
|
|
|
|
|
if (!variant_id) {
|
2021-11-22 17:51:50 +00:00
|
|
|
this.formDisable = false;
|
|
|
|
console.error("No se pudo generar la variante", {
|
|
|
|
variant_id,
|
|
|
|
price,
|
|
|
|
currency,
|
|
|
|
quantity,
|
|
|
|
});
|
|
|
|
return;
|
2021-10-27 19:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Configurar la moneda del pedido
|
2021-11-22 17:51:50 +00:00
|
|
|
let response = await this.spree.sutty.updateOrder(
|
|
|
|
{ orderToken },
|
|
|
|
{ currency, confirmation_delivered, custom_return_url }
|
|
|
|
);
|
2021-10-27 19:12:18 +00:00
|
|
|
|
|
|
|
if (response.status > 299) {
|
2021-11-22 17:51:50 +00:00
|
|
|
console.error(response);
|
|
|
|
this.formDisable = false;
|
|
|
|
return;
|
2021-10-27 19:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Agregar al carrito
|
2021-11-22 17:51:50 +00:00
|
|
|
response = await this.spree.cart.addItem(
|
|
|
|
{ orderToken },
|
|
|
|
{ variant_id, quantity, include }
|
|
|
|
);
|
2021-10-27 19:12:18 +00:00
|
|
|
|
|
|
|
if (response.isFail()) {
|
2021-11-22 17:51:50 +00:00
|
|
|
this.handleFailure(response);
|
|
|
|
this.formDisable = false;
|
|
|
|
return;
|
2021-10-27 19:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Actualizar la dirección
|
2021-11-22 17:51:50 +00:00
|
|
|
response = await this.spree.checkout.orderUpdate(
|
|
|
|
{ orderToken },
|
|
|
|
{ order: { email, ship_address_attributes, bill_address_attributes } }
|
|
|
|
);
|
2021-10-27 19:12:18 +00:00
|
|
|
|
|
|
|
if (response.isFail()) {
|
2021-11-22 17:51:50 +00:00
|
|
|
this.handleFailure(response);
|
|
|
|
this.formDisable = false;
|
|
|
|
return;
|
2021-10-27 19:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Obtener los medios de envío
|
2021-11-22 17:51:50 +00:00
|
|
|
response = await this.spree.checkout.shippingMethods(
|
|
|
|
{ orderToken },
|
|
|
|
{ include: "shipping_rates" }
|
|
|
|
);
|
2021-10-27 19:12:18 +00:00
|
|
|
|
|
|
|
if (response.isFail()) {
|
2021-11-22 17:51:50 +00:00
|
|
|
this.handleFailure(response);
|
|
|
|
this.formDisable = false;
|
|
|
|
return;
|
2021-10-27 19:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Elegir medio de envío
|
2021-11-22 17:51:50 +00:00
|
|
|
response = await this.spree.checkout.orderUpdate(
|
|
|
|
{ orderToken },
|
|
|
|
{
|
|
|
|
order: {
|
|
|
|
shipments_attributes: [
|
|
|
|
{
|
|
|
|
id: response.success().data[0].id,
|
|
|
|
selected_shipping_rate_id: response
|
|
|
|
.success()
|
|
|
|
.included.filter((x) => x.type == "shipping_rate")[0].id,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2021-10-27 19:12:18 +00:00
|
|
|
}
|
2021-11-22 17:51:50 +00:00
|
|
|
);
|
2021-10-27 19:12:18 +00:00
|
|
|
|
|
|
|
// Elegir medio de pago
|
2021-11-22 17:51:50 +00:00
|
|
|
response = await this.spree.checkout.paymentMethods({ orderToken });
|
2021-10-27 19:12:18 +00:00
|
|
|
|
|
|
|
if (response.isFail()) {
|
2021-11-22 17:51:50 +00:00
|
|
|
this.handleFailure(response);
|
|
|
|
this.formDisable = false;
|
|
|
|
return;
|
2021-10-27 19:12:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
const payment_method_id = response
|
|
|
|
.success()
|
|
|
|
.data.find(
|
|
|
|
(x) =>
|
|
|
|
this.paymentMethodByCurrency[this.currencyValue] == x.attributes.type
|
|
|
|
).id;
|
2021-10-27 19:12:18 +00:00
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
response = await this.spree.checkout.orderUpdate(
|
|
|
|
{ orderToken },
|
2021-10-27 19:12:18 +00:00
|
|
|
{
|
|
|
|
order: { payments_attributes: [{ payment_method_id }] },
|
|
|
|
payment_source: {
|
|
|
|
[payment_method_id]: {
|
2021-11-22 17:51:50 +00:00
|
|
|
name: "Pepitx",
|
2021-10-27 19:12:18 +00:00
|
|
|
month: 12,
|
2021-11-22 17:51:50 +00:00
|
|
|
year: 2021,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2021-10-27 19:12:18 +00:00
|
|
|
|
|
|
|
if (response.isFail()) {
|
2021-11-22 17:51:50 +00:00
|
|
|
this.handleFailure(response);
|
|
|
|
this.formDisable = false;
|
|
|
|
return;
|
2021-10-27 19:12:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
response = await this.spree.checkout.complete({ orderToken });
|
2021-10-27 19:12:18 +00:00
|
|
|
|
|
|
|
if (response.isFail()) {
|
2021-11-22 17:51:50 +00:00
|
|
|
this.handleFailure(response);
|
|
|
|
this.formDisable = false;
|
|
|
|
return;
|
2021-10-27 19:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reenviar al medio de pago
|
2021-11-22 17:51:50 +00:00
|
|
|
const checkoutUrls = await this.spree.sutty.getCheckoutURL({ orderToken });
|
|
|
|
const redirectUrl = checkoutUrls.data[0];
|
2021-10-27 19:12:18 +00:00
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
Turbolinks.visit(redirectUrl);
|
2021-10-27 19:12:18 +00:00
|
|
|
|
|
|
|
// Volver
|
|
|
|
}
|
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
async tempCartCreate() {
|
|
|
|
const response = await this.spree.cart.create();
|
2021-10-27 19:12:18 +00:00
|
|
|
|
|
|
|
// If we fail here it's probably a server error, so we inform the
|
|
|
|
// user.
|
|
|
|
if (response.isFail()) {
|
2021-11-22 17:51:50 +00:00
|
|
|
this.handleFailure(response);
|
|
|
|
return;
|
2021-10-27 19:12:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
return response.success().data.attributes.token;
|
2021-10-27 19:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// @return [String]
|
2021-11-22 17:51:50 +00:00
|
|
|
customReturnUrl() {
|
|
|
|
const url = new URL(window.location.href);
|
|
|
|
url.searchParams.set("open", "");
|
2021-10-27 19:12:18 +00:00
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
return url.toString();
|
2021-10-27 19:12:18 +00:00
|
|
|
}
|
|
|
|
}
|