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