@@ -21,13 +21,15 @@ import { AbstractFulfillmentService } from "@medusajs/medusa"
class MyFulfillmentService extends AbstractFulfillmentService {
// methods here...
}
export default MyFulfillmentService
```
---
## Identifier Property
The `FulfillmentProvider` entity has 2 properties: `identifier` and `is_installed`. The `identifier` property in the class is used when the fulfillment provider is created in the database.
The `FulfillmentProvider` entity has 2 properties: `identifier` and `is_installed`. The `identifier` property in the fulfillment provider service is used when the fulfillment provider is added to the database.
The value of this property is also used to reference the fulfillment provider throughout Medusa. For example, it is used to [add a fulfillment provider](https://docs.medusajs.com/api/admin#regions\_postregionsregionfulfillmentproviders) to a region.
@@ -51,8 +53,15 @@ Additionally, if you’re creating your fulfillment provider as an external plug
```ts
class MyFulfillmentService extends AbstractFulfillmentService {
@@ -6,17 +6,139 @@ import ParameterTypes from "@site/src/components/ParameterTypes"
# AbstractPaymentProcessor
Payment processor in charge of creating , managing and processing a payment
## Overview
A Payment Processor is the payment method used to authorize, capture, and refund payment, among other actions. An example of a Payment Processor is Stripe.
By default, Medusa has a `manual` payment provider that has minimal implementation. It can be synonymous with a Cash on Delivery payment method. It allows
store operators to manage the payment themselves but still keep track of its different stages on Medusa.
A payment processor is a service that extends the `AbstractPaymentProcessor` and implements its methods. So, adding a Payment Processor is as simple as
creating a service file in `src/services`. The file's name is the payment processor's class name as a slug and without the word `Service`.
For example, if you're creating a `MyPaymentService` class, the file name is `src/services/my-payment.ts`.
```ts
import { AbstractPaymentProcessor } from "@medusajs/medusa";
class MyPaymentService extends AbstractPaymentProcessor {
// methods here...
}
export default MyPaymentService
```
The methods of the payment processor are used at different points in the Checkout flow as well as when processing an order after it’s placed.
The `PaymentProvider` entity has 2 properties: `id` and `is_installed`. The `identifier` property in the payment processor service is used when the payment processor is added to the database.
The value of this property is also used to reference the payment processor throughout Medusa.
For example, it is used to [add a payment processor](https://docs.medusajs.com/api/admin#regions\_postregionsregionpaymentproviders) to a region.
```ts
class MyPaymentService extends AbstractPaymentProcessor {
static identifier = "my-payment"
// ...
}
```
---
## PaymentProcessorError
Before diving into the methods of the Payment Processor, you'll notice that part of the expected return signature of these method includes `PaymentProcessorError`.
```ts
interface PaymentProcessorError {
error: string
code?: string
detail?: any
}
```
While implementing the Payment Processor's methods, if you need to inform the Medusa core that an error occurred at a certain stage,
return an object having the attributes defined in the `PaymentProcessorError` interface.
For example, the Stripe payment processor has the following method to create the error object, which is used within other methods:
```ts
abstract class StripeBase extends AbstractPaymentProcessor {
You can use the `constructor` of your Payment Processor to have access to different services in Medusa through [dependency injection](https://docs.medusajs.com/development/fundamentals/dependency-injection).
You can also use the constructor to initialize your integration with the third-party provider. For example, if you use a client to connect to the third-party provider’s APIs,
you can initialize it in the constructor and use it in other methods in the service.
Additionally, if you’re creating your Payment Processor as an external plugin to be installed on any Medusa backend and you want to access the options added for the plugin,
you can access it in the constructor. The options are passed as a second parameter.
### Example
```ts
class MyPaymentService extends AbstractPaymentProcessor {
"description": "An instance of `MedusaContainer` that allows you to access other resources, such as services, in your Medusa backend through [dependency injection](https://docs.medusajs.com/development/fundamentals/dependency-injection)",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -44,7 +166,7 @@ Payment processor in charge of creating , managing and processing a payment
{
"name": "config",
"type": "`Record<string, unknown>`",
"description": "",
"description": "If this fulfillment provider is created in a plugin, the plugin's options are passed in this parameter.",
"optional": true,
"defaultValue": "",
"expandable": false,
@@ -60,7 +182,7 @@ ___
{
"name": "config",
"type": "`Record<string, unknown>`",
"description": "",
"description": "If this fulfillment provider is created in a plugin, the plugin's options are passed in this parameter.",
"description": "An instance of `MedusaContainer` that allows you to access other resources, such as services, in your Medusa backend through [dependency injection](https://docs.medusajs.com/development/fundamentals/dependency-injection)",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -97,7 +219,7 @@ ___
{
"name": "identifier",
"type": "`string`",
"description": "",
"description": "The `PaymentProvider` entity has 2 properties: `id` and `is_installed`. The `identifier` property in the payment processor service is used when the payment processor is added to the database.\n\nThe value of this property is also used to reference the payment processor throughout Medusa.\nFor example, it is used to [add a payment processor](https://docs.medusajs.com/api/admin#regions\\_postregionsregionpaymentproviders) to a region.\n\n```ts\nclass MyPaymentService extends AbstractPaymentProcessor {\n static identifier = \"my-payment\"\n // ...\n}\n```",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -111,7 +233,64 @@ ___
### authorizePayment
Authorize an existing session if it is not already authorized
This method is used to authorize payment using the Payment Session of an order. This is called when the
[cart is completed](https://docs.medusajs.com/api/store#carts\_postcartscartcomplete) and before the order is created.
This method is also used for authorizing payments of a swap of an order and when authorizing sessions in a payment collection.
You can interact with a third-party provider and perform any actions necessary to authorize the payment.
The payment authorization might require additional action from the customer before it is declared authorized. Once that additional action is performed,
the `authorizePayment` method will be called again to validate that the payment is now fully authorized. So, make sure to implement it for this case as well, if necessary.
Once the payment is authorized successfully and the Payment Session status is set to `authorized`, the associated order or swap can then be placed or created.
If the payment authorization fails, then an error will be thrown and the order will not be created.
:::note
The payment authorization status is determined using the [getPaymentStatus](medusa.AbstractPaymentProcessor.mdx#getpaymentstatus) method. If the status is `requires_more`, then it means additional actions are required
from the customer. If you try to create the order with a status that isn't `authorized`, the process will fail.
:::
#### Example
```ts
import {
PaymentProcessorError,
PaymentSessionStatus,
// ...
} from "@medusajs/medusa"
// ...
class MyPaymentService extends AbstractPaymentProcessor {
@@ -119,7 +298,7 @@ Authorize an existing session if it is not already authorized
{
"name": "paymentSessionData",
"type": "`Record<string, unknown>`",
"description": "",
"description": "The `data` field of the payment session.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -128,7 +307,7 @@ Authorize an existing session if it is not already authorized
{
"name": "context",
"type": "`Record<string, unknown>`",
"description": "",
"description": "The context of the authorization. It may include some of the following fields:\n\n- `ip`: The customer’s IP.\n- `idempotency_key`: The [Idempotency Key](https://docs.medusajs.com/modules/carts-and-checkout/payment#idempotency-key) that is associated with the current cart. It is useful when retrying payments, retrying checkout at a failed point, or for payments that require additional actions from the customer.\n- `cart_id`: The ID of a cart. This is only during operations like placing an order or creating a swap.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -144,7 +323,7 @@ Authorize an existing session if it is not already authorized
"description": "The authorization details or an error object.",
"expandable": false,
"children": [
{
@@ -164,7 +343,41 @@ ___
### cancelPayment
Cancel an existing session
This method is used to cancel an order’s payment. This method is typically triggered by one of the following situations:
1. Before an order is placed and after the payment is authorized, an inventory check is done on products to ensure that products are still available for purchase. If the inventory check fails for any of the products, the payment is canceled.
2. If the store operator cancels the order from the admin.
3. When the payment of an order's swap is canceled.
You can utilize this method to interact with the third-party provider and perform any actions necessary to cancel the payment.
#### Example
```ts
import {
PaymentProcessorError,
// ...
} from "@medusajs/medusa"
// ...
class MyPaymentService extends AbstractPaymentProcessor {
"description": "Either an error object or a value that's stored in the `data` field of the Payment.",
"expandable": false,
"children": [
{
@@ -208,7 +421,39 @@ ___
### capturePayment
Capture an existing session
This method is used to capture the payment amount of an order. This is typically triggered manually by the store operator from the admin.
This method is also used for capturing payments of a swap of an order, or when a request is sent to the [Capture Payment API Route](https://docs.medusajs.com/api/admin#payments\_postpaymentspaymentcapture).
You can utilize this method to interact with the third-party provider and perform any actions necessary to capture the payment.
#### Example
```ts
import {
PaymentProcessorError,
// ...
} from "@medusajs/medusa"
// ...
class MyPaymentService extends AbstractPaymentProcessor {
"description": "Either an error object or a value that's stored in the `data` field of the Payment.",
"expandable": false,
"children": [
{
@@ -252,7 +497,36 @@ ___
### deletePayment
Delete an existing session
This method is used to perform any actions necessary before a Payment Session is deleted. The Payment Session is deleted in one of the following cases:
1. When a request is sent to [delete the Payment Session](https://docs.medusajs.com/api/store#carts\_deletecartscartpaymentsessionssession).
2. When the [Payment Session is refreshed](https://docs.medusajs.com/api/store#carts\_postcartscartpaymentsessionssession). The Payment Session is deleted so that a newer one is initialized instead.
3. When the Payment Processor is no longer available. This generally happens when the store operator removes it from the available Payment Processor in the admin.
4. When the region of the store is changed based on the cart information and the Payment Processor is not available in the new region.
#### Example
```ts
import {
PaymentProcessorError,
// ...
} from "@medusajs/medusa"
// ...
class MyPaymentService extends AbstractPaymentProcessor {
"description": "Either an error object or an empty object.",
"expandable": false,
"children": [
{
@@ -294,29 +568,33 @@ Delete an existing session
___
### getIdentifier
Return a unique identifier to retrieve the payment plugin provider
#### Returns
<ParameterTypes parameters={[
{
"name": "string",
"type": "`string`",
"optional": true,
"defaultValue": "",
"description": "",
"expandable": false,
"children": []
}
]} />
___
### getPaymentStatus
Return the status of the session
This method is used to get the status of a Payment or a Payment Session. Its main usage is within the place order and create swap flows.
If the status returned is not `authorized` within these flows, then the payment is considered failed and an error will be thrown, stopping the flow from completion.
#### Example
```ts
import {
PaymentSessionStatus
// ...
} from "@medusajs/medusa"
// ...
class MyPaymentService extends AbstractPaymentProcessor {
// ...
async getPaymentStatus(
paymentSessionData: Record<string, unknown>
): Promise<PaymentSessionStatus> {
const paymentId = paymentSessionData.id
// assuming client is an initialized client
// communicating with a third-party service.
return await this.client.getStatus(paymentId) as PaymentSessionStatus
}
}
```
#### Parameters
@@ -324,7 +602,7 @@ Return the status of the session
{
"name": "paymentSessionData",
"type": "`Record<string, unknown>`",
"description": "",
"description": "The `data` field of a Payment as a parameter. You can use this data to interact with the third-party provider to check the status of the payment if necessary.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -340,7 +618,7 @@ Return the status of the session
"description": "The status of the Payment or Payment Session.",
"expandable": false,
"children": [
{
@@ -360,15 +638,47 @@ ___
### initiatePayment
Initiate a payment session with the external provider
This method is called either if a region has only one payment provider enabled or when [a Payment Session is selected](https://docs.medusajs.com/api/store#carts\_postcartscartpaymentsession),
which occurs when the customer selects their preferred payment method during checkout.
It is used to allow you to make any necessary calls to the third-party provider to initialize the payment. For example, in Stripe this method is used to create a Payment Intent for the customer.
#### Example
```ts
import {
PaymentContext,
PaymentSessionResponse,
// ...
} from "@medusajs/medusa"
class MyPaymentService extends AbstractPaymentProcessor {
"description": "A customer can make purchases in your store and manage their profile.",
"description": "The customer associated with this payment.",
"optional": true,
"defaultValue": "",
"expandable": false,
@@ -421,7 +731,7 @@ Initiate a payment session with the external provider
{
"name": "email",
"type": "`string`",
"description": "",
"description": "The customer's email.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -430,7 +740,7 @@ Initiate a payment session with the external provider
{
"name": "paymentSessionData",
"type": "`Record<string, unknown>`",
"description": "",
"description": "If the payment session hasn't been created or initiated yet, it'll be an empty object.\nIf the payment session exists, it'll be the value of the payment session's `data` field.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -439,7 +749,7 @@ Initiate a payment session with the external provider
{
"name": "resource_id",
"type": "`string`",
"description": "",
"description": "The ID of the resource the payment is associated with. For example, the cart's ID.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -454,15 +764,15 @@ Initiate a payment session with the external provider
This method is used to refund an order’s payment. This is typically triggered manually by the store operator from the admin. The refund amount might be the total order amount or part of it.
This method is also used for refunding payments of a swap or a claim of an order, or when a request is sent to the [Refund Payment API Route](https://docs.medusajs.com/api/admin#payments\_postpaymentspaymentrefunds).
You can utilize this method to interact with the third-party provider and perform any actions necessary to refund the payment.
#### Example
```ts
import {
PaymentProcessorError,
// ...
} from "@medusajs/medusa"
// ...
class MyPaymentService extends AbstractPaymentProcessor {
"description": "The `data` field of a Payment Session. Make sure to store in the `data` field any necessary data that would allow you to retrieve the payment data from the third-party provider.",
"description": "The payment's data, typically retrieved from a third-party provider.",
"expandable": false,
"children": [
{
@@ -574,15 +941,50 @@ ___
### updatePayment
Update an existing payment session
This method is used to update the payment session when the payment amount changes. It's called whenever the cart or any of its related data is updated.
For example, when a [line item is added to the cart](https://docs.medusajs.com/api/store#carts\_postcartscartlineitems) or when a
[shipping method is selected](https://docs.medusajs.com/api/store#carts\_postcartscartshippingmethod).
#### Example
```ts
import {
PaymentProcessorContext,
PaymentProcessorError,
PaymentProcessorSessionResponse,
// ...
} from "@medusajs/medusa"
// ...
class MyPaymentService extends AbstractPaymentProcessor {
"description": "A customer can make purchases in your store and manage their profile.",
"description": "The customer associated with this payment.",
"optional": true,
"defaultValue": "",
"expandable": false,
@@ -635,7 +1037,7 @@ Update an existing payment session
{
"name": "email",
"type": "`string`",
"description": "",
"description": "The customer's email.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -644,7 +1046,7 @@ Update an existing payment session
{
"name": "paymentSessionData",
"type": "`Record<string, unknown>`",
"description": "",
"description": "If the payment session hasn't been created or initiated yet, it'll be an empty object.\nIf the payment session exists, it'll be the value of the payment session's `data` field.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -653,7 +1055,7 @@ Update an existing payment session
{
"name": "resource_id",
"type": "`string`",
"description": "",
"description": "The ID of the resource the payment is associated with. For example, the cart's ID.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -668,15 +1070,15 @@ Update an existing payment session
This method is used to update the `data` field of a payment session. It's called when a request is sent to the
[Update Payment Session API Route](https://docs.medusajs.com/api/store#carts\_postcartscartpaymentsessionupdate), or when the `CartService`'s `updatePaymentSession` is used.
This method can also be used to update the data in the third-party payment provider, if necessary.
#### Example
```ts
import {
PaymentProcessorError,
PaymentProviderService,
// ...
} from "@medusajs/medusa"
// ...
class MyPaymentService extends AbstractPaymentProcessor {
"description": "the data to store in the `data` field of the payment session.\nYou can keep the data as-is, or make changes to it by communicating with the third-party provider.",
"description": "The type of the Shipping Option price. `flat\\_rate` indicates fixed pricing, whereas `calculated` indicates that the price will be calculated each time by the fulfillment provider.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
"children": [
{
"name": "CALCULATED",
"type": "`\"calculated\"`",
"description": "The shipping option's price is calculated. In this case, the `amount` field is typically `null`.",
"optional": true,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "FLAT_RATE",
"type": "`\"flat_rate\"`",
"description": "The shipping option's price is a flat rate.",
"optional": true,
"defaultValue": "",
"expandable": false,
"children": []
}
]
},
{
"name": "profile_id",
@@ -128,7 +147,7 @@ import ParameterTypes from "@site/src/components/ParameterTypes"
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.