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

View File

@@ -61,7 +61,6 @@ export const carts = {
id: IdMap.getId("product"),
},
quantity: 1,
quantity: 10,
},
],
shipping_methods: [
@@ -96,6 +95,7 @@ export const carts = {
billing_address: {},
discounts: [],
customer_id: IdMap.getId("lebron"),
context: {}
},
frCartNoStripeCustomer: {
id: IdMap.getId("fr-cart-no-customer"),

View File

@@ -22,6 +22,7 @@ export const StripeMock = {
id: "pi_lebron",
amount: 100,
customer: "cus_123456789_new",
description: data?.description,
})
}
if (data.customer === "cus_lebron") {
@@ -29,6 +30,7 @@ export const StripeMock = {
id: "pi_lebron",
amount: 100,
customer: "cus_lebron",
description: data?.description,
})
}
}),

View File

@@ -70,11 +70,13 @@ describe("StripeProviderService", () => {
it("returns created stripe payment intent for cart with no customer", async () => {
carts.frCart.customer_id = ""
carts.frCart.context.payment_description = 'some description'
result = await stripeProviderService.createPayment(carts.frCart)
expect(result).toEqual({
id: "pi_lebron",
customer: "cus_lebron",
amount: 100,
description: 'some description',
})
})
})

View File

@@ -90,6 +90,7 @@ class BancontactProviderService extends PaymentService {
const intentRequest = {
amount: Math.round(amount),
description: cart?.context?.payment_description ?? this.options?.payment_description,
currency: currency_code,
payment_method_types: ["bancontact"],
capture_method: "automatic",

View File

@@ -90,6 +90,7 @@ class GiropayProviderService extends PaymentService {
const intentRequest = {
amount: Math.round(amount),
description: cart?.context?.payment_description ?? this.options?.payment_description,
currency: currency_code,
payment_method_types: ["giropay"],
capture_method: "automatic",

View File

@@ -90,6 +90,7 @@ class IdealProviderService extends PaymentService {
const intentRequest = {
amount: Math.round(amount),
description: cart?.context?.payment_description ?? this.options?.payment_description,
currency: currency_code,
payment_method_types: ["ideal"],
capture_method: "automatic",

View File

@@ -1,6 +1,6 @@
import _ from "lodash"
import Stripe from "stripe"
import { PaymentService } from "medusa-interfaces"
import { PaymentSessionStatus } from '@medusajs/medusa/dist'
class StripeProviderService extends PaymentService {
static identifier = "stripe"
@@ -42,37 +42,21 @@ class StripeProviderService extends PaymentService {
const { id } = paymentData
const paymentIntent = await this.stripe_.paymentIntents.retrieve(id)
let status = "pending"
if (paymentIntent.status === "requires_payment_method") {
return status
switch (paymentIntent.status) {
case "requires_payment_method":
case "requires_confirmation":
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 intentRequest = {
description: cart?.context?.payment_description ?? this.options?.payment_description,
amount: Math.round(amount),
currency: currency_code,
setup_future_usage: "on_session",
@@ -169,11 +154,9 @@ class StripeProviderService extends PaymentService {
intentRequest.customer = stripeCustomer.id
}
const paymentIntent = await this.stripe_.paymentIntents.create(
return await this.stripe_.paymentIntents.create(
intentRequest
)
return paymentIntent
}
/**