UNPKG

zarinpal-checkout-ts

Version:

Easy to implement solution for Zarinpal API

97 lines (93 loc) 2.45 kB
import got from 'got'; const config = { https: "https://www.zarinpal.com/pg/rest/WebGate/", sandbox: "https://sandbox.zarinpal.com/pg/rest/WebGate/", merchantIDLength: 36, API: { PR: "PaymentRequest.json", PRX: "PaymentRequestWithExtra.json", PV: "PaymentVerification.json", PVX: "PaymentVerificationWithExtra.json", RA: "RefreshAuthority.json", UT: "UnverifiedTransactions.json" }, PG: function(sandbox) { if (sandbox) { return "https://sandbox.zarinpal.com/pg/StartPay/"; } return "https://www.zarinpal.com/pg/StartPay/"; } }; class ZarinPal { constructor(MerchantID, sandbox = false, options) { this.MerchantID = MerchantID; this.sandbox = sandbox; if (MerchantID.length !== config.merchantIDLength) { throw new Error( `The MerchantID must be ${config.merchantIDLength} Characters.` ); } this.MerchantID = MerchantID; this.sandbox = sandbox || false; this.client = got.extend({ ...options, prefixUrl: sandbox ? config.sandbox : config.https, headers: { ...options?.headers, "Cache-Control": "no-cache" } }); } async PaymentRequest(input) { const response = await this.client.post(config.API.PR, { json: { MerchantID: this.MerchantID, ...input } }).json(); return { status: response.Status, authority: response.Authority, url: config.PG(this.sandbox) + response.Authority }; } async PaymentVerification(input) { const response = await this.client.post(config.API.PV, { json: { MerchantID: this.MerchantID, ...input } }).json(); return { status: response.Status, RefID: response.RefID }; } async UnverifiedTransactions() { const response = await this.client.post(config.API.UT, { json: { MerchantID: this.MerchantID } }).json(); return { status: response.Status, authorities: response.Authorities }; } async RefreshAuthority(input) { const response = await this.client.post(config.API.RA, { json: { MerchantID: this.MerchantID, Authority: input.Authority, ExpireIn: input.Expire } }).json(); return { status: response.Status }; } } function create(MerchantID, sandbox, options) { return new ZarinPal(MerchantID, sandbox, options); } export { create };