registers payment/fulfillment providers with correct names

This commit is contained in:
Sebastian Rindom
2020-05-28 20:17:13 +02:00
parent 9b51cf857c
commit 98a72ca7b4
5 changed files with 35 additions and 18 deletions

View File

@@ -5,7 +5,7 @@ import { PaymentService } from "medusa-interfaces"
class StripeProviderService extends PaymentService {
static identifier = "stripe"
constructor({ customerService, totalsService }, options) {
constructor({ customerService, totalsService, regionService }, options) {
super()
this.options_ = options
@@ -14,6 +14,8 @@ class StripeProviderService extends PaymentService {
this.customerService_ = customerService
this.regionService_ = regionService
this.totalsService_ = totalsService
}
@@ -76,10 +78,10 @@ class StripeProviderService extends PaymentService {
* @returns {string} id of payment intent
*/
async createPayment(cart) {
const { customer_id } = cart
const { customer_id, region_id } = cart
const { currency_code } = await this.regionService_.retrieve(region_id)
let stripeCustomerId
if (!customer_id) {
const { id } = await this.stripe_.customers.create({
email: cart.email,
@@ -97,10 +99,11 @@ class StripeProviderService extends PaymentService {
}
}
const amount = this.totalsService_.getTotal(cart)
const amount = await this.totalsService_.getTotal(cart)
const paymentIntent = await this.stripe_.paymentIntents.create({
customer: stripeCustomerId,
amount,
amount: amount * 100, // Stripe amount is in cents
currency: currency_code
})
return paymentIntent

View File

@@ -32,10 +32,6 @@ export default app => {
)
// Shipping Options
route.post(
"/:id/shipping-options",
middlewares.wrap(require("./create-shipping-options").default)
)
route.post(
"/:id/shipping-methods",
middlewares.wrap(require("./add-shipping-method").default)

View File

@@ -148,12 +148,14 @@ function registerServices(pluginDetails, container) {
cradle => new loaded(cradle, pluginDetails.options)
),
})
} else {
const name = formatRegistrationName(fn)
container.register({
[name]: asFunction(cradle => new loaded(cradle, pluginDetails.options)),
})
}
// Always register the service with a nice name, if we need to import it
// from somewhere
const name = formatRegistrationName(fn)
container.register({
[name]: asFunction(cradle => new loaded(cradle, pluginDetails.options)),
})
})
}

View File

@@ -692,7 +692,15 @@ class CartService extends BaseService {
if (!region.payment_providers.includes(pSession.provider_id)) {
return null
}
return this.paymentProviderService_.updateSession(pSession, cart)
const data = await this.paymentProviderService_.updateSession(
pSession,
cart
)
return {
provider_id: pSession.provider_id,
data,
}
})
)
}
@@ -708,13 +716,20 @@ class CartService extends BaseService {
return null
}
return this.paymentProviderService_.createSession(pId, cart)
const data = await this.paymentProviderService_.createSession(pId, cart)
return {
provider_id: pId,
data,
}
})
)
// Filter null sessions
newSessions = newSessions.filter(s => !!s)
console.log(sessions)
console.log(newSessions)
// Update the payment sessions with the concatenated array of updated and
// newly created payment sessions
return this.cartModel_

View File

@@ -335,8 +335,9 @@ class ShippingProfileService extends BaseService {
async fetchCartOptions(cart) {
const products = this.getProductsInCart_(cart)
const profiles = await this.list({ products: { $in: products } })
const optionIds = profiles.reduce((acc, next) =>
acc.concat(next.shipping_options)
const optionIds = profiles.reduce(
(acc, next) => acc.concat(next.shipping_options),
[]
)
const options = await Promise.all(