Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions api/services/purchaseService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand Down Expand Up @@ -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 => {
Expand Down
9 changes: 5 additions & 4 deletions db/migrations/20190520142255_payment_type_payment_option.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');``
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are those back tick's after the semi colon?

table
.integer('user_id')
.references('id')
Expand Down
12 changes: 9 additions & 3 deletions db/seeds/11_payment_option.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down