diff --git a/api/services/purchaseService.js b/api/services/purchaseService.js index a060bbe..ef5e490 100644 --- a/api/services/purchaseService.js +++ b/api/services/purchaseService.js @@ -2,6 +2,7 @@ const knex = require('../../db/knex'); const statusService = require('./statusService'); const invoiceService = require('./invoiceService'); const userService = require('./userService'); +const paymentOptionService = require('./paymentOptionService'); const Taxjar = require('taxjar'); const client = new Taxjar({ apiKey: process.env.TAXJAR_API_KEY }); @@ -46,21 +47,43 @@ const findPurchaseByUserId = (user_id, status) => { const savePurchase = (id, purchase) => { return userService .findCartSubTotalByUserId(id) - .then(subTotal => { + .then(async subTotal => { // creating the tax info and submitting to taxjar const payload = { from_country: 'US', // TODO convert hardcoded data to variable data. from_city: 'Las Vegas', from_state: 'NV', from_street: 'Lake Mead Blvd', - to_country: purchase.country, - to_city: purchase.city, - to_zip: purchase.zip, - to_state: purchase.state, - to_street: purchase.street_one, amount: subTotal, shipping: 5.0, }; + let street, city, state, country, zip_code; + if (purchase.same_address) { + const paymentOptions = await paymentOptionService.findAllPaymentOptionByUser( + id, + ); + const paymentOption = paymentOptions.filter( + option => option.id === purchase.payment_id, + ); + const { street, city, state, country, zip_code } = paymentOption; + street = street; + city = city; + state = state; + country = country; + zip_code = zip_code; + } else { + const { street, city, state, country, zip_code } = purchase.address; + street = street; + city = city; + state = state; + country = country; + zip_code = zip_code; + } + payload.to_country = country; + payload.to_city = city; + payload.to_street = street; + payload.to_state = state; + payload.to_zip = zip_code; return client.taxForOrder(payload); }) .then(res => { diff --git a/db/migrations/20190520142255_payment_type_payment_option.js b/db/migrations/20190520142255_payment_type_payment_option.js index 71db096..573e756 100644 --- a/db/migrations/20190520142255_payment_type_payment_option.js +++ b/db/migrations/20190520142255_payment_type_payment_option.js @@ -7,15 +7,16 @@ exports.up = function(knex, Promise) { .createTable('payment_option', table => { table.increments('id'); table.string('credit_card', 16); - table.string('address_one', 100); - table.string('address_two', 100); + table.integer('security_number', 3); + table.string('street', 100); + table.string('state', 100); + table.string('city', 100); table.string('full_name', 30); table.string('country', 2); table.string('postal_code', 5); table.string('exp_month', 2); table.string('exp_year', 4); - table.integer('security_number', 3); - table.boolean('active'); + table.boolean('active');`` table .integer('user_id') .references('id') diff --git a/db/seeds/11_payment_option.js b/db/seeds/11_payment_option.js index 847bffb..b73d66d 100644 --- a/db/seeds/11_payment_option.js +++ b/db/seeds/11_payment_option.js @@ -8,7 +8,9 @@ exports.seed = function(knex, Promise) { { credit_card: 424242424242, security_number: 444, - address_one: '123 whambam st', + street: '123 whambam st', + state: 'Nevada', + city: 'Las Vegas', full_name: 'lorem ipsum', country: 'US', //iso2 country code postal_code: '55555', @@ -20,7 +22,9 @@ exports.seed = function(knex, Promise) { }, { credit_card: 4242424242423, - address_one: '424 whambam st', + street: '424 whambam st', + state: 'Nevada', + city: 'Las Vegas', exp_month: '05', exp_year: '2020', postal_code: '55555', @@ -33,7 +37,9 @@ exports.seed = function(knex, Promise) { }, { credit_card: 324242424242, - address_one: '324 whambam st', + street: '324 whambam st', + state: 'Nevada', + city: 'Las Vegas', exp_month: '05', exp_year: '2020', country: 'US',