docs: add TSDoc for payment processor + generate docs (#5917)

* added tsdocs for payment processor

* generated reference for payment processor
This commit is contained in:
Shahed Nasser
2023-12-18 14:02:18 +02:00
committed by GitHub
parent e63f4e6c7a
commit ddc6cc13a0
73 changed files with 34162 additions and 17470 deletions

View File

@@ -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 youre creating your fulfillment provider as an external plug
```ts
class MyFulfillmentService extends AbstractFulfillmentService {
static identifier = "my-fulfillment"
// ...
constructor(container, options) {
super(container)
// you can access options here
// you can also initialize a client that
// communicates with a third-party service.
this.client = new Client(options)
}
// ...
}
```

View File

@@ -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 its placed.
![Checkout Flow - Payment](https://res.cloudinary.com/dza7lstvk/image/upload/v1680177820/Medusa%20Docs/Diagrams/checkout-payment\_cy9efp.jpg)
---
## Identifier Property
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 {
// ...
protected buildError(
message: string,
e: Stripe.StripeRawError | PaymentProcessorError | Error
): PaymentProcessorError {
return {
error: message,
code: "code" in e ? e.code : "",
detail: isPaymentProcessorError(e)
? `${e.error}${EOL}${e.detail ?? ""}`
: "detail" in e
? e.detail
: e.message ?? "",
}
}
// used in other methods
async retrievePayment(
paymentSessionData: Record<string, unknown>
): Promise<
PaymentProcessorError |
PaymentProcessorSessionResponse["session_data"]
> {
try {
// ...
} catch (e) {
return this.buildError(
"An error occurred in retrievePayment",
e
)
}
}
}
```
---
## constructor
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 providers APIs,
you can initialize it in the constructor and use it in other methods in the service.
Additionally, if youre 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 {
// ...
constructor(container, options) {
super(container)
// you can access options here
// you can also initialize a client that
// communicates with a third-party service.
this.client = new Client(options)
}
// ...
}
```
### Parameters
<ParameterTypes parameters={[
{
"name": "container",
"type": "[MedusaContainer](../types/medusa.MedusaContainer-2.mdx)",
"description": "",
"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.",
"optional": true,
"defaultValue": "",
"expandable": false,
@@ -69,7 +191,7 @@ ___
{
"name": "container",
"type": "[MedusaContainer](../types/medusa.MedusaContainer-2.mdx)",
"description": "",
"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 {
// ...
async authorizePayment(
paymentSessionData: Record<string, unknown>,
context: Record<string, unknown>
): Promise<
PaymentProcessorError |
{
status: PaymentSessionStatus;
data: Record<string, unknown>;
}
> {
try {
await this.client.authorize(paymentSessionData.id)
return {
status: PaymentSessionStatus.AUTHORIZED,
data: {
id: paymentSessionData.id
}
}
} catch (e) {
return {
error: e.message
}
}
}
}
```
#### Parameters
@@ -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 customers 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
"type": "Promise&#60;[PaymentProcessorError](../interfaces/medusa.PaymentProcessorError.mdx) \\| object&#62;",
"optional": false,
"defaultValue": "",
"description": "",
"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 orders 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 {
// ...
async cancelPayment(
paymentSessionData: Record<string, unknown>
): Promise<Record<string, unknown> | PaymentProcessorError> {
const paymentId = paymentSessionData.id
// assuming client is an initialized client
// communicating with a third-party service.
const cancelData = this.client.cancel(paymentId)
return {
id: paymentId,
...cancelData
}
}
}
```
#### Parameters
@@ -172,7 +385,7 @@ Cancel an existing session
{
"name": "paymentSessionData",
"type": "`Record<string, unknown>`",
"description": "",
"description": "The `data` field of the Payment.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -188,7 +401,7 @@ Cancel an existing session
"type": "Promise&#60;Record&#60;string, unknown&#62; \\| [PaymentProcessorError](../interfaces/medusa.PaymentProcessorError.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "",
"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 {
// ...
async capturePayment(
paymentSessionData: Record<string, unknown>
): Promise<Record<string, unknown> | PaymentProcessorError> {
const paymentId = paymentSessionData.id
// assuming client is an initialized client
// communicating with a third-party service.
const captureData = this.client.catch(paymentId)
return {
id: paymentId,
...captureData
}
}
}
```
#### Parameters
@@ -216,7 +461,7 @@ Capture an existing session
{
"name": "paymentSessionData",
"type": "`Record<string, unknown>`",
"description": "",
"description": "The `data` field of the Payment for its first parameter.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -232,7 +477,7 @@ Capture an existing session
"type": "Promise&#60;Record&#60;string, unknown&#62; \\| [PaymentProcessorError](../interfaces/medusa.PaymentProcessorError.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "",
"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 {
// ...
async deletePayment(
paymentSessionData: Record<string, unknown>
): Promise<Record<string, unknown> | PaymentProcessorError> {
const paymentId = paymentSessionData.id
// assuming client is an initialized client
// communicating with a third-party service.
this.client.delete(paymentId)
return {}
}
}
```
#### Parameters
@@ -260,7 +534,7 @@ Delete an existing session
{
"name": "paymentSessionData",
"type": "`Record<string, unknown>`",
"description": "",
"description": "The `data` field of the Payment Session.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -276,7 +550,7 @@ Delete an existing session
"type": "Promise&#60;Record&#60;string, unknown&#62; \\| [PaymentProcessorError](../interfaces/medusa.PaymentProcessorError.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "",
"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
"type": "Promise&#60;[PaymentSessionStatus](../../entities/enums/entities.PaymentSessionStatus.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "",
"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 {
// ...
async initiatePayment(
context: PaymentProcessorContext
): Promise<
PaymentProcessorError | PaymentProcessorSessionResponse
> {
// assuming client is an initialized client
// communicating with a third-party service.
const clientPayment = await this.client.initiate(context)
return {
session_data: {
id: clientPayment.id
},
}
}
}
```
#### Parameters
<ParameterTypes parameters={[
{
"name": "context",
"type": "[PaymentProcessorContext](../types/medusa.PaymentProcessorContext.mdx)",
"description": "",
"type": "[PaymentProcessorContext](../interfaces/medusa.PaymentProcessorContext.mdx)",
"description": "The context of the payment.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -376,7 +686,7 @@ Initiate a payment session with the external provider
{
"name": "amount",
"type": "`number`",
"description": "",
"description": "The payment's amount.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -384,8 +694,8 @@ Initiate a payment session with the external provider
},
{
"name": "billing_address",
"type": "[Address](../../entities/classes/entities.Address.mdx) \\| `null`",
"description": "",
"type": "`null` \\| [Address](../../entities/classes/entities.Address.mdx)",
"description": "The payment's billing address.",
"optional": true,
"defaultValue": "",
"expandable": false,
@@ -394,7 +704,7 @@ Initiate a payment session with the external provider
{
"name": "context",
"type": "`Record<string, unknown>`",
"description": "",
"description": "The cart's context.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -403,7 +713,7 @@ Initiate a payment session with the external provider
{
"name": "currency_code",
"type": "`string`",
"description": "",
"description": "The selected currency code, typically associated with the customer's cart.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -412,7 +722,7 @@ Initiate a payment session with the external provider
{
"name": "customer",
"type": "[Customer](../../entities/classes/entities.Customer.mdx)",
"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
<ParameterTypes parameters={[
{
"name": "Promise",
"type": "Promise&#60;[PaymentProcessorError](../interfaces/medusa.PaymentProcessorError.mdx) \\| [PaymentProcessorSessionResponse](../types/medusa.PaymentProcessorSessionResponse.mdx)&#62;",
"type": "Promise&#60;[PaymentProcessorError](../interfaces/medusa.PaymentProcessorError.mdx) \\| [PaymentProcessorSessionResponse](../interfaces/medusa.PaymentProcessorSessionResponse.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "",
"description": "Either the payment's data or an error object.",
"expandable": false,
"children": [
{
"name": "PaymentProcessorError \\| PaymentProcessorSessionResponse",
"type": "[PaymentProcessorError](../interfaces/medusa.PaymentProcessorError.mdx) \\| [PaymentProcessorSessionResponse](../types/medusa.PaymentProcessorSessionResponse.mdx)",
"type": "[PaymentProcessorError](../interfaces/medusa.PaymentProcessorError.mdx) \\| [PaymentProcessorSessionResponse](../interfaces/medusa.PaymentProcessorSessionResponse.mdx)",
"optional": true,
"defaultValue": "",
"description": "",
@@ -477,7 +787,40 @@ ___
### refundPayment
Refund an existing session
This method is used to refund an orders 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 {
// ...
async refundPayment(
paymentSessionData: Record<string, unknown>,
refundAmount: number
): Promise<Record<string, unknown> | PaymentProcessorError> {
const paymentId = paymentSessionData.id
// assuming client is an initialized client
// communicating with a third-party service.
const refundData = this.client.refund(paymentId, refundAmount)
return {
id: paymentId,
...refundData
}
}
}
```
#### Parameters
@@ -485,7 +828,7 @@ Refund an existing session
{
"name": "paymentSessionData",
"type": "`Record<string, unknown>`",
"description": "",
"description": "The `data` field of a Payment.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -494,7 +837,7 @@ Refund an existing session
{
"name": "refundAmount",
"type": "`number`",
"description": "",
"description": "the amount to refund.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -510,7 +853,7 @@ Refund an existing session
"type": "Promise&#60;Record&#60;string, unknown&#62; \\| [PaymentProcessorError](../interfaces/medusa.PaymentProcessorError.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "",
"description": "Either an error object or a value that's stored in the `data` field of the Payment.",
"expandable": false,
"children": [
{
@@ -530,7 +873,31 @@ ___
### retrievePayment
Retrieve an existing session
This method is used to provide a uniform way of retrieving the payment information from the third-party provider.
For example, in Stripes Payment Processor this method is used to retrieve the payment intent details from Stripe.
#### Example
```ts
import {
PaymentProcessorError
// ...
} from "@medusajs/medusa"
// ...
class MyPaymentService extends AbstractPaymentProcessor {
// ...
async retrievePayment(
paymentSessionData: Record<string, unknown>
): Promise<Record<string, unknown> | PaymentProcessorError> {
const paymentId = paymentSessionData.id
// assuming client is an initialized client
// communicating with a third-party service.
return await this.client.retrieve(paymentId)
}
}
```
#### Parameters
@@ -538,7 +905,7 @@ Retrieve an existing session
{
"name": "paymentSessionData",
"type": "`Record<string, unknown>`",
"description": "",
"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.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -554,7 +921,7 @@ Retrieve an existing session
"type": "Promise&#60;Record&#60;string, unknown&#62; \\| [PaymentProcessorError](../interfaces/medusa.PaymentProcessorError.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "",
"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 {
// ...
async updatePayment(
context: PaymentProcessorContext
): Promise<
void |
PaymentProcessorError |
PaymentProcessorSessionResponse
> {
// assuming client is an initialized client
// communicating with a third-party service.
const paymentId = context.paymentSessionData.id
await this.client.update(paymentId, context)
return {
session_data: context.paymentSessionData
}
}
}
```
#### Parameters
<ParameterTypes parameters={[
{
"name": "context",
"type": "[PaymentProcessorContext](../types/medusa.PaymentProcessorContext.mdx)",
"description": "",
"type": "[PaymentProcessorContext](../interfaces/medusa.PaymentProcessorContext.mdx)",
"description": "The context of the payment.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -590,7 +992,7 @@ Update an existing payment session
{
"name": "amount",
"type": "`number`",
"description": "",
"description": "The payment's amount.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -598,8 +1000,8 @@ Update an existing payment session
},
{
"name": "billing_address",
"type": "[Address](../../entities/classes/entities.Address.mdx) \\| `null`",
"description": "",
"type": "`null` \\| [Address](../../entities/classes/entities.Address.mdx)",
"description": "The payment's billing address.",
"optional": true,
"defaultValue": "",
"expandable": false,
@@ -608,7 +1010,7 @@ Update an existing payment session
{
"name": "context",
"type": "`Record<string, unknown>`",
"description": "",
"description": "The cart's context.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -617,7 +1019,7 @@ Update an existing payment session
{
"name": "currency_code",
"type": "`string`",
"description": "",
"description": "The selected currency code, typically associated with the customer's cart.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -626,7 +1028,7 @@ Update an existing payment session
{
"name": "customer",
"type": "[Customer](../../entities/classes/entities.Customer.mdx)",
"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
<ParameterTypes parameters={[
{
"name": "Promise",
"type": "Promise&#60;void \\| [PaymentProcessorError](../interfaces/medusa.PaymentProcessorError.mdx) \\| [PaymentProcessorSessionResponse](../types/medusa.PaymentProcessorSessionResponse.mdx)&#62;",
"type": "Promise&#60;void \\| [PaymentProcessorError](../interfaces/medusa.PaymentProcessorError.mdx) \\| [PaymentProcessorSessionResponse](../interfaces/medusa.PaymentProcessorSessionResponse.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "",
"description": "Either the payment's data or an error object.",
"expandable": false,
"children": [
{
"name": "void \\| PaymentProcessorError \\| PaymentProcessorSessionResponse",
"type": "`void` \\| [PaymentProcessorError](../interfaces/medusa.PaymentProcessorError.mdx) \\| [PaymentProcessorSessionResponse](../types/medusa.PaymentProcessorSessionResponse.mdx)",
"type": "`void` \\| [PaymentProcessorError](../interfaces/medusa.PaymentProcessorError.mdx) \\| [PaymentProcessorSessionResponse](../interfaces/medusa.PaymentProcessorSessionResponse.mdx)",
"optional": true,
"defaultValue": "",
"description": "",
@@ -691,7 +1093,48 @@ ___
### updatePaymentData
Update the session data for a 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 {
protected paymentProviderService: PaymentProviderService
// ...
constructor(container, options) {
super(container)
this.paymentProviderService = container.paymentProviderService
// ...
}
// ...
async updatePaymentData(
sessionId: string,
data: Record<string, unknown>
): Promise<
Record<string, unknown> |
PaymentProcessorError
> {
const paymentSession = await this.paymentProviderService.retrieveSession(sessionId)
// assuming client is an initialized client
// communicating with a third-party service.
const clientPayment = await this.client.update(paymentSession.data.id, data)
return {
id: clientPayment.id
}
}
}
```
#### Parameters
@@ -699,7 +1142,7 @@ Update the session data for a payment session
{
"name": "sessionId",
"type": "`string`",
"description": "",
"description": "The ID of the payment session.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -708,7 +1151,7 @@ Update the session data for a payment session
{
"name": "data",
"type": "`Record<string, unknown>`",
"description": "",
"description": "The data to be updated in the payment session.",
"optional": false,
"defaultValue": "",
"expandable": false,
@@ -724,7 +1167,7 @@ Update the session data for a payment session
"type": "Promise&#60;Record&#60;string, unknown&#62; \\| [PaymentProcessorError](../interfaces/medusa.PaymentProcessorError.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "",
"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.",
"expandable": false,
"children": [
{

View File

@@ -1,32 +0,0 @@
---
displayed_sidebar: homepage
---
import ParameterTypes from "@site/src/components/ParameterTypes"
# AdminGetRegionsPaginationParams
Parameters that can be used to configure how a list of data is paginated.
## Properties
<ParameterTypes parameters={[
{
"name": "limit",
"type": "`number`",
"description": "Limit the number of items returned in the list.",
"optional": true,
"defaultValue": "50",
"expandable": false,
"children": []
},
{
"name": "offset",
"type": "`number`",
"description": "The number of items to skip when retrieving a list.",
"optional": true,
"defaultValue": "0",
"expandable": false,
"children": []
}
]} expandUrl="https://docs.medusajs.com/development/entities/repositories#retrieving-a-list-of-records"/>

View File

@@ -103,12 +103,30 @@ Parameters used to filter and configure the pagination of the retrieved regions.
}
]
},
{
"name": "expand",
"type": "`string`",
"description": "Comma-separated relations that should be expanded in the returned data.",
"optional": true,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "fields",
"type": "`string`",
"description": "Comma-separated fields that should be included in the returned data.",
"optional": true,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "limit",
"type": "`number`",
"description": "Limit the number of items returned in the list.",
"optional": true,
"defaultValue": "50",
"defaultValue": "20",
"expandable": false,
"children": []
},

View File

@@ -55,6 +55,34 @@ import ParameterTypes from "@site/src/components/ParameterTypes"
"expandable": false,
"children": []
},
{
"name": "price_type",
"type": "[ShippingOptionPriceType](../../entities/enums/entities.ShippingOptionPriceType.mdx)",
"description": "",
"optional": true,
"defaultValue": "",
"expandable": false,
"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": "requirements",
"type": "[OptionRequirement](medusa.OptionRequirement-1.mdx)[]",

View File

@@ -29,7 +29,7 @@ import ParameterTypes from "@site/src/components/ParameterTypes"
},
{
"name": "data",
"type": "`object`",
"type": "`Record<string, unknown>`",
"description": "The data needed for the Fulfillment Provider to handle shipping with this Shipping Option.",
"optional": false,
"defaultValue": "",
@@ -75,12 +75,31 @@ import ParameterTypes from "@site/src/components/ParameterTypes"
},
{
"name": "price_type",
"type": "`string`",
"type": "[ShippingOptionPriceType](../../entities/enums/entities.ShippingOptionPriceType.mdx)",
"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"
},
{
"name": "type",
"type": "`string`",
"type": "[RequirementType](../../entities/enums/entities.RequirementType.mdx)",
"description": "The type of the requirement",
"optional": false,
"defaultValue": "",

View File

@@ -24,11 +24,30 @@ ___
},
{
"name": "type",
"type": "`string`",
"type": "[RequirementType](../../entities/enums/entities.RequirementType.mdx)",
"description": "The type of the requirement",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
"children": [
{
"name": "MAX_SUBTOTAL",
"type": "`\"max_subtotal\"`",
"description": "The shipping option can only be applied if the subtotal is less than the requirement's amont.",
"optional": true,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "MIN_SUBTOTAL",
"type": "`\"min_subtotal\"`",
"description": "The shipping option can only be applied if the subtotal is greater than the requirement's amount.",
"optional": true,
"defaultValue": "",
"expandable": false,
"children": []
}
]
}
]} expandUrl="https://docs.medusajs.com/development/entities/repositories#retrieving-a-list-of-records"/>

View File

@@ -55,19 +55,37 @@ import ParameterTypes from "@site/src/components/ParameterTypes"
}
]
},
{
"name": "expand",
"type": "`string`",
"description": "Comma-separated relations that should be expanded in the returned data.",
"optional": true,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "fields",
"type": "`string`",
"description": "Comma-separated fields that should be included in the returned data.",
"optional": true,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "limit",
"type": "`number`",
"description": "",
"description": "Limit the number of items returned in the list.",
"optional": true,
"defaultValue": "100",
"defaultValue": "20",
"expandable": false,
"children": []
},
{
"name": "offset",
"type": "`number`",
"description": "",
"description": "The number of items to skip when retrieving a list.",
"optional": true,
"defaultValue": "0",
"expandable": false,

View File

@@ -0,0 +1,32 @@
---
displayed_sidebar: homepage
---
import ParameterTypes from "@site/src/components/ParameterTypes"
# StoreGetRegionsRegionParams
Parameters that can be used to configure how data is retrieved.
## Properties
<ParameterTypes parameters={[
{
"name": "expand",
"type": "`string`",
"description": "Comma-separated relations that should be expanded in the returned data.",
"optional": true,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "fields",
"type": "`string`",
"description": "Comma-separated fields that should be included in the returned data.",
"optional": true,
"defaultValue": "",
"expandable": false,
"children": []
}
]} expandUrl="https://docs.medusajs.com/development/entities/repositories#retrieving-a-list-of-records"/>