179 lines
7.4 KiB
JavaScript
179 lines
7.4 KiB
JavaScript
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
};
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
};
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
var PayMobProvider_1;
|
|
var _a;
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.PayMobProvider = void 0;
|
|
const common_1 = require("@nestjs/common");
|
|
const config_1 = require("@nestjs/config");
|
|
const axios_1 = __importDefault(require("axios"));
|
|
const crypto = __importStar(require("crypto"));
|
|
let PayMobProvider = PayMobProvider_1 = class PayMobProvider {
|
|
configService;
|
|
logger = new common_1.Logger(PayMobProvider_1.name);
|
|
baseUrl = 'https://accept.paymob.com/api';
|
|
constructor(configService) {
|
|
this.configService = configService;
|
|
}
|
|
async createPaymentKey(tenantId, amount, plan) {
|
|
try {
|
|
const authToken = await this.getAuthToken();
|
|
const orderRes = await axios_1.default.post(`${this.baseUrl}/ecommerce/orders`, {
|
|
auth_token: authToken,
|
|
delivery_needed: "false",
|
|
amount_cents: amount * 50 * 100,
|
|
currency: "EGP",
|
|
items: [{
|
|
name: `${plan} Subscription`,
|
|
amount_cents: amount * 50 * 100,
|
|
description: `Intaleq Maps ${plan} Plan`
|
|
}],
|
|
shipping_data: {
|
|
first_name: "Intaleq",
|
|
last_name: "User",
|
|
email: "user@intaleq.com",
|
|
phone_number: "+201000000000",
|
|
extra_description: `${tenantId}|${plan}`
|
|
}
|
|
});
|
|
const orderId = orderRes.data.id;
|
|
const keyRes = await axios_1.default.post(`${this.baseUrl}/acceptance/payment_keys`, {
|
|
auth_token: authToken,
|
|
amount_cents: amount * 50 * 100,
|
|
expiration: 3600,
|
|
order_id: orderId,
|
|
billing_data: {
|
|
apartment: "NA",
|
|
email: "user@intaleq.com",
|
|
floor: "NA",
|
|
first_name: "Intaleq",
|
|
street: "NA",
|
|
building: "NA",
|
|
phone_number: "+201000000000",
|
|
shipping_method: "NA",
|
|
postal_code: "NA",
|
|
city: "NA",
|
|
country: "NA",
|
|
last_name: "User",
|
|
state: "NA"
|
|
},
|
|
currency: "EGP",
|
|
integration_id: this.configService.get('PAYMOB_INTEGRATION_ID') || 4556055,
|
|
lock_order_when_paid: "false"
|
|
});
|
|
return {
|
|
paymentKey: keyRes.data.token,
|
|
orderId: orderId.toString()
|
|
};
|
|
}
|
|
catch (error) {
|
|
this.logger.error(`PayMob Payment Key failed: ${error.response?.data ? JSON.stringify(error.response.data) : error.message}`);
|
|
throw new common_1.BadRequestException('Failed to initialize PayMob payment');
|
|
}
|
|
}
|
|
async getTransactionDetails(transactionId) {
|
|
try {
|
|
const authToken = await this.getAuthToken();
|
|
const res = await axios_1.default.get(`${this.baseUrl}/acceptance/transactions/${transactionId}`, {
|
|
headers: { Authorization: `Bearer ${authToken}` }
|
|
});
|
|
return res.data;
|
|
}
|
|
catch (error) {
|
|
this.logger.error(`Failed to fetch PayMob transaction ${transactionId}: ${error.message}`);
|
|
return null;
|
|
}
|
|
}
|
|
async getAuthToken() {
|
|
const authRes = await axios_1.default.post(`${this.baseUrl}/auth/tokens`, {
|
|
api_key: this.configService.get('PAYMOB_API_KEY'),
|
|
});
|
|
return authRes.data.token;
|
|
}
|
|
verifyHmac(payload, hmac) {
|
|
const secret = this.configService.get('PAYMOB_HMAC_SECRET');
|
|
if (!secret)
|
|
return false;
|
|
const { amount_cents, created_at, currency, error_occured, has_parent_transaction, id, integration_id, is_3d_secure, is_auth, is_capture, is_refunded, is_standalone_payment, is_voided, order, owner, pending, success } = payload;
|
|
const source_pan = payload.source_data?.pan || "";
|
|
const source_sub_type = payload.source_data?.sub_type || "";
|
|
const source_type = payload.source_data?.type || "";
|
|
const data = [
|
|
amount_cents,
|
|
created_at,
|
|
currency,
|
|
error_occured,
|
|
has_parent_transaction,
|
|
id,
|
|
integration_id,
|
|
is_3d_secure,
|
|
is_auth,
|
|
is_capture,
|
|
is_refunded,
|
|
is_standalone_payment,
|
|
is_voided,
|
|
order.id || order,
|
|
owner,
|
|
pending,
|
|
source_pan,
|
|
source_sub_type,
|
|
source_type,
|
|
success
|
|
].join("");
|
|
const hash = crypto
|
|
.createHmac('sha512', secret)
|
|
.update(data)
|
|
.digest('hex');
|
|
return hash === hmac;
|
|
}
|
|
};
|
|
exports.PayMobProvider = PayMobProvider;
|
|
exports.PayMobProvider = PayMobProvider = PayMobProvider_1 = __decorate([
|
|
(0, common_1.Injectable)(),
|
|
__metadata("design:paramtypes", [typeof (_a = typeof config_1.ConfigService !== "undefined" && config_1.ConfigService) === "function" ? _a : Object])
|
|
], PayMobProvider);
|
|
//# sourceMappingURL=paymob.provider.js.map
|