NPM integration
The preferred method of integrating ChargeAfter SDK is via NPM.
Our NPM package is always up-to-date, is strongly typed (Typescript), and optimized modern frameworks.
Our NPM package is usable in one of two ways:
- Import the
initialize
method and use to create an SDK instance - Preferred (see below) - import
checkout
andprequalify
which accept initialization parameters, and use directly
Installing
// using NPM
npm install @chargeafter/payment-sdk
// using yarn
yarn add install @chargeafter/payment-sdk
Getting Started
import { EnvironmentType, initialize } from "@chargeafter/payment-sdk";
import * as React from "react";
import * as ReactDOM from "react-dom";
const CheckoutButton = () => {
const cfg = {
//optional - use in case of more than a single store
storeId: "<storeId>",
apiKey: "<your api key>",
// Optional:
delegatedMerchantId: "<merchant id to act on behalf of>",
// Optional:
preferences: {
// Optional: default en
language: "en",
// Optional: true/false to opt in to marketing or optout
marketingOptIn: true,
// Optional:
currency: "<merchant currency, e.g USD>",
// Optional:
// used to determine language and currency
// if they are not provided
country: "<merchant country>"
}
};
const onClick = async () => {
try {
// initialize is called with config as first parameter, and environment name as second.
// if not passed, production is assumed.
const caObject = await initialize(cfg, "sandbox");
// caObject.payments.present("apply");
// caObject.payments.present("checkout", {
// callback: () => {},
// cartDetails: {
// items: [],
// taxAmount: 5,
// shippingAmount: 6,
// totalAmount: 7
// }
// });
} catch (ex) {
console.log(ex.message);
}
};
return (
<button onClick={onClick}>
Click to checkout
</button>
);
};
import { checkout, ICheckoutProps } from "@chargeafter/payment-sdk";
const checkoutButton = () => {
const cfg: ICheckoutProps = {
config: {
//optional - use in case of more than a single store
storeId: "<storeId>",
env: {
name: "qa",
apiKey: "<your api key>"
}
},
callback: () => {},
cartDetails: {
items: [],
taxAmount: 5,
shippingAmount: 6,
totalAmount: 7
}
};
const onClick = async () => {
try {
const { consumerId } = await checkout(cfg);
console.log(consumerId);
} catch (ex) {
console.log(ex.message);
}
};
return <button dataButtonType="small-generic" onClick={onClick}>Click to checkout</button>;
};
All types are exported from the package for your convinience.
See following sections for exact interfaces of checkout and apply functionality.
Updated 8 months ago