A NodeJS package, written in TypeScript, to provide access to Zarinpal's payment gateway.
Open a command prompt in the directory where
package.json resides, and run this:
npm install zarinpal-ts --save
This installs the latest version of the package in your project's local node modules.
Sample usages of the package are located under the
The package is a set of loosely-coupled components that enable developers to replace their own implementations, if they ever had to. More than that, the design of the package is general to most of the current web based payment providers, so the developers do not need to replace the whole package (and hence, their code) if they've switched to a new payment service; adding a few implementations, if not already been made by others, and a little modification of the application's composition root will do the job.
The design is based on these abstract entities:
PaymentManager is the outmost interface that provides the clients (here,
the developers that use the package) with appropriate
which encapsulate the payment process. That is, every payment, from the
beginning to the end, takes place in a context, called
PaymentSession. The job
PaymentManager, is to create
PaymentSession objects for new payments,
track all active sessions, and given the HTTP request sent back from the payment
server to the application (callback), return the corresponding payment session
PaymentSession, enables client to set the details of the payment (held
Payment class objects) and register it on the payment server. This
is done via the
register method, which takes a callback URL that determines
where to navigate back the user after the payment has been done/aborted.
Since, the three steps: registration, payment, and verification are common among payment services, it's possible to use the abstraction above for payment services other than the Zarinpal's.
- To start a payment process, first off, you need to create a new payment session
PaymentManager has to be a singleton. So, here,
this.sessionContext() is assumed to return from the user session data on the
- Setting up payment information:
paymentSession.payment.amount = 1000; // (In Rials.)paymentSession.payment.description = "Showcase payment.";paymentSession.payment.email = "email@example.com";
- Registering on the payment server:
- After the user has done/abort the payment, the payment server redirects to
the given callback URL at which payment verification should take place. Before proceeding to verification, it's necessary to recover the corresponding
request is assumed to be the original request object of
- Verifying the payment:
At startup, application should compose payment module from components. For example, for a sandboxed payment module:
;;;/*** The line below initializes a sandboxed payment gateway. To use the actual* gateway replace "SandboxZarinpalPaymentSessionFactory" with* "DefaultZarinpalPaymentSessionFactory".*/;;
paymentManager should be stored to be available for every request as a
For complete sample code, look at the