zarinpal-typescript
Advanced tools
Comparing version 2.1.4 to 3.0.0
@@ -1,53 +0,3 @@ | ||
/// <reference types="node" /> | ||
import { Agent } from 'http'; | ||
export interface IZarinpalOption { | ||
sandbox?: boolean; | ||
timeout?: number; | ||
agent?: Agent | ((parsedUrl: URL) => Agent); | ||
} | ||
export interface IZarinpalEndpoints { | ||
requestPayment: string; | ||
startPayment: string; | ||
verifyPayment: string; | ||
} | ||
export declare type TAllowedMethods = 'POST'; | ||
export interface IZarinpalRequestPaymentResponse { | ||
Status: number; | ||
Authority: string; | ||
errorMessage?: string; | ||
} | ||
export interface IZarinpalVerifyPaymentResponse { | ||
Status: number; | ||
RefID: number; | ||
errorMessage?: string; | ||
} | ||
export interface IZarinpalGeneralResponse { | ||
Status: number; | ||
[key: string]: any; | ||
} | ||
export interface IZarinpalRequestPayment { | ||
amount: number; | ||
description: string; | ||
callbackUrl: string; | ||
email?: string; | ||
mobile?: string; | ||
} | ||
export interface IZarinpalVerifyPayment { | ||
authority: string; | ||
amount: number; | ||
} | ||
export declare class Zarinpal { | ||
readonly merchantId: string; | ||
private readonly endpoints; | ||
private readonly sandbox; | ||
private readonly basicHeaders; | ||
private readonly timeout; | ||
private readonly agent?; | ||
constructor(merchantId: string, option?: IZarinpalOption); | ||
private setEndpoints; | ||
private getErrorMessage; | ||
private requestZarinpal; | ||
requestPayment: (data: IZarinpalRequestPayment) => Promise<IZarinpalRequestPaymentResponse>; | ||
startPayment: (authority: string) => string; | ||
verifyPayment: (data: IZarinpalVerifyPayment) => Promise<IZarinpalVerifyPaymentResponse>; | ||
} | ||
export * from "./Zarinpal"; | ||
export * from "./ZarinpalErrorCodes"; | ||
export * from "./errors"; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Zarinpal = void 0; | ||
var node_fetch_1 = __importDefault(require("node-fetch")); | ||
var Zarinpal = /** @class */ (function () { | ||
function Zarinpal(merchantId, option) { | ||
var _this = this; | ||
if (option === void 0) { option = {}; } | ||
this.basicHeaders = { | ||
'Cache-Control': 'no-cache', | ||
'Content-Type': 'application/json', | ||
}; | ||
this.setEndpoints = function () { | ||
var baseUrl = 'https://www.zarinpal.com/'; | ||
if (_this.sandbox) { | ||
baseUrl = 'https://sandbox.zarinpal.com/'; | ||
} | ||
return { | ||
requestPayment: baseUrl + 'pg/rest/WebGate/PaymentRequest.json', | ||
startPayment: baseUrl + 'pg/StartPay/', | ||
verifyPayment: baseUrl + 'pg/rest/WebGate/PaymentVerification.json', | ||
}; | ||
}; | ||
this.requestZarinpal = function (url, method, body) { return __awaiter(_this, void 0, void 0, function () { | ||
var res, resBody, errorMessage; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, node_fetch_1.default(url, { | ||
method: method, | ||
headers: this.basicHeaders, | ||
body: JSON.stringify(body), | ||
timeout: this.timeout, | ||
agent: this.agent, | ||
})]; | ||
case 1: | ||
res = _a.sent(); | ||
return [4 /*yield*/, res.json()]; | ||
case 2: | ||
resBody = _a.sent(); | ||
if (!res.ok) { | ||
throw new Error('Zarinpal response was not ok.'); | ||
} | ||
if (resBody.Status < 0) { | ||
errorMessage = this.getErrorMessage(resBody.Status); | ||
resBody.errorMessage = errorMessage; | ||
} | ||
return [2 /*return*/, resBody]; | ||
} | ||
}); | ||
}); }; | ||
this.requestPayment = function (data) { return __awaiter(_this, void 0, void 0, function () { | ||
var response; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.requestZarinpal(this.endpoints.requestPayment, 'POST', { | ||
MerchantID: this.merchantId, | ||
Amount: data.amount, | ||
Description: data.description, | ||
CallbackURL: data.callbackUrl, | ||
Email: data.email, | ||
Mobile: data.mobile, | ||
})]; | ||
case 1: | ||
response = (_a.sent()); | ||
return [2 /*return*/, response]; | ||
} | ||
}); | ||
}); }; | ||
this.startPayment = function (authority) { | ||
return _this.endpoints.startPayment + authority; | ||
}; | ||
this.verifyPayment = function (data) { return __awaiter(_this, void 0, void 0, function () { | ||
var response; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.requestZarinpal(this.endpoints.verifyPayment, 'POST', { | ||
MerchantID: this.merchantId, | ||
Authority: data.authority, | ||
Amount: data.amount, | ||
})]; | ||
case 1: | ||
response = _a.sent(); | ||
return [2 /*return*/, response]; | ||
} | ||
}); | ||
}); }; | ||
if (merchantId.length !== 36) { | ||
throw new Error('Zarinpal merchantId must be 36 characters.'); | ||
} | ||
this.merchantId = merchantId; | ||
this.sandbox = option.sandbox || false; | ||
this.timeout = option.timeout || 20000; | ||
this.agent = option.agent; | ||
this.endpoints = this.setEndpoints(); | ||
} | ||
Zarinpal.prototype.getErrorMessage = function (code) { | ||
var message = 'Zarinpal is down.'; | ||
switch (code) { | ||
case -1: | ||
message = 'Insufficient information'; | ||
break; | ||
case -2: | ||
message = 'IP or Merchant Code is not correct'; | ||
break; | ||
case -3: | ||
message = 'Amount should be greater than 1000'; | ||
break; | ||
case -4: | ||
message = 'Insufficient'; | ||
break; | ||
case -11: | ||
message = "Requested response didn't find"; | ||
break; | ||
case -21: | ||
message = 'No financial action found for this transaction'; | ||
break; | ||
case -22: | ||
message = 'Unsuccessful transaction'; | ||
break; | ||
case -33: | ||
message = 'Transaction price is not equal to payed amount'; | ||
break; | ||
case -54: | ||
message = 'The request had archived'; | ||
break; | ||
} | ||
return message; | ||
}; | ||
return Zarinpal; | ||
}()); | ||
exports.Zarinpal = Zarinpal; | ||
__exportStar(require("./Zarinpal"), exports); | ||
__exportStar(require("./ZarinpalErrorCodes"), exports); | ||
__exportStar(require("./errors"), exports); |
{ | ||
"name": "zarinpal-typescript", | ||
"version": "2.1.4", | ||
"version": "3.0.0", | ||
"main": "dist/index.js", | ||
@@ -23,9 +23,7 @@ "types": "dist/index.d.ts", | ||
"devDependencies": { | ||
"@types/node-fetch": "^2.5.7", | ||
"typescript": "^4.0.2" | ||
}, | ||
"dependencies": { | ||
"abort-controller": "^3.0.0", | ||
"node-fetch": "^2.6.0" | ||
"axios": "^0.21.1" | ||
} | ||
} |
@@ -16,10 +16,10 @@ Simple Zarinpal package written in TypeScript. | ||
```typescript | ||
import { Zarinpal } from 'zarinpal-typescript'; | ||
import { Zarinpal } from "zarinpal-typescript"; | ||
const zp = new Zarinpal('merchant-code', { | ||
/** | ||
* Sandbox is for development only | ||
* And should be turned off in production. | ||
*/ | ||
sandbox: true | ||
const zp = new Zarinpal("merchant-code", { | ||
/** | ||
* Sandbox is for development only | ||
* And should be turned off in production. | ||
*/ | ||
sandbox: true, | ||
}); | ||
@@ -32,15 +32,9 @@ ``` | ||
// Create invoice | ||
const invoiceResponse = await zp.requestPayment({ | ||
amount: 1000, // Toman | ||
callbackUrl: 'https://example.com/my-call-back-url?custom-param=value', | ||
description: 'Invoice description', // This is required! | ||
const createdPayment = await zp.requestPayment({ | ||
amount: 1000, // Toman | ||
callbackUrl: "https://example.com/my-call-back-url?custom-param=value", | ||
description: "Invoice description", // This is required! | ||
}); | ||
// Check for any errors | ||
if (invoiceResponse.errorMessage) throw new Error(invoiceResponse.errorMessage); | ||
// Get payment url | ||
const paymentUrl = zp.startPayment(invoiceResponse.Authority); | ||
// Redirect user to the paymentUrl | ||
// Redirect user to the createdPayment.url | ||
``` | ||
@@ -52,17 +46,5 @@ | ||
const amount = 1000; | ||
const authority = 'invoice-authority'; | ||
const authority = "previously-created-authority"; | ||
const verificationResponse = await zp.verifyPayment({ amount: amount, authority: authority }); | ||
// Check for errors | ||
if (verificationResponse.errorMessage) throw new Error(verificationResponse.errorMessage); | ||
switch(verificationResponse.Status) { | ||
case 100: | ||
// Invoice paid | ||
break; | ||
case 101: | ||
// Invoice is already paid. | ||
break; | ||
} | ||
const verifiedPayment = await zp.verifyPayment({ amount, authority }); | ||
``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
22953
1
1
23
608
48
1
+ Addedaxios@^0.21.1
+ Addedaxios@0.21.4(transitive)
+ Addedfollow-redirects@1.15.9(transitive)
- Removedabort-controller@^3.0.0
- Removednode-fetch@^2.6.0
- Removedabort-controller@3.0.0(transitive)
- Removedevent-target-shim@5.0.1(transitive)
- Removednode-fetch@2.7.0(transitive)
- Removedtr46@0.0.3(transitive)
- Removedwebidl-conversions@3.0.1(transitive)
- Removedwhatwg-url@5.0.0(transitive)