UNPKG

@lomray/microservice-payment-stripe

Version:

Stripe payment microservice based on NodeJS & inverted json.

244 lines (240 loc) 10.7 kB
'use strict'; var tslib = require('tslib'); var microserviceHelpers = require('@lomray/microservice-helpers'); var classValidator = require('class-validator'); var classValidatorJsonschema = require('class-validator-jsonschema'); var typeorm = require('typeorm'); var chargeRefundStatus = require('../constants/charge-refund-status.js'); var transactionDefaultParams = require('../constants/transaction-default-params.js'); var transactionStatus = require('../constants/transaction-status.js'); var transactionType = require('../constants/transaction-type.js'); var isStripeIdValid = require('../helpers/validators/is-stripe-id-valid.js'); var Transaction_1; let Transaction = Transaction_1 = class Transaction { /** * Check is cardId exist */ static isCardOrBankAccountExist(entity) { return Boolean(entity.cardId) || Boolean(entity.bankAccountId); } }; tslib.__decorate([ typeorm.PrimaryGeneratedColumn('uuid'), classValidator.Allow(), tslib.__metadata("design:type", String) ], Transaction.prototype, "id", void 0); tslib.__decorate([ classValidatorJsonschema.JSONSchema({ description: 'Stripe transaction id (payment intent)', example: 'pi_3Nha3JAmQ4asS8PS0JPXIyEh', }), typeorm.Column({ type: 'varchar', length: 66 }), classValidator.Length(1, 66), tslib.__metadata("design:type", String) ], Transaction.prototype, "transactionId", void 0); tslib.__decorate([ classValidatorJsonschema.JSONSchema({ description: 'Stripe Tax Calculation API - calculated by Stripe tax for transaction (e.g. payment intent)', example: 'taxcalc_1OFdOqFpQjUWTpHe3KV73alY', }), typeorm.Column({ type: 'varchar', length: 66, default: null }), classValidator.Length(1, 66), microserviceHelpers.IsUndefinable(), microserviceHelpers.IsNullable(), tslib.__metadata("design:type", Object) ], Transaction.prototype, "taxCalculationId", void 0); tslib.__decorate([ classValidatorJsonschema.JSONSchema({ description: 'Stripe Tax Transaction API - Stripe tax transaction presented in Stripe Tax reports', example: 'tax_1OFdOqFpQjUWTpHeIDJlZQwS', }), typeorm.Column({ type: 'varchar', length: 66, default: null }), classValidator.Length(1, 66), microserviceHelpers.IsUndefinable(), microserviceHelpers.IsNullable(), tslib.__metadata("design:type", Object) ], Transaction.prototype, "taxTransactionId", void 0); tslib.__decorate([ classValidatorJsonschema.JSONSchema({ description: 'Represents a single attempt to move money into Platform Stripe account. Required for refunds, reversals, fees', example: 'ch_3OFZJjAmQ4asS8PS1l9MLI4v', }), typeorm.Column({ type: 'varchar', length: 66, default: null }), classValidator.Length(1, 66), microserviceHelpers.IsUndefinable(), microserviceHelpers.IsNullable(), tslib.__metadata("design:type", Object) ], Transaction.prototype, "chargeId", void 0); tslib.__decorate([ classValidatorJsonschema.JSONSchema({ description: `Stripe payment intent application fee id. Contain Stripe fee, collected by application (platform) fee, tax. Transaction can be refunded with or without application fee: in this case for understanding NET transaction amount check application fee status (refunded or not) and amount`, example: 'fee_1OEqCzPBMR5FbqzbywqfYDwA', }), typeorm.Column({ type: 'varchar', length: 66, default: null }), classValidator.Length(1, 66), microserviceHelpers.IsUndefinable(), microserviceHelpers.IsNullable(), tslib.__metadata("design:type", Object) ], Transaction.prototype, "applicationFeeId", void 0); tslib.__decorate([ typeorm.Column({ type: 'varchar', length: 100, default: '' }), microserviceHelpers.IsUndefinable(), classValidator.Length(1, 100), tslib.__metadata("design:type", String) ], Transaction.prototype, "title", void 0); tslib.__decorate([ classValidatorJsonschema.JSONSchema({ description: 'UserId from our microservice for user (customer) which pays (init transaction)', }), typeorm.Column({ type: 'varchar', length: 36 }), classValidator.Length(1, 36), tslib.__metadata("design:type", String) ], Transaction.prototype, "userId", void 0); tslib.__decorate([ typeorm.Column({ type: 'enum', enum: transactionType }), classValidator.IsEnum(transactionType), microserviceHelpers.IsUndefinable(), tslib.__metadata("design:type", String) ], Transaction.prototype, "type", void 0); tslib.__decorate([ typeorm.Column({ type: 'varchar', length: 66, default: null }), isStripeIdValid(), classValidator.Length(1, 66), microserviceHelpers.IsUndefinable(), microserviceHelpers.IsNullable(), tslib.__metadata("design:type", Object) ], Transaction.prototype, "bankAccountId", void 0); tslib.__decorate([ typeorm.Column({ type: 'varchar', length: 66, default: null }), isStripeIdValid(), classValidator.Length(1, 66), microserviceHelpers.IsUndefinable(), microserviceHelpers.IsNullable(), tslib.__metadata("design:type", Object) ], Transaction.prototype, "cardId", void 0); tslib.__decorate([ classValidatorJsonschema.JSONSchema({ description: `Payment method that was used to charge, for instance: card, bank account, etc.. Payment method is stripe entity that attached to customer. Setup intent don't have card or bank account id`, example: 'pm_1N0vl32eZvKYlo2CiORpHAvo', }), typeorm.Column({ type: 'varchar', length: 27, default: null }), isStripeIdValid(), classValidator.Length(1, 27), microserviceHelpers.IsValidate(Transaction_1, (e) => !Transaction_1.isCardOrBankAccountExist(e)), tslib.__metadata("design:type", Object) ], Transaction.prototype, "paymentMethodId", void 0); tslib.__decorate([ classValidatorJsonschema.JSONSchema({ description: `Microservice entity. Single line item (ticket, product) or for custom low-level workflow (payment intent) - payment group`, }), typeorm.Column({ type: 'varchar', length: 36 }), microserviceHelpers.IsUndefinable(), classValidator.Length(1, 36), tslib.__metadata("design:type", String) ], Transaction.prototype, "entityId", void 0); tslib.__decorate([ classValidatorJsonschema.JSONSchema({ description: 'Processing amount, includes all fees, taxes. Presented in units, for instance: $100 = 10000 unit', }), typeorm.Column({ type: 'int' }), classValidator.IsNumber(), tslib.__metadata("design:type", Number) ], Transaction.prototype, "amount", void 0); tslib.__decorate([ classValidatorJsonschema.JSONSchema({ description: `Sales tax or other, that should be paid to the government by tax collector. Tax included in the payment intent amount and storing as collected fees amount.`, }), typeorm.Column({ type: 'int', default: 0 }), microserviceHelpers.IsUndefinable(), classValidator.IsNumber(), tslib.__metadata("design:type", Number) ], Transaction.prototype, "tax", void 0); tslib.__decorate([ classValidatorJsonschema.JSONSchema({ description: `Total fees: application, stripe, amount that application collect, tax, debit and credit extra fees. Contain all amounts that Platform is required or interested to grab from transaction. Should be the same as in Stripe`, }), typeorm.Column({ type: 'int', default: 0 }), microserviceHelpers.IsUndefinable(), classValidator.IsNumber(), tslib.__metadata("design:type", Number) ], Transaction.prototype, "fee", void 0); tslib.__decorate([ classValidatorJsonschema.JSONSchema({ description: 'Field for storing status of payment by the card or any other source.', }), typeorm.Column({ type: 'enum', enum: transactionStatus, default: transactionStatus.INITIAL }), classValidator.IsEnum(transactionStatus), microserviceHelpers.IsUndefinable(), tslib.__metadata("design:type", String) ], Transaction.prototype, "status", void 0); tslib.__decorate([ classValidatorJsonschema.JSONSchema({ description: 'If transaction was fail refund on second partial refund - this status indicate real refund state.', }), typeorm.Column({ type: 'enum', enum: chargeRefundStatus, default: chargeRefundStatus.NO_REFUND }), classValidator.IsEnum(chargeRefundStatus), microserviceHelpers.IsUndefinable(), tslib.__metadata("design:type", String) ], Transaction.prototype, "chargeRefundStatus", void 0); tslib.__decorate([ classValidatorJsonschema.JSONSchema({ description: 'Does transaction was disputed. Chargeback or injury occur.', }), typeorm.Column({ type: 'boolean', default: false }), classValidator.IsBoolean(), microserviceHelpers.IsUndefinable(), tslib.__metadata("design:type", Boolean) ], Transaction.prototype, "isDisputed", void 0); tslib.__decorate([ classValidatorJsonschema.JSONSchema({ description: 'Store data about payment connected account and etc.', }), typeorm.Column({ type: 'json', default: transactionDefaultParams }), classValidator.IsObject(), microserviceHelpers.IsUndefinable(), tslib.__metadata("design:type", Object) ], Transaction.prototype, "params", void 0); tslib.__decorate([ microserviceHelpers.IsTypeormDate(), typeorm.CreateDateColumn(), tslib.__metadata("design:type", Date) ], Transaction.prototype, "createdAt", void 0); tslib.__decorate([ microserviceHelpers.IsTypeormDate(), typeorm.UpdateDateColumn(), tslib.__metadata("design:type", Date) ], Transaction.prototype, "updatedAt", void 0); tslib.__decorate([ typeorm.ManyToOne('Product', 'transactions'), typeorm.JoinColumn({ name: 'productId', }), tslib.__metadata("design:type", Function) ], Transaction.prototype, "product", void 0); tslib.__decorate([ typeorm.ManyToOne('Customer', 'transactions'), typeorm.JoinColumn({ name: 'userId', referencedColumnName: 'userId', }), tslib.__metadata("design:type", Function) ], Transaction.prototype, "customer", void 0); Transaction = Transaction_1 = tslib.__decorate([ classValidatorJsonschema.JSONSchema({ // Check payment stripe docs fo detailed description description: 'Stipe transaction presentation. Disputed transaction can be refundable if disputed charge have is refundable to true', properties: { customer: { $ref: '#/definitions/Customer' }, product: { $ref: '#/definitions/Product' }, }, }), typeorm.Entity() ], Transaction); var TransactionEntity = Transaction; module.exports = TransactionEntity;