wk-nmi
v0.0.21
Published
This is a simple utility package
Downloads
11
Maintainers
Readme
NMI Gateway
This is the wklive implementation of nmi gateway, the original documentation of nmi is: https://secure.networkmerchants.com/gw/merchants/resources/integration/integration_portal.php#cv_variables
This package was build with the unique intention to make the integration with the gateway easy.
You can generate tokens using: https://nmi-dashboard.vercel.app/nmi?/generateToken
Getting started
First you need to configure GOOGLE_APPLICATION_CREDENTIALS env variable to point to your google key credentials.
GOOGLE_APPLICATION_CREDENTIALS = "path/to/you/service/account/credentials.json"
Also you need
WK_NMI_SERVER = "nmi server url"
NMI_ORGANIZATION = " nmi organization"
NextJS configuration example
You can get the project here: https://github.com/gtavo95/wk-nmi-front-example.git
// page.tsx
// import google authorization libraries
import { GoogleAuth } from "google-auth-library";
// import wk-nmi library to interact with nmi server
import { Plans } from "wk-nmi";
// server fetch function
async function getPlans() {
// Init google auth libraries
const auth = new GoogleAuth();
// Workinglive nmi server url
const url = process.env.WK_NMI_SERVER;
// Nmi organization
const organization = process.env.NMI_ORGANIZATION;
if (url && organization) {
console.info(
`request ${url} with target audience ${url}`
);
// Get an auth client
const client = await auth.getIdTokenClient(url);
// Get the tokenID token
const tokenId = await client.idTokenProvider.fetchIdToken(url);
// Now use the wk-nmi library to get all plans as below
const plan = new Plans(url, "testOrg4", tokenId);
const plans = await plan.all();
if (plans.status === 200) return plans.response;
else return [];
}
}
export default async function Page() {
// Get plans
const plans = await getPlans();
// Render plans as you wish
return (
<div className="flex flex-col h-screen w-screen bg-white overflow-auto gap-3">
{/* titulo */}
<h1 className="text-black text-2xl">Plans</h1>
{/* plans */}
<div className="flex flex-row flex-wrap gap-1">
{plans &&
plans.map((plan: any) => (
<div
key={plan.plan_id}
className="flex flex-col rounded-lg border border-gray-400 text-black p-3"
>
<h1>Name: {plan.plan_name}</h1>
<p>payments: {plan.plan_payments}</p>
<p>Amounts: {plan.plan_amount}</p>
</div>
))}
</div>
</div>
);
}
Doc
The library supports the next operations:
Billing
Add
The add method creates a new billing to the selected user_id
// Import library
import { Billing } from "wk-nmi";
// Init Billing
const billing = new Billing(
"https://nmi-server-orkrbzqvda-uc.a.run.app",
"testOrg4",
);
// use add method
const res = await billing.add({
userId: "1",
billingId: "16",
token: "00000000-000000-000000-000000000000", // collect-js-token
billingInfo: {
// all fields are optional
firstName: "test",
lastName: "test",
address1: "test",
city: "test",
state: "test",
zip: "test",
country: "test",
phone: "test",
email: "test",
company: "test",
address2: "test",
fax: "test",
shippingId: "test",
shippingAddress1: "test",
shippingAddress2: "test",
shippingCity: "test",
shippingCountry: "test",
shippingZip: "test",
shippingState: "test",
shippingFirst_name: "test",
shippingLast_name: "test",
shippingPhone: "test",
shippingEmail: "test",
},
});
updateBillingInfo
The updateBillingInfo method only updates only billing information, it can't edit billing_id, user_id.
const res = await billing.updateBillingInfo({
userId: "1",
billingId: "16",
token: "00000000-000000-000000-000000000000", // collect-js-token
billingInfo: {
// all fields are optional
firstName: "test",
lastName: "test",
address1: "test",
city: "test",
state: "test",
zip: "test",
country: "test",
phone: "test",
email: "test",
company: "test",
address2: "test",
fax: "test",
shippingId: "test",
shippingAddress1: "test",
shippingAddress2: "test",
shippingCity: "test",
shippingCountry: "test",
shippingZip: "test",
shippingState: "test",
shippingFirst_name: "test",
shippingLast_name: "test",
shippingPhone: "test",
shippingEmail: "test",
},
});
delete
Deletes selected billing_id
const userId = "";
const billingId = "";
const res = await billing.delete(userId, billingId);
priority
Changes the billing priority of a billing id.
const res = await billing.changePriority({
userId: "1",
billingId: "16",
priority: 1,
});
info
get billing information of userId
const userId = "";
const res = await billing.info(userId);
Customer Vault
Handle customer vault operations
add
Create a new customer vault with the provided id (this id is then used by other methods of this library as user_id)
// import library
import { CustomerVault } from "wk-nmi";
const host = "";
const org = "";
// init CustomerVault
customerVault = new CustomerVault(host, org);
// add user
const res = await customerVault.add({
id: "testId113233",
token: "00000000-000000-000000-000000000000",
billingId: "",
billingInfo: {
firstName: "",
lastName: "",
address1: "",
city: "",
state: "",
zip: "",
country: "",
phone: "",
email: "",
},
});
Plans
Handles plan operations with the nmi api
all
Get all plans created plans in the organization
// import
import { Plans } from "wk-nmi";
const host = "https://nmi-server-orkrbzqvda-uc.a.run.app";
const org = "testOrg4";
// init CustomerVault
plan = new Plans(host, org);
// get plans
const res = await plan.all();
id
Get plan id information
// Get swzshoppingonly plan information
const planId = "";
const res = await plan.id(planId);
addMonthConfiguration
Create a new plan using month setup
# create plan
const res = await plan.withMonthConfig({
customPlan: {
planAmount: "10.00",
planName: "test",
planId: "testtset",
monthFrequency: "1",
dayOfMonth: "1",
planPayments: "0"
}
})
edit_month_configuation
Edit plan using month configuration
// edit month configuration plan
const res = await plan.editMonthConfig({
customPlan: {
planAmount: "10.00",
planName: "test",
planId: "testtset",
monthFrequency: "1",
dayOfMonth: "1",
planPayments: "0",
},
});
addDayConfiguration
Create a new plan using day setup
// create a plan using day configuration
plan.withDayConfig({
customPlan: {
planAmount: "10.00",
planName: "test",
planId: "testtsetts",
dayFrequency: "1",
planPayments: "0",
},
});
editDayConfiguration
Edit a plan using day setup
// edit a plan using day configuration
plan.editDayConfig({
customPlan: {
planAmount: "10.00",
planName: "test",
planId: "testtsetts",
dayFrequency: "1",
planPayments: "0",
},
});
Subscription
Handles subscriptions using the nmi api
withMonthConfig
Assign a subscriptions to the user_id (customer vault). if total_amount is set to 0, the method will behave as a normal subscription and the user will be charged according to the custom_subscription info object. But if total_amount is set to a value greater that 0 the user will be charged by that amount
// import library
import { Subs } from "wk-nmi";
// init
const subsObj = new Subs(
"https://nmi-server-orkrbzqvda-uc.a.run.app",
"testOrg4",
);
// assign subscription to custumer vault
const res = await subsObj.withMonthConfig({
userId: "1",
orderId: "test",
totalAmount: "0", // 0 for subscription without an addition sale amount
customSubscriptionInfo: {
planId: "test",
planAmount: "10.00",
planName: "test",
monthFrequency: "1",
dayOfMonth: "1",
planPayments: "0",
},
});
// The output can be of type BadResponse | ErrorDetail | SubscriptionResult
if (
"successfull" in subResponse &&
subResponse.successfull &&
"nm_response" in subResponse
) {
let nmiRespones = subResponse.nm_response.
} else {
// error handling
}
withDayConfig
Assign a subscriptions to the user_id (customer vault). if total_amount is set to 0, the method will behave as a normal subscription and the user will be charged according to the custom_subscription info object. But if total_amount is set to a value greater that 0 the user will be charged by that amount
// Assign subscription to custumer vault
const res = await subsObj.withDayConfig({
userId: "1",
orderId: "testRef",
totalAmount: "0", //0 for subscription without an addition sale amount
customSubscriptionInfo: {
planPayments: "15",
planAmount: "6",
dayFrequency: "1",
},
});
// The output can be of type BadResponse | ErrorDetail | SubscriptionResult
if (
"successfull" in subResponse &&
subResponse.successfull &&
"nm_response" in subResponse
) {
let nmiRespones = subResponse.nm_response.
} else {
// error handling
}
Pause/Resume
Pause subscription
// pause subscription
const subscriptionId = "your subscription id";
const pause = true; // true to pause/false to resume
const res = await subsObj.pause(subscriptionId, pause);
Cancel
Cancel a subscripion
const subscriptionId = "your subscription id";
const result = await subsObj.cancel(subscriptionId);
by_user_id
Get all subscriptions of user_id
// Get subscriptions of user id
const userId = "your user id";
const result = await subsObj.all(userId);
update
Update payment method, using a new customer vault id,
// Update subscriptions
const subId = ""
const customerId = ""
const org = ""
const url = ""
const apikey = ""
const subs = new Subs(url, org, apikey);
const res = await subs.update_month_subscription(subId, customerId, {
planPayments: "1",
planAmount: "14",
monthFrequency: "1",
dayOfMonth: "1",
startDate: ""
});
Config
Handles creations of the merchants
get
Get merchant information
// import library
import { Config } from "wk-nmi
// init
const configObj = new Config("http://127.0.0.1:8000","testOrg4")
// get merchant configuration
const res = await configObj.get()
update
Update merchant information
// edit merchant information
const res = await configObj.update({
storeId: 2342311,
environmentActive: "sandbox",
productionEnv: {
token: "",
},
sandboxEnv: {
token: "",
},
secretToken: "",
merchantOrchestrator_url: "http://",
merchantId: "123",
merchantSignature: "123",
});
add
Create a new merchant
// add a new merchant
const res = await configObj.add({
storeId: 2342311,
environmentActive: "sandbox",
productionEnv: {
token: "",
},
sandboxEnv: {
token: "",
},
secretToken: "",
merchantOrchestrator_url: "http://",
merchantId: "123",
merchantSignature: "123",
});
delete
Delete a current merchant,
// warning
const res = await configObj.delete();
Payment
Handles payments
withCustomerVault
Handles customer vaults requests
import { Pay } from "../lib/payments";
const pay = new Pay("http://127.0.0.1:8000", "testOrg4", "secretToken");
const paymentResult = await pay.withCustomerVault({
total: "6",
customerVault: "2",
billingInfo: {
firstName: "testFirstName",
lastName: "testLastName",
address1: "testAddress1",
city: "testCity",
state: "testState",
zip: "testZip",
country: "testCountry",
phone: "testPhone",
email: "testEmail",
},
});
// The output can be of type BadResponse | ErrorDetail | NMIResponse
if (
"successfull" in subResponse &&
subResponse.successfull &&
"nm_response" in subResponse
) {
// successfull
let nmiRespones = subResponse.nm_response.
} else {
// error handling
}
withToken
Handles simple payment requests
const pay = new Pay("http://127.0.0.1:8000", "testOrg4", "secretToken");
const paymentResult = await pay.withToken({
total: "6",
customerVault: "2",
billingInfo: {
firstName: "testFirstName",
lastName: "testLastName",
address1: "testAddress1",
city: "testCity",
state: "testState",
zip: "testZip",
country: "testCountry",
phone: "testPhone",
email: "testEmail",
},
});
// The output can be of type BadResponse | ErrorDetail | NMIResponse
if (
"successfull" in subResponse &&
subResponse.successfull &&
"nm_response" in subResponse
) {
// successfull
let nmiRespones = subResponse.nm_response.
} else {
// error handling
}