@migopayments/sdk v2.1.1
๐ฆ Installation
$ npm install @migopayments/sdk
๐ Initialize the SDK
The merchant
and privateKey
are provided by MigoPayments.
import { loadMigo } from "@migopayments/sdk";
const migo = await loadMigo({
merchant: "merchant", // provided by Migo
privateKey: "privateKey", // provided by Migo
environment: "sandbox",
});
Environment | Description |
---|---|
sandbox | Use this to build and test your application ๐งช. |
production | Use this for production. |
The migo
object will expose the multiple methods available in the SDK.
๐งฉ The schema object
The schema object serves a crucial purpose in the SDK by dynamically validating payload information sent by the client. It is custom-tailored to each client, providing a unique framework for organizing and structuring data. By printing it to the console, you can gain valuable insight into how the SDK handles and enforces data validations, making it an indispensable tool for developers.
// print out the schema
console.log(JSON.stringify(migo.schema));
๐ Creating a new transaction
Each method the SDK exposes returns an object with the
data
anderror
properties. Which you can destructure using ES6. If anerror
is present thedata
object will benull
.
To create a new transaction use the createTransaction
method. Make sure to use one of the clients found in the schema.
// create a new transaction
const { data, error } = await migo.createTransaction({
amount: 500,
client: "elektra",
});
// console.log(data?.reference);
// console.log(data?.uid);
๐ณ Creating a payment intent
const { data, error } = await migo.createPaymentIntent("transactionId", payload);
// console.log(data?.challenge);
// console.log(data?.transaction);
As the payload
object varies from client to client, it's important to refer to your schema object to ensure that you're using the correct structure for a payment intent. Usually the payload
object contains the card information, such as card holder name, card number, CVV and expiration date (if applicable). If you don't follow the required format, the SDK will return an error
.
If a challenge is required for the payment intent, it will be returned in the data
object.
๐ฅ Payment intent challenge
3D Secure is a protocol used to provide an additional layer of security for online transactions. When a user makes a payment online with a card that supports 3D Secure, they may be prompted to complete an extra step to authenticate the transaction.
This extra step is typically a challenge, which can take different forms depending on the specific implementation. For example, the challenge may involve entering a one-time code sent to the user's mobile phone or email, answering a security question, or providing biometric authentication such as a fingerprint or facial recognition.
The purpose of the challenge is to verify that the user making the transaction is the authorized cardholder, and to prevent fraudulent transactions. Once the challenge is completed, the transaction can proceed if the authentication is successful.
๐ค Tokenize a card
const card = {
expMonth: '03',
expYear: '25',
cardFullNumber: '411111111111111',
cardCvv: '123',
cardHolderName: 'John',
cardHolderLastName: 'Doe'
}
const { data, error } = await migo.tokenizeCard('transactionId', 'processor', card);
๐ Get the list of tokenized cards
This method returns a list of tokenized cards associated with a specific transaction.
const { data, error } = await migo.getTokenizedCards('transactionId', 'processor');
๐งน Delete a tokenized card
const { data, error } = await migo.deleteTokenizedCard('transactionId', 'processor', 'cardId');
๐ฐ Refund
const { data, error } = await migo.refund('transactionId', amount);
// console.log(data?.transaction);
// console.log(data?.attempt);
๐ Revert a transaction
const { data, error } = await migo.revertTransaction('transactionId');
// console.log(data?.transaction);
๐ฆ Authorization and capture process
The customer initiates a credit/debit card payment intent.
Migo sends a request to the customer's bank (the issuer) to confirm that the customer has sufficient funds in their account to complete the transaction.
If the funds are available, the issuer authorizes the payment and reserves the necessary amount in the customer's account.
Migo sends a request to the merchant's bank (the acquirer) to capture the authorized amount and transfer it to the merchant's account.
If the capture is successful, the payment is considered complete, and the merchant can ship the product or provide the service to the customer.
To confirm a capture transaction, use the
confirmCapture
method to finalize the payment and transfer the funds to the merchant's account.If the confirmation is successful, the payment is considered complete, and the merchant can proceed with fulfilling the order.
To cancel a capture transaction, use the
cancelCapture
method to reverse the payment and release the reserved funds back to the customer's account.If the cancellation is successful, the payment is considered cancelled, and the reserved funds are released back to the customer's account.
Depending on the timing and processing schedules of the involved banks, it may take a few business days for the cancelled funds to become available again in the customer's account.
โ๏ธ Confirm capture
const { data, error } = await migo.confirmCapture('transactionId');
โ Cancel capture
const { data, error } = await migo.cancelCapture('transactionId');
๐ข Transaction status
const { data, error } = await migo.transactionStatus('transactionId');
// console.log(data?.transaction);