UNPKG

@lomray/microservice-payment-stripe

Version:

Stripe payment microservice based on NodeJS & inverted json.

153 lines (149 loc) 5.67 kB
'use strict'; var tslib = require('tslib'); var microserviceNodejsLib = require('@lomray/microservice-nodejs-lib'); var StripeSdk = require('stripe'); var typeorm = require('typeorm'); var uuidv4 = require('uuidv4'); var bankAccount = require('../../entities/bank-account.js'); var card = require('../../entities/card.js'); var cart = require('../../entities/cart.js'); var coupon = require('../../entities/coupon.js'); var customer = require('../../entities/customer.js'); var dispute = require('../../entities/dispute.js'); var evidenceDetails = require('../../entities/evidence-details.js'); var payout = require('../../entities/payout.js'); var price = require('../../entities/price.js'); var product = require('../../entities/product.js'); var refund = require('../../entities/refund.js'); var transaction = require('../../entities/transaction.js'); var messages = require('../../helpers/validators/messages.js'); /** * Abstract class for payment gateway */ class Abstract { /** * @constructor */ constructor(manager, apiKey, stripeConfig, methods) { this.customerRepository = manager.getRepository(customer); this.productRepository = manager.getRepository(product); this.priceRepository = manager.getRepository(price); this.cartRepository = manager.getRepository(cart); this.transactionRepository = manager.getRepository(transaction); this.refundRepository = manager.getRepository(refund); this.cardRepository = manager.getRepository(card); this.bankAccountRepository = manager.getRepository(bankAccount); this.couponRepository = manager.getRepository(coupon); this.disputeRepository = manager.getRepository(dispute); this.evidenceDetailsRepository = manager.getRepository(evidenceDetails); this.payoutRepository = manager.getRepository(payout); this.methods = methods; this.sdk = new StripeSdk(apiKey, stripeConfig); this.manager = manager; } /** * Create new transaction */ createTransaction(params, transactionId = uuidv4.uuid()) { return tslib.__awaiter(this, void 0, void 0, function* () { const transaction = this.transactionRepository.create(Object.assign(Object.assign({}, params), { product: { productId: params.productId }, customer: { userId: params.userId }, transactionId })); yield this.transactionRepository.save(transaction); return transaction; }); } /** * Create new customer */ createCustomer(userId, customerId = uuidv4.uuid()) { return tslib.__awaiter(this, void 0, void 0, function* () { const customer = this.customerRepository.create({ customerId, userId, }); yield this.customerRepository.save(customer); return customer; }); } /** * Create new product */ createProduct(params, productId = uuidv4.uuid()) { return tslib.__awaiter(this, void 0, void 0, function* () { const { entityId, userId } = params; const product = this.productRepository.create({ entityId, userId, productId, }); yield this.productRepository.save(product); return product; }); } /** * Create new price */ createPrice(params, priceId = uuidv4.uuid()) { return tslib.__awaiter(this, void 0, void 0, function* () { const { productId, currency, unitAmount, userId } = params; const price = this.priceRepository.create({ priceId, productId, userId, currency, unitAmount, }); yield this.priceRepository.save(price); return price; }); } /** * Get transaction by transactionId */ getTransactionById(transactionId) { return tslib.__awaiter(this, void 0, void 0, function* () { const transaction = yield this.transactionRepository.findOne({ id: transactionId }); if (!transaction) { throw new microserviceNodejsLib.BaseException({ status: 500, message: messages.getNotFoundMessage('Transaction'), }); } return transaction; }); } /** * Get the customer */ getCustomer(userId) { return tslib.__awaiter(this, void 0, void 0, function* () { const customer = yield this.customerRepository.findOne({ userId }); if (customer) { return customer; } return this.createCustomer(userId); }); } /** * Create coupon */ createCoupon(params, couponId) { return tslib.__awaiter(this, void 0, void 0, function* () { const products = yield this.productRepository.find({ where: { productId: typeorm.In(params.products), }, }); if (!products || products.length !== params.products.length) { throw new microserviceNodejsLib.BaseException({ status: 400, message: "One or more products don't exists.", }); } const coupon = this.couponRepository.create(Object.assign(Object.assign({}, params), { couponId, products })); yield this.couponRepository.save(coupon); return coupon; }); } } module.exports = Abstract;