feat(medusa-payment-stripe): Ability to add payment description and improve status resolution (#1404)

This commit is contained in:
Adrien de Peretti
2022-05-10 17:34:41 +02:00
committed by GitHub
parent eae37d8449
commit 327614e126
7 changed files with 25 additions and 35 deletions
@@ -61,7 +61,6 @@ export const carts = {
id: IdMap.getId("product"), id: IdMap.getId("product"),
}, },
quantity: 1, quantity: 1,
quantity: 10,
}, },
], ],
shipping_methods: [ shipping_methods: [
@@ -96,6 +95,7 @@ export const carts = {
billing_address: {}, billing_address: {},
discounts: [], discounts: [],
customer_id: IdMap.getId("lebron"), customer_id: IdMap.getId("lebron"),
context: {}
}, },
frCartNoStripeCustomer: { frCartNoStripeCustomer: {
id: IdMap.getId("fr-cart-no-customer"), id: IdMap.getId("fr-cart-no-customer"),
@@ -22,6 +22,7 @@ export const StripeMock = {
id: "pi_lebron", id: "pi_lebron",
amount: 100, amount: 100,
customer: "cus_123456789_new", customer: "cus_123456789_new",
description: data?.description,
}) })
} }
if (data.customer === "cus_lebron") { if (data.customer === "cus_lebron") {
@@ -29,6 +30,7 @@ export const StripeMock = {
id: "pi_lebron", id: "pi_lebron",
amount: 100, amount: 100,
customer: "cus_lebron", customer: "cus_lebron",
description: data?.description,
}) })
} }
}), }),
@@ -70,11 +70,13 @@ describe("StripeProviderService", () => {
it("returns created stripe payment intent for cart with no customer", async () => { it("returns created stripe payment intent for cart with no customer", async () => {
carts.frCart.customer_id = "" carts.frCart.customer_id = ""
carts.frCart.context.payment_description = 'some description'
result = await stripeProviderService.createPayment(carts.frCart) result = await stripeProviderService.createPayment(carts.frCart)
expect(result).toEqual({ expect(result).toEqual({
id: "pi_lebron", id: "pi_lebron",
customer: "cus_lebron", customer: "cus_lebron",
amount: 100, amount: 100,
description: 'some description',
}) })
}) })
}) })
@@ -90,6 +90,7 @@ class BancontactProviderService extends PaymentService {
const intentRequest = { const intentRequest = {
amount: Math.round(amount), amount: Math.round(amount),
description: cart?.context?.payment_description ?? this.options?.payment_description,
currency: currency_code, currency: currency_code,
payment_method_types: ["bancontact"], payment_method_types: ["bancontact"],
capture_method: "automatic", capture_method: "automatic",
@@ -90,6 +90,7 @@ class GiropayProviderService extends PaymentService {
const intentRequest = { const intentRequest = {
amount: Math.round(amount), amount: Math.round(amount),
description: cart?.context?.payment_description ?? this.options?.payment_description,
currency: currency_code, currency: currency_code,
payment_method_types: ["giropay"], payment_method_types: ["giropay"],
capture_method: "automatic", capture_method: "automatic",
@@ -90,6 +90,7 @@ class IdealProviderService extends PaymentService {
const intentRequest = { const intentRequest = {
amount: Math.round(amount), amount: Math.round(amount),
description: cart?.context?.payment_description ?? this.options?.payment_description,
currency: currency_code, currency: currency_code,
payment_method_types: ["ideal"], payment_method_types: ["ideal"],
capture_method: "automatic", capture_method: "automatic",
@@ -1,6 +1,6 @@
import _ from "lodash"
import Stripe from "stripe" import Stripe from "stripe"
import { PaymentService } from "medusa-interfaces" import { PaymentService } from "medusa-interfaces"
import { PaymentSessionStatus } from '@medusajs/medusa/dist'
class StripeProviderService extends PaymentService { class StripeProviderService extends PaymentService {
static identifier = "stripe" static identifier = "stripe"
@@ -42,37 +42,21 @@ class StripeProviderService extends PaymentService {
const { id } = paymentData const { id } = paymentData
const paymentIntent = await this.stripe_.paymentIntents.retrieve(id) const paymentIntent = await this.stripe_.paymentIntents.retrieve(id)
let status = "pending" switch (paymentIntent.status) {
case "requires_payment_method":
if (paymentIntent.status === "requires_payment_method") { case "requires_confirmation":
return status case "processing":
return PaymentSessionStatus.PENDING
case "requires_action":
return PaymentSessionStatus.REQUIRES_MORE
case "canceled":
return PaymentSessionStatus.CANCELED
case "requires_capture":
case "succeeded":
return PaymentSessionStatus.AUTHORIZED
default:
return PaymentSessionStatus.PENDING
} }
if (paymentIntent.status === "requires_confirmation") {
return status
}
if (paymentIntent.status === "processing") {
return status
}
if (paymentIntent.status === "requires_action") {
status = "requires_more"
}
if (paymentIntent.status === "canceled") {
status = "canceled"
}
if (paymentIntent.status === "requires_capture") {
status = "authorized"
}
if (paymentIntent.status === "succeeded") {
status = "authorized"
}
return status
} }
/** /**
@@ -141,6 +125,7 @@ class StripeProviderService extends PaymentService {
const amount = await this.totalsService_.getTotal(cart) const amount = await this.totalsService_.getTotal(cart)
const intentRequest = { const intentRequest = {
description: cart?.context?.payment_description ?? this.options?.payment_description,
amount: Math.round(amount), amount: Math.round(amount),
currency: currency_code, currency: currency_code,
setup_future_usage: "on_session", setup_future_usage: "on_session",
@@ -169,11 +154,9 @@ class StripeProviderService extends PaymentService {
intentRequest.customer = stripeCustomer.id intentRequest.customer = stripeCustomer.id
} }
const paymentIntent = await this.stripe_.paymentIntents.create( return await this.stripe_.paymentIntents.create(
intentRequest intentRequest
) )
return paymentIntent
} }
/** /**