@a15-ghm/gh-payment

1.0.0 • Public • Published

gh-payment

NPM package with payment methods

gh-payment is a package that includes different types of payment utility functions. Using gh-payment you can interact with different utilities for the processes of calculating, creating payment data and http requests to the TouchCR API.

Installation

  1. Install the gh-payment package by running this command
npm install --save @a15-ghm/gh-payment

Once gh-payment is installed, all packages in peerDependencies will also be installed (gh-utils and gh-models).

Usage

To use gh-payment, you need to set the environment variables. Import Environment from gh-utils package and pass BRAND and API_URL. It needs for setting headers to requests and making calls to the TouchCR API.

import Environment from '@a15-ghm/gh-utils/environment';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent implements OnInit {
  public ngOnInit() {
    Environment.setEnvVariables({ BRAND: 'brand', API_URL: 'api_url' });
  }
}

The above steps are basic usage instructions, below is a more detailed description of each part of the gh-payment package functionality

Coupon utils

.calculateCouponDiscount()

Claculates and returns the coupon discount depending on coupon info and subtotal

// The signature
function calculateCouponDiscount(
  cartOverValue: number,
  discountType: string,
  discountAmount: number,
  subtotal: number,
): number;

// Usage
import { calculateCouponDiscount } from '@a15-ghm/gh-payment/coupon';

const couponDiscount = calculateCouponDiscount(100, 'Discount %', 10, 299);

.getCouponInfo()

Makes a call to the TouchCR API to verify an existing coupon, then check the eligibility of the coupon - whether the coupon can be applied to products in the cart. Throws an error in case of any problems. If there are no errors and the coupon is eligible, it will return the coupon information from the TouchCR API.

// The signature
async function getCouponInfo(couponCode: string, productCartIds: string[]): Promise<ICouponResponse>;

// Usage
import { getCouponInfo } from '@a15-ghm/gh-payment/coupon';

const couponInfo = await getCouponInfo('Coupon_Code', ['01t0m000004HiNTAA0', '21F0m011104HiNTAA0']);

Shipping utils

.getShippingCost()

It is used to calculate shipping costs and it can return a default shipping cost of 0 if there are no variants in the cart, or it can return an international/domestic cost depending on the shipping country. If the shipping country is the United States or shipping is falsy, it can return 4.95 (domestic) or 0 (domestic free) depending on the incoming arguments freeShippingThreshold, isLoggedIn and cartSubtotal. FreeShippingThreshold is optional parameter, if you don't pass it, it will use 0

Example:

// The signature
function getShippingCost(
  country: string,
  cart: ICart,
  cartSubtotal: number,
  isLoggedIn: boolean,
  freeShippingThreshold = 0,
): ShippingCost;

// Usage
import { getShippingCost } from '@a15-ghm/gh-payment/shipping';

const shippingCost = getShippingCost('United States', { products: new Map(), variants: new Map() }, 100, true);

Tax utils

.getTax()

Creates a tax request body and makes a call to the TouchCR API to getting the tax. If the cart is empty, the shipping postal code is falsy, or the subtotal amount is 0, a default tax (0) will be returned. The shipping address incoming argument should contain the country and state in the form of a country code and state code

// The signature
async function getTax(
  shippingAddress: IAddress, // {  country: 'US', state: 'CA', city: 'City', street: 'Street 14', postal: '456123'}
  subtotal: number,
  cart: ICart,
  affiliateInfo: IAffiliateInfo,
  shippingCost: number,
  couponDiscount: number,
  cartCount: number,
): Promise<ITouchcrTax>;

// Usage
import { getTax } from '@a15-ghm/gh-payment/tax';

const tax: ITouchcrTax = await getTax(
  shippingAddress,
  subtotal,
  cart,
  affiliateInfo,
  shippingCost,
  couponDiscount,
  cartCount,
);

.getUpsellTax()

Creates a tax request body and makes a call to the TouchCR API for getting upsell tax. The shipping address incoming argument should contain the country and state in the form of a country code and state code.

// The signature
async function getUpsellTax(
  shippingAddress: IAddress, // {  country: 'US', state: 'CA', city: 'City', street: 'Street 14', postal: '456123'}
  affiliateInfo: IAffiliateInfo,
  upsell: IUpsell,
): Promise<ITouchcrTax>;

// Usage
import { getUpsellTax } from '@a15-ghm/gh-payment/tax';

const tax: ITouchcrTax = await getUpsellTax(shippingAddress, affiliateInfo, upsell);

.fetchTax()

Makes a call to the TouchCR API for getting tax. The account details in tax request body should contain the country and state in the form of a country code and state code.

// The signature
async function fetchTax(body: ITaxRequestBody): Promise<ITouchcrTax>;

// Usage
import { fetchTax } from '@a15-ghm/gh-payment/tax';

const tax: ITouchcrTax = await fetchTax(taxRequestBody);

Payment utils

.checkoutLoggedIn()

Makes a call to the TouchCR API for creation the payment for logged in user

// The signature
async function checkoutLoggedIn(paymentBody: IPaymentBody | IManualPaymentBody): Promise<ILoggedInPaymentResponse>;

// Usage
import { checkoutLoggedIn } from '@a15-ghm/gh-payment/payment';

const response = await checkoutLoggedIn(paymentBody);

.checkoutGuest()

Makes a call to the TouchCR API for creation the payment for not logged in user

// The signature
async function checkoutGuest(guestPaymentBody: IPaymentBody | IManualPaymentBody): Promise<IPaymentResponse>;

// Usage
import { checkoutGuest } from '@a15-ghm/gh-payment/payment';

const response = await checkoutGuest(guestPaymentBody);

.getManualCheckoutPaymentBody()

Returns a body for payment requests to the TouchCR API with credit card info

// The signature
async function getManualCheckoutPaymentBody(
  paymentDetails: IPaymentDetailsPaymentBody,
  cart: ICart,
  customerDetails: ICustomerDetails,
  creditCard: ICreditCardInfo,
  affiliateInfo: IAffiliateInfo,
  isGuest: boolean,
  sessionId: string | null,
  useSeparateBillingAddress = true,
): IManualPaymentBody;

// Usage
import { getManualCheckoutPaymentBody } from '@a15-ghm/gh-payment/payment';

const response = await getManualCheckoutPaymentBody(
  paymentDetails,
  cart,
  customerDetails,
  creditCard,
  affiliateInfo,
  isGuest,
  sessionId,
  useSeparateBillingAddress,
);

.getCheckoutPaymentBody()

Returns a body for payment requests to the TouchCR API

// The signature
async function getCheckoutPaymentBody(
  paymentDetails: IPaymentDetailsPaymentBody,
  cart: ICart,
  customerDetails: ICustomerDetails,
  affiliateInfo: IAffiliateInfo,
  sessionId: string,
  isGuest: boolean,
  useSeparateBillingAddress = true,
): IPaymentBody;

// Usage
import { getCheckoutPaymentBody } from '@a15-ghm/gh-payment/payment';

const response = await getCheckoutPaymentBody(
  paymentDetails,
  cart,
  customerDetails,
  affiliateInfo,
  sessionId,
  isGuest,
  (useSeparateBillingAddress = true),
);

PayPal utils

.getRedirectUrls()

Returns a object with redirect urls for PayPal. This object contains a url for canceling payment and a url for executing payment

// The signature
function getRedirectUrls(): IPayPalRedirectUrls;

// Usage
import { getRedirectUrls } from '@a15-ghm/gh-payment/paypal';

const redirectUrls = getRedirectUrls();

.initPayPalReq()

Creates a initial call to checkout an order and redirect to PayPal for auth

// The signature
async function initPayPalReq(paymentBody: IPaymentBody): Promise<IPayPalPaymentResponse>;

// Usage
import { initPayPalReq } from '@a15-ghm/gh-payment/paypal';

const response = await initPayPalReq(paymentBody);

.executePayPalReq()

Once we return from PayPal, we will have to actually execute the checkout

// The signature
async function executePayPalReq(
  orderDetails: IPaymentResponse,
  taxData: ITouchcrTax,
): Promise<IExecutePayPalPaymentResponse>;

// Usage
import { executePayPalReq } from '@a15-ghm/gh-payment/paypal';

const response = await executePayPalReq(orderDetails, taxData);

.initPayPalUpsellReq()

Creates a initial call to checkout an upsell order

// The signature
async function initPayPalUpsellReq(payPalUpsellPaymentBody: IPayPalUpsellPaymentBody): Promise<IPayPalPaymentResponse>;

// Usage
import { initPayPalUpsellReq } from '@a15-ghm/gh-payment/paypal';

const response = await initPayPalUpsellReq(payPalUpsellPaymentBody);

Readme

Keywords

none

Package Sidebar

Install

npm i @a15-ghm/gh-payment

Weekly Downloads

0

Version

1.0.0

License

none

Unpacked Size

36 kB

Total Files

36

Last publish

Collaborators

  • devops-ghm-npm
  • danni.liu
  • s.bely
  • kiskina_victoria
  • sergey.kuzovlev
  • brian.park