1.1.0 • Published 5 years ago

@janiscommerce/totem-checkout v1.1.0

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

@janiscommerce/totem-checkout

Installation

npm install @janiscommerce/totem-checkout

API

Usage

const { Payment, Cart, Order } = require('@janiscommerce/totem-checkout');

const cartInstance = new Cart({ environment: 'tiendaqa' })

const orderInstance = new Order({ environment: 'tiendaqa', apiKey: 'API_KEY', apiToken: 'API_TOKEN' });

const paymentInstance = new Payment({ environment: 'tiendaqa', apiKey: 'API_KEY', apiToken: 'API_TOKEN', slaId: 'SLA_ID' });

Cart

Methods

addItems(items) ⇒ Promise

Function for add a new item or items to current order

ParamTypeDescription
itemsarray/objectObject or Array with Object with id and quantity
items.idnumberid of sku product.
items.quantitynumberquantity items

Example

const cart = await cartInstance.addItems({
	id: 45,
	quantity: 2
});

const cart = await cartInstance.addItems([{
	id: 45,
	quantity: 2
},{
	id: 45,
	quantity: 2
}]);

changeItemQuantity(items) ⇒ Promise

Function for change the quantity to item into the current orderForm

ParamTypeDescription
itemsarray/objectObject or Array with Object with id and quantity
items.idnumberid of sku product.
items.quantitynumberquantity items

Example

const cart = await cartInstance.changeItemsQuantity({
	id: 45,
	quantity: 2
});

const cart = await cartInstance.changeItemsQuantity([{
	id: 45,
	quantity: 5
},{
	id: 45,
	quantity: 8
}]);

removeItems(items) ⇒ Promise

Function for remove items

ParamTypeDescription
itemsarray/objectObject or Array with Object with id and quantity
items.idnumberid of sku product.

Example

const cart = await cartInstance.removeItems({
		id: 44
});

const cart = await cartInstance.removeItems([{
	id: 45
},{
	id: 46
}]);

removeAllItems() ⇒ Promise

Function for remove all items Example

const cart = await CartInstance.removeAllItems();

simulate(items) ⇒ Promise

Function for simulate cart

ParamTypeDescription
itemsarray/objectObject or Array with Object with id and quantity
items.idnumberid of sku product.
items.quantitynumberquantity items
postalCodenumberPostalCoode for get availables slas

Example

const cartSimulated = await cartInstance.simulate({
    id: 17,
    quantity: 2
}, 1000);

 const cartSimulated = await cartInstance.simulate([{
    id: 17,
    quantity: 2
}], 1000);

clearCart()

Function for clear cart cached in instance then call getOrder method Example

await cartInstance.getCart();
// into cartInstance.cart getCart method save cart created

cartInstance.clearCart();
// cartInstance.cart is null

Order

Methods

create(data) ⇒ Promise

Create new Order

ParamTypeDescription
dataobjectObject with data necesary for make order
data.cartobjectObject Cart or Object Cart simulation
data.slaIdstring/logistic sla id
data.paymentSystemIdnumber/stringid of payment system

If order is created, the order response and passed data is cached in instance

Example

// Create order with payment promisory
await orderInstance.create({
	cart: orderSimulated,
	slaId: 'Normal',
	paymentSystemId: 201
});

// Create order with payment card
await orderInstance.create({
	cart: orderSimulated,
	slaId: 'Normal',
	paymentSystemId: 201,
	paymentFields: {
		holderName: 'Test TEST',
		cardNumber: '4700000000000000',
		validationCode: '',
		dueDate: "12/22",
		document: '',
		accountId: '',
		address: null,
		callbackUrl: ''
	}
});

sendTransactionPayments(data, transactionId) ⇒ Promise

Send to order created the paymentData

ParamTypeDescription
dataobjectObject with data necesary for make order
data.cartobjectObject Cart or Object Cart simulation
data.slaIdstring/logistic sla id
data.paymentSystemIdnumber/stringid of payment system
transactionIdstringid of transaction

if not pass data and transactionId, the function use data passed in method create, saved in instance

Example

// Send transaction custom
const data = {
	cart: orderSimulated,
	slaId: 'Normal',
	paymentSystemId: 201
};

await orderInstance.sendTransactionPayments(data, 'TRANSACTION_ID');

// send transaction with data and transaction id cached
await orderInstance.sendTransactionPayments();

authorizeTransaction(data) ⇒ Promise

Authorize Payment Methods order

ParamTypeDescription
transactionIdstringid of transaction

if not pass transactionId, the function use transactionId in cached order, saved in instance

Example

// authorizeTransaction custom
await orderInstance.authorizeTransaction('TRANSACTION_ID');

// authorizeTransaction with transactionId in order cached
await orderInstance.authorizeTransaction();

clearOrder()

Clear order created cached and data initial passed Example

await orderInstance.create({
	cart: orderSimulated,
	slaId: 'Normal',
	paymentSystemId: 201
});
// into orderInstance.order, create method save order created
// Into orderInstace.initialdata, create method save argument data passed

orderInstance.clearOrder();
// orderInstance.order is null
// orderInstance.initialData is null

Creating a Order

Example for simulate cart, and then create cart and send and authorize payment

const { Cart, Order } = require('totem-app-checkout');

const evironment = 'testqa';
const cart = new Cart({ environment });

const order = new Order({
	environment,
	apiKey: 'API_KEY',
	apiToken: 'API_TOKEN'
});

const items = [
	{ id: 8, quantity: 1 },
	{ id: 7, quantity: 1 }
];

const cartSimulated = await cart.simulate(items, 1000)


const data = await order.create({
	cart: orderSimulated,
	slaId: 'Normal',
	paymentSystemId: 201,
	paymentFields: {
		holderName: 'Test VTEX',
		cardNumber: '4704550000000005',
		validationCode: '',
		dueDate: "12/22",
		document: '',
		accountId: '',
		address: null,
		callbackUrl: ''
	}
});

const sendPaymentResponse = await order.sendTransactionPayments();

const authorizeResponse = await order.authorizeTransaction();

Payment

Methods

collect(value, paymentSystem, transactionId, cart, isCash, isContactless) ⇒ Promise

Function for add a new item or items to current order

ParamTypeDescriptionDefault
valuenumberValue cart total
paymentSystemstring|numberid of payment
transactionIdstringid of transaction
cartobjectobject cart simulated
isCashbooleanis payment in cash or notfalse
isContactlessbooleanis payment with card contaclessfalse

Creating a payment

  // payment with transaction id with card
  await paymentInstance.collect(18300, 2, 'TRANSACTION_ID');

  // payment with cart simlated with card
  await paymentInstance.collect(18300, 2, null, cartSimulatedObj);

  // payment with transaction id in cash
  await paymentInstance.collect(18300, 1, 'TRANSACTION_ID', null, true);

  // payment with cart simlated in cash
  await paymentInstance.collect(18300, 1, null, cartSimulatedObj, true);