Permalink
Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign up
Fetching contributors…
Cannot retrieve contributors at this time.
Cannot retrieve contributors at this time
| 'use strict'; | |
| import * as config from 'config'; | |
| import * as q from 'q'; | |
| import * as base64 from 'base-64'; | |
| import { inject, injectable } from 'inversify'; | |
| import { IDENTIFIERS } from '../constants/indentifiers'; | |
| import { BaseService } from './base.service'; | |
| import { IPaymentsService } from './interfaces/payments.service.interface'; | |
| import { IEnvironment } from '../app/scripts/services/interfaces/config.service.interface'; | |
| import { Charge, CreditCard, IOrderSubmission } from './interfaces/order.service.interface'; | |
| import { IAppConfig } from '../interfaces/app.interface'; | |
| import { ConfigService } from './config.service'; | |
| const stripe = require('stripe'); | |
| @injectable() | |
| export class PaymentsService extends BaseService implements IPaymentsService { | |
| private stripe: any = null; | |
| constructor ( | |
| @inject(IDENTIFIERS.ConfigService) private config: ConfigService | |
| ) { | |
| super(); | |
| this.init(); | |
| return this; | |
| } | |
| private init (): void { | |
| let environment: IEnvironment = this.config.getAllEnvironmentVariables(), | |
| application: IAppConfig = config.get('application') as any, | |
| key: string = environment.TEST_SECRET_KEY; | |
| if (this.config.isProduction) { | |
| key = environment.LIVE_SECRET_KEY; | |
| } | |
| this.stripe = stripe(key); | |
| } | |
| public createCharge ( | |
| submission: IOrderSubmission | |
| ): q.Promise<Charge> { | |
| let deferred: q.Deferred<Charge> = q.defer<Charge>(), | |
| self: PaymentsService = this, | |
| amount: number = Math.ceil(submission.order.onlineOrderTotalPrice * 100); | |
| if (submission.customer.hasOwnProperty('cc')) { | |
| let cc: CreditCard = JSON.parse(base64.decode(submission.customer.cc)); | |
| self | |
| .stripe | |
| .customers | |
| .create({ | |
| email: submission.customer.email || 'no-email@hoagiesonmain.com' | |
| }).then((customer: any): void => { | |
| let month: string = cc.exp.substr(0, 2), | |
| year: string = cc.exp.substr(2, 2); | |
| return self | |
| .stripe | |
| .customers | |
| .createSource(customer.id, { | |
| 'source': { | |
| 'object': 'card', | |
| 'exp_month': month, | |
| 'exp_year': `20${year}`, | |
| 'number': cc.cc, | |
| 'cvc': cc.cvc | |
| } | |
| }); | |
| }).then((source: any): void => { | |
| return self | |
| .stripe | |
| .charges | |
| .create({ | |
| 'amount': amount, | |
| 'currency': 'usd', | |
| 'customer': source.customer, | |
| 'description': 'Hoagies On Main' | |
| }); | |
| }).then((charge: Charge): void => { | |
| deferred.resolve(charge); | |
| }).catch((err: any): void => { | |
| // Deal with an error | |
| }); | |
| } else { | |
| this | |
| .stripe | |
| .charges | |
| .create({ | |
| 'amount': amount, | |
| 'currency': 'USD', | |
| 'source': submission.customer.stripe.token.id, | |
| 'description': 'Hoagies On Main' | |
| }, (...args: any[]) => { | |
| if (args && args.length === 2) { | |
| let err: any = args[0], | |
| charge: Charge = args[1]; | |
| if (!err) { | |
| deferred.resolve(charge); | |
| } | |
| } | |
| }); | |
| } | |
| return deferred.promise; | |
| } | |
| } |