# zarinpal-nestjs

> Zarinpal NestJs Adaptor for an easy use

Latest version **1.1.1** (published 2024-08-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install zarinpal-nestjs
pnpm add zarinpal-nestjs
yarn add zarinpal-nestjs
bun add zarinpal-nestjs
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2024-08-01 |
| First published | 2022-11-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 61.7 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 14 |
| Author | Mehdi Rahimi |
| Maintainers | medira99 |
| Keywords | nestjs, zarinpal, node, adaptor |

## Links

- npm: https://www.npmjs.com/package/zarinpal-nestjs
- Repository: https://github.com/me-dira/zarinpal-nestjs
- Homepage: https://github.com/me-dira/zarinpal-nestjs/wiki
- Issues: https://github.com/me-dira/zarinpal-nestjs
- npm.io page: https://npm.io/package/zarinpal-nestjs

## Dependencies (7)

- [rxjs](https://npm.io/package/rxjs.md) ^7.5.7
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [node-fetch](https://npm.io/package/node-fetch.md) ^2.6.1
- [class-validator](https://npm.io/package/class-validator.md) ^0.13.2
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13
- [class-transformer](https://npm.io/package/class-transformer.md) ^0.5.1
- [@nestjs/class-validator](https://npm.io/package/@nestjs/class-validator.md) ^0.13.4

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2024-08-01
- 1.1.0 — 2024-03-02
- 1.0.6 — 2024-03-02
- 1.0.5 — 2024-03-02
- 1.0.4 — 2022-12-19
- 1.0.3 — 2022-12-05
- 1.0.2 — 2022-12-04
- 1.0.1 — 2022-12-04
- 1.0.0 — 2022-11-08
- 0.1.6 — 2022-11-07
- 0.1.5 — 2022-11-07
- 0.1.4 — 2022-11-07
- 0.1.3 — 2022-11-07
- 0.1.2 — 2022-11-07
- 0.1.1 — 2022-11-07
- … 41 more at https://npm.io/package/zarinpal-nestjs/versions

## README

# Zarinpal Adaptor for `NestJs`.

<p align="center">
  <a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="200" alt="Nest Logo" /></a>
</p>

[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456

# Features:

[circleci-url]: https://circleci.com/gh/nestjs/nest

[![Documents](https://badgen.net/badge/Documents/v1/blue?icon=wiki)](https://github.com/me-dira/zarinpal-nestjs/wiki)
[![Version](https://badgen.net/badge/Version/v1.0.0/orange?icon=github)](https://github.com/me-dira/zarinpal-nestjs)
[![status](https://badgen.net/badge/Status/Released/green?icon=now)](https://github.com/me-dira/zarinpal-nestjs)

- Easy to use.
- Light and fast.
- Specific error type (ZarinpalError).

## How to use:

The process is very simple and includes:

- Open transaction on Zarinpal.
- Generate the URL using the result and redirect the user to the Start Pay page.
- The user will be redirected after payment is done to verify the URL you specified.

---

### installation

Installation is very simple and can be done using below commands:

```bash
yarn add zarinpal-nestjs
npm install --save zarinpal-nestjs
```

### Register the module

After successful installation, you need to register `zarinpal-nestjs` module in your NestJs application:

```typescript
@Module({
  imports: [
    ZarinpalModule.register({
      // Required options:
      callBackUrl: 'https://google.com',
      merchantId: '32 character long merchant_id',
    }),
  ],
})
export class AppModule {}
```

### Inject inside the class

You can check one simple example of injecting the `service`:

```typescript
import { ZarinpalService } from 'zarinpal-nestjs';
@Controller('transaction')
export class AppController {
  constructor(
    private readonly zarinpalService: ZarinpalService,
  ) {}

```

## Open transaction & Generate Start Pay URL

As you know you need to redirect the user to the payment gate, then the user can pay the bill. After you open a transaction and get the authorization code, you can generate the URL using the `transactionOpen` result like:

```typescript
  async openTransaction(): Promise<string> {
      const transactionResult = await this.zarinpalService.openTransaction({
        amount: 1000,
        description: 'Buying a car (example)',
      });

      // https://www.zarinpal.com/pg/StartPay/A00000000000000000000000000387664294
      return this.zarinpalService.generateStartPayUrl(transactionResult);
  }
```

## Verify transaction

After the user completes the payment process, will return to the URL you pass through Module registration and you can verify them like this:

```typescript
@Get('verify')
async verifyTransaction(@Query() query: ZarinpalVerifyQueryParams) {
    const transaction = await this.appService.findTransactionByAuthority(
      query.Authority,
    );

    await this.zarinpalService.verifyTransaction({
      authority: query.Authority,
      amount: transaction.amount,
    });
}
```

## Encode & Decode Authority code

**Be Careful with using this method. experimental flag**

As you know authority codes of transactions are pretty long and it's very bad practice to store them raw. You can encode and decode them using two predefined functions. For example:

```typescript
import {
  zarinpalAuthorityEncode,
  zarinpalAuthorityDecode,
} from 'zarinpal-nestjs';

const longAuthorityCode = 'A00000000000000000000000000387664294';
const encodedAuthorityCode = zarinpalAuthorityEncode(longAuthorityCode);
const decodedEncodedAuthorityCode = zarinpalAuthorityDecode(
  encodedAuthorityCode,
);

console.log({
  encoded: encodedAuthorityCode, // 26x387664294
  decoded: decodedEncodedAuthorityCode, // A00000000000000000000000000387664294
});
```

for more information please read [Documentation](https://github.com/me-dira/zarinpal-nestjs/wiki).

---
_Source: https://npm.io/package/zarinpal-nestjs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
