Automatic stripe customers; address

This commit is contained in:
Sebastian Rindom
2020-07-14 08:10:09 +02:00
parent cd989c56a7
commit 44c7177e43
7 changed files with 24 additions and 18 deletions

View File

@@ -119,7 +119,7 @@ class StripeProviderService extends PaymentService {
customer: stripeCustomerId, customer: stripeCustomerId,
amount: amount * 100, // Stripe amount is in cents amount: amount * 100, // Stripe amount is in cents
currency: currency_code, currency: currency_code,
setup_future_usage: "on-session", setup_future_usage: "on_session",
capture_method: "manual", capture_method: "manual",
metadata: { cart_id: `${cart._id}` }, metadata: { cart_id: `${cart._id}` },
}) })

View File

@@ -29,15 +29,18 @@ export default async (req, res) => {
} }
let customerId = "" let customerId = ""
let email = ""
if (req.user && req.user.customer_id) { if (req.user && req.user.customer_id) {
const customerService = req.scope.resolve("customerService") const customerService = req.scope.resolve("customerService")
const customer = await customerService.retrieve(req.user.customer_id) const customer = await customerService.retrieve(req.user.customer_id)
customerId = customer._id customerId = customer._id
email = customer.email
} }
let cart = await cartService.create({ let cart = await cartService.create({
region_id: regionId, region_id: regionId,
customer_id: customerId, customer_id: customerId,
email,
}) })
if (value.items) { if (value.items) {

View File

@@ -6,7 +6,11 @@ export default async (req, res) => {
// If there is a logged in user add the user to the cart // If there is a logged in user add the user to the cart
if (req.user && req.user.customer_id) { if (req.user && req.user.customer_id) {
if (!cart.customer_id || cart.customer_id !== req.user.customer_id) { if (
!cart.customer_id ||
!cart.email ||
cart.customer_id !== req.user.customer_id
) {
const customerService = req.scope.resolve("customerService") const customerService = req.scope.resolve("customerService")
const customer = await customerService.retrieve(req.user.customer_id) const customer = await customerService.retrieve(req.user.customer_id)

View File

@@ -4,14 +4,14 @@
import mongoose from "mongoose" import mongoose from "mongoose"
export default new mongoose.Schema({ export default new mongoose.Schema({
first_name: { type: String, required: true }, first_name: { type: String },
last_name: { type: String, required: true }, last_name: { type: String },
address_1: { type: String, required: true }, address_1: { type: String },
address_2: { type: String }, address_2: { type: String },
city: { type: String, required: true }, city: { type: String },
country_code: { type: String, required: true }, country_code: { type: String },
province: { type: String }, province: { type: String },
postal_code: { type: String, required: true }, postal_code: { type: String },
phone: { type: String }, phone: { type: String },
metadata: { type: mongoose.Schema.Types.Mixed, default: {} }, metadata: { type: mongoose.Schema.Types.Mixed, default: {} },
}) })

View File

@@ -211,11 +211,7 @@ class CartService extends BaseService {
if (region.countries.length === 1) { if (region.countries.length === 1) {
// Preselect the country if the region only has 1 // Preselect the country if the region only has 1
data.shipping_address = { data.shipping_address = {
country_code: countries[0], country_code: region.countries[0],
}
data.billing_address = {
country_code: countries[0],
} }
} }

View File

@@ -259,7 +259,7 @@ class CustomerService extends BaseService {
const customer = await this.retrieve(customerId) const customer = await this.retrieve(customerId)
this.validateBillingAddress_(address) this.validateBillingAddress_(address)
let shouldAdd = !!customer.shipping_addresses.find( let shouldAdd = !customer.shipping_addresses.find(
a => a =>
a.country_code === address.country_code && a.country_code === address.country_code &&
a.address_1 === address.address_1 && a.address_1 === address.address_1 &&

View File

@@ -21,10 +21,13 @@ class OrderSubscriber {
this.eventBus_.subscribe("order.placed", async order => { this.eventBus_.subscribe("order.placed", async order => {
await this.customerService_.addOrder(order.customer_id, order._id) await this.customerService_.addOrder(order.customer_id, order._id)
await this.customerService_.addAddress(
order.customer_id, const address = {
order.shipping_address ...order.shipping_address,
) }
delete address._id
await this.customerService_.addAddress(order.customer_id, address)
}) })
this.eventBus_.subscribe("order.placed", async order => { this.eventBus_.subscribe("order.placed", async order => {