hotfix(medusa-payment-stripe): Support provider specific intent options (#2581)

This commit is contained in:
Oliver Windall Juhl authored and GitHub committed 2022-11-10 19:28:35 +01:00
1 parent c0cdbd025e
commit 04e894ec39
8 files changed
+73 -27

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"medusa-payment-stripe": patch
---
Support provider specific intent options
@@ -1,5 +1,5 @@
import Stripe from "stripe"
import { AbstractPaymentService, PaymentSessionData } from "@medusajs/medusa" import { AbstractPaymentService, PaymentSessionData } from "@medusajs/medusa"
import Stripe from "stripe"
class StripeBase extends AbstractPaymentService { class StripeBase extends AbstractPaymentService {
static identifier = null static identifier = null
@@ -58,6 +58,25 @@ class StripeBase extends AbstractPaymentService {
this.manager_ = manager this.manager_ = manager
} }
getPaymentIntentOptions() {
const options = {}
if (this?.paymentIntentOptions?.capture_method) {
options.capture_method = this.paymentIntentOptions.capture_method
}
if (this?.paymentIntentOptions?.setup_future_usage) {
options.setup_future_usage = this.paymentIntentOptions.setup_future_usage
}
if (this?.paymentIntentOptions?.payment_method_types) {
options.payment_method_types =
this.paymentIntentOptions.payment_method_types
}
return options
}
/** /**
* Fetches Stripe payment intent. Check its status and returns the * Fetches Stripe payment intent. Check its status and returns the
* corresponding Medusa status. * corresponding Medusa status.
@@ -104,19 +123,13 @@ class StripeBase extends AbstractPaymentService {
* @return {Promise<PaymentSessionData>} Stripe payment intent * @return {Promise<PaymentSessionData>} Stripe payment intent
*/ */
async createPayment(cart) { async createPayment(cart) {
const intentRequest = { const intentRequest = this.getPaymentIntentOptions()
payment_method_types: this.paymentMethodTypes,
capture_method: "automatic",
}
return await this.stripeProviderService_.createPayment(cart, intentRequest) return await this.stripeProviderService_.createPayment(cart, intentRequest)
} }
async createPaymentNew(paymentInput) { async createPaymentNew(paymentInput) {
const intentRequest = { const intentRequest = this.getPaymentIntentOptions()
payment_method_types: this.paymentMethodTypes,
capture_method: "automatic",
}
return await this.stripeProviderService_.createPaymentNew( return await this.stripeProviderService_.createPaymentNew(
paymentInput, paymentInput,
@@ -21,10 +21,16 @@ class BancontactProviderService extends StripeBase {
regionService, regionService,
manager, manager,
}, },
options, options
["bancontact"]
) )
} }
get paymentIntentOptions() {
return {
payment_method_types: ["bancontact"],
capture_method: "automatic",
}
}
} }
export default BancontactProviderService export default BancontactProviderService
@@ -21,10 +21,16 @@ class BlikProviderService extends StripeBase {
regionService, regionService,
manager, manager,
}, },
options, options
["blik"]
) )
} }
get paymentIntentOptions() {
return {
payment_method_types: ["blik"],
capture_method: "automatic",
}
}
} }
export default BlikProviderService export default BlikProviderService
@@ -21,10 +21,16 @@ class GiropayProviderService extends StripeBase {
regionService, regionService,
manager, manager,
}, },
options, options
["giropay"]
) )
} }
get paymentIntentOptions() {
return {
payment_method_types: ["giropay"],
capture_method: "automatic",
}
}
} }
export default GiropayProviderService export default GiropayProviderService
@@ -21,10 +21,16 @@ class IdealProviderService extends StripeBase {
regionService, regionService,
manager, manager,
}, },
options, options
["ideal"]
) )
} }
get paymentIntentOptions() {
return {
payment_method_types: ["ideal"],
capture_method: "automatic",
}
}
} }
export default IdealProviderService export default IdealProviderService
@@ -1,9 +1,9 @@
import Stripe from "stripe"
import { import {
AbstractPaymentService, AbstractPaymentService,
PaymentSessionData, PaymentSessionData,
PaymentSessionStatus, PaymentSessionStatus,
} from "@medusajs/medusa" } from "@medusajs/medusa"
import Stripe from "stripe"
class StripeProviderService extends AbstractPaymentService { class StripeProviderService extends AbstractPaymentService {
static identifier = "stripe" static identifier = "stripe"
@@ -89,7 +89,7 @@ class StripeProviderService extends AbstractPaymentService {
/** /**
* Fetches a Stripe customer * Fetches a Stripe customer
* @param {string} customerId - Stripe customer id * @param {string} customerId - Stripe customer id
* @returns {Promise<object>} Stripe customer * @return {Promise<object>} Stripe customer
*/ */
async retrieveCustomer(customerId) { async retrieveCustomer(customerId) {
if (!customerId) { if (!customerId) {
@@ -101,7 +101,7 @@ class StripeProviderService extends AbstractPaymentService {
/** /**
* Creates a Stripe customer using a Medusa customer. * Creates a Stripe customer using a Medusa customer.
* @param {object} customer - Customer data from Medusa * @param {object} customer - Customer data from Medusa
* @returns {Promise<object>} Stripe customer * @return {Promise<object>} Stripe customer
*/ */
async createCustomer(customer) { async createCustomer(customer) {
try { try {
@@ -144,7 +144,6 @@ class StripeProviderService extends AbstractPaymentService {
amount: Math.round(amount), amount: Math.round(amount),
currency: currency_code, currency: currency_code,
metadata: { cart_id: `${cart.id}` }, metadata: { cart_id: `${cart.id}` },
setup_future_usage: "on_session",
capture_method: this.options_.capture ? "automatic" : "manual", capture_method: this.options_.capture ? "automatic" : "manual",
...intentRequestData, ...intentRequestData,
} }
@@ -183,14 +182,13 @@ class StripeProviderService extends AbstractPaymentService {
const { customer, currency_code, amount, resource_id, cart } = paymentInput const { customer, currency_code, amount, resource_id, cart } = paymentInput
const { id: customer_id, email } = customer const { id: customer_id, email } = customer
let intentRequest = { const intentRequest = {
description: description:
cart?.context?.payment_description ?? cart?.context?.payment_description ??
this.options_?.payment_description, this.options_?.payment_description,
amount: Math.round(amount), amount: Math.round(amount),
currency: currency_code, currency: currency_code,
metadata: { resource_id }, metadata: { resource_id },
setup_future_usage: "on_session",
capture_method: this.options_.capture ? "automatic" : "manual", capture_method: this.options_.capture ? "automatic" : "manual",
...intentRequestData, ...intentRequestData,
} }
@@ -333,7 +331,7 @@ class StripeProviderService extends AbstractPaymentService {
* Updates customer of Stripe payment intent. * Updates customer of Stripe payment intent.
* @param {string} paymentIntentId - id of payment intent to update * @param {string} paymentIntentId - id of payment intent to update
* @param {string} customerId - id of new Stripe customer * @param {string} customerId - id of new Stripe customer
* @returns {object} Stripe payment intent * @return {object} Stripe payment intent
*/ */
async updatePaymentIntentCustomer(paymentIntentId, customerId) { async updatePaymentIntentCustomer(paymentIntentId, customerId) {
try { try {
@@ -408,7 +406,7 @@ class StripeProviderService extends AbstractPaymentService {
* @param {object} data - the data of the webhook request: req.body * @param {object} data - the data of the webhook request: req.body
* @param {object} signature - the Stripe signature on the event, that * @param {object} signature - the Stripe signature on the event, that
* ensures integrity of the webhook event * ensures integrity of the webhook event
* @returns {object} Stripe Webhook event * @return {object} Stripe Webhook event
*/ */
constructWebhookEvent(data, signature) { constructWebhookEvent(data, signature) {
return this.stripe_.webhooks.constructEvent( return this.stripe_.webhooks.constructEvent(
@@ -21,10 +21,16 @@ class Przelewy24ProviderService extends StripeBase {
regionService, regionService,
manager, manager,
}, },
options, options
["p24"]
) )
} }
get paymentIntentOptions() {
return {
payment_method_types: ["p24"],
capture_method: "automatic",
}
}
} }
export default Przelewy24ProviderService export default Przelewy24ProviderService