"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 BinanceProvider_1; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.BinanceProvider = 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 BinanceProvider = BinanceProvider_1 = class BinanceProvider { configService; logger = new common_1.Logger(BinanceProvider_1.name); apiUrl = 'https://bpay.binanceapi.com/binancepay/openapi/v2/order'; constructor(configService) { this.configService = configService; } get apiKey() { return this.configService.get('BINANCE_PAY_API_KEY') || ''; } get secretKey() { return this.configService.get('BINANCE_PAY_SECRET_KEY') || ''; } generateSignature(timestamp, nonce, body) { const payload = timestamp + '\n' + nonce + '\n' + JSON.stringify(body) + '\n'; return crypto .createHmac('sha512', this.secretKey) .update(payload) .digest('hex') .toUpperCase(); } async createOrder(tenantId, amount, plan) { this.logger.log(`Creating Binance Pay order for ${tenantId} - ${plan}`); if (!this.apiKey || !this.secretKey) { this.logger.error('Binance Pay API keys are not configured'); throw new common_1.InternalServerErrorException('Payment provider is not configured properly'); } const nonce = crypto.randomBytes(16).toString('hex'); const timestamp = Date.now().toString(); const merchantTradeNo = `txn_${Date.now()}_${Math.floor(Math.random() * 10000)}`; const body = { env: { terminalType: 'WEB', }, merchantTradeNo: merchantTradeNo, orderAmount: amount, currency: 'USDT', goods: { goodsType: '01', goodsCategory: 'Z000', referenceGoodsId: plan, goodsName: `${plan} Plan Subscription`, goodsDetail: `Subscription to ${plan} plan for Maps SaaS`, }, passThroughInfo: JSON.stringify({ tenantId, plan }), returnUrl: `https://map-dashboard.intaleqapp.com/dashboard.html#billing`, }; const signature = this.generateSignature(timestamp, nonce, body); try { const response = await axios_1.default.post(this.apiUrl, body, { headers: { 'Content-Type': 'application/json', 'BinancePay-Timestamp': timestamp, 'BinancePay-Nonce': nonce, 'BinancePay-Certificate-SN': this.apiKey, 'BinancePay-Signature': signature, }, }); if (response.data && response.data.status === 'SUCCESS') { return { checkoutUrl: response.data.data.checkoutUrl, orderId: merchantTradeNo, prepayId: response.data.data.prepayId, }; } else { this.logger.error(`Binance API Error: ${JSON.stringify(response.data)}`); throw new common_1.InternalServerErrorException('Failed to create Binance order'); } } catch (error) { this.logger.error(`Error communicating with Binance API: ${error.message}`); throw new common_1.InternalServerErrorException('Failed to communicate with payment provider'); } } verifySignature(timestamp, nonce, signature, payloadBody) { const payload = timestamp + '\n' + nonce + '\n' + JSON.stringify(payloadBody) + '\n'; const expectedSignature = crypto .createHmac('sha512', this.secretKey) .update(payload) .digest('hex') .toUpperCase(); try { return crypto.timingSafeEqual(Buffer.from(signature || ''), Buffer.from(expectedSignature)); } catch (e) { return false; } } }; exports.BinanceProvider = BinanceProvider; exports.BinanceProvider = BinanceProvider = BinanceProvider_1 = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [typeof (_a = typeof config_1.ConfigService !== "undefined" && config_1.ConfigService) === "function" ? _a : Object]) ], BinanceProvider); //# sourceMappingURL=binance.provider.js.map