Skip to content

Commit

Permalink
feat(medusa-payment-stripe): Ability to add payment description and i…
Browse files Browse the repository at this point in the history
…mprove status resolution (#1404)
  • Loading branch information
adrien2p committed May 10, 2022
1 parent eae37d8 commit 327614e
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 35 deletions.
2 changes: 1 addition & 1 deletion packages/medusa-payment-stripe/src/__mocks__/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export const carts = {
id: IdMap.getId("product"),
},
quantity: 1,
quantity: 10,
},
],
shipping_methods: [
Expand Down Expand Up @@ -96,6 +95,7 @@ export const carts = {
billing_address: {},
discounts: [],
customer_id: IdMap.getId("lebron"),
context: {}
},
frCartNoStripeCustomer: {
id: IdMap.getId("fr-cart-no-customer"),
Expand Down
2 changes: 2 additions & 0 deletions packages/medusa-payment-stripe/src/__mocks__/stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ export const StripeMock = {
id: "pi_lebron",
amount: 100,
customer: "cus_123456789_new",
description: data?.description,
})
}
if (data.customer === "cus_lebron") {
return Promise.resolve({
id: "pi_lebron",
amount: 100,
customer: "cus_lebron",
description: data?.description,
})
}
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class GiropayProviderService extends PaymentService {

const intentRequest = {
amount: Math.round(amount),
description: cart?.context?.payment_description ?? this.options?.payment_description,

This comment has been minimized.

Copy link
@ayushbgl

ayushbgl Jul 25, 2022

Should it be this.options_?.payment_description?

currency: currency_code,
payment_method_types: ["giropay"],
capture_method: "automatic",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
51 changes: 17 additions & 34 deletions packages/medusa-payment-stripe/src/services/stripe-provider.js
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}

if (paymentIntent.status === "requires_confirmation") {
return status
}

if (paymentIntent.status === "processing") {
return status
}

if (paymentIntent.status === "requires_action") {
status = "requires_more"
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 === "canceled") {
status = "canceled"
}

if (paymentIntent.status === "requires_capture") {
status = "authorized"
}

if (paymentIntent.status === "succeeded") {
status = "authorized"
}

return status
}

/**
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
}

/**
Expand Down

0 comments on commit 327614e

Please sign in to comment.