94 lines
3.2 KiB
TypeScript
94 lines
3.2 KiB
TypeScript
import axios from "axios";
|
|
import { injectable } from "inversify";
|
|
|
|
import { InternalError } from "../../../core/app/app.errors";
|
|
import { Logger } from "../../../core/logging/logger";
|
|
interface SepRequestPaymentResponse {
|
|
status: 1 | -1;
|
|
errorCode?: string;
|
|
errorDesc?: string;
|
|
token?: string;
|
|
}
|
|
|
|
@injectable()
|
|
class SepGateway {
|
|
private logger = new Logger(SepGateway.name);
|
|
|
|
private gatewayApiUrl: string = process.env.SEP_HOST || "https://sep.shaparak.ir";
|
|
private callBackURL = `https://www.fajrtabloshop.com/verify/saman/`;
|
|
private TERMINAL_ID: string = process.env.SEP_TERMINAL_ID || "15364098";
|
|
private requestHeader = { "Content-Type": "application/json", Accept: "application/json" };
|
|
|
|
async processPayment(amount: number, description: string, email: string, mobile: string, callBackPath: string) {
|
|
if (!this.gatewayApiUrl || !this.TERMINAL_ID) {
|
|
throw new InternalError(["SEP gateway is not configured"]);
|
|
}
|
|
await this.checkOutgoingIp();
|
|
const requesturl = `${this.gatewayApiUrl}/onlinepg/onlinepg`;
|
|
|
|
console.log("SEP Gateway processPayment called", email, description);
|
|
try {
|
|
const purchaseData = {
|
|
Action: "Token",
|
|
TerminalId: this.TERMINAL_ID,
|
|
Amount: amount, // ریال
|
|
RedirectUrl: `${this.callBackURL}${callBackPath}`,
|
|
ResNum: Date.now().toString(),
|
|
CellNumber: mobile,
|
|
};
|
|
console.log("purchase data", purchaseData);
|
|
const response = await axios.post<SepRequestPaymentResponse>(requesturl, purchaseData, { headers: this.requestHeader });
|
|
const data = response.data;
|
|
|
|
console.log("response data", data);
|
|
if (data.status == -1) {
|
|
throw new InternalError(data.errorCode + " " + data.errorDesc);
|
|
}
|
|
|
|
return {
|
|
redirectUrl: this.callBackURL,
|
|
message: "successfull",
|
|
authority: data.token,
|
|
};
|
|
} catch (error) {
|
|
this.logger.error("error in SEP payment process", error);
|
|
throw new InternalError(["error processing SEP payment"]);
|
|
}
|
|
}
|
|
|
|
async checkOutgoingIp() {
|
|
try {
|
|
const response = await axios.get("https://api.ipify.org?format=json");
|
|
console.log("Your Server's Public Outgoing IP is:", response.data.ip);
|
|
return response.data.ip;
|
|
} catch (error) {
|
|
console.error("Could not determine outgoing IP", error);
|
|
}
|
|
}
|
|
|
|
async verifyPayment(authority: string, amount: number) {
|
|
if (!this.gatewayApiUrl || !this.TERMINAL_ID) {
|
|
throw new InternalError(["SEP gateway is not configured"]);
|
|
}
|
|
// https://sep.shaparak.ir/verifyTxnRandomSessionkey/ipg/VerifyTransaction
|
|
|
|
try {
|
|
const verifyData = { authority, amount, terminal_id: this.TERMINAL_ID };
|
|
const response = await axios.post(`${this.gatewayApiUrl}/payment/verify`, verifyData, { headers: this.requestHeader });
|
|
return response.data;
|
|
} catch (error) {
|
|
this.logger.error("error in SEP payment verification", error);
|
|
throw new InternalError(["error in SEP payment verification"]);
|
|
}
|
|
}
|
|
|
|
async retryPayment(authority: string) {
|
|
return {
|
|
redirectUrl: `${this.gatewayApiUrl}/pay/${authority}`,
|
|
authority: authority,
|
|
};
|
|
}
|
|
}
|
|
|
|
export { SepGateway };
|