Skip to main content
POST
/
store
/
payment-collections
JS SDK
import Medusa from "@medusajs/js-sdk"

let MEDUSA_BACKEND_URL = "http://localhost:9000"

if (process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL) {
  MEDUSA_BACKEND_URL = process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL
}

export const sdk = new Medusa({
  baseUrl: MEDUSA_BACKEND_URL,
  debug: process.env.NODE_ENV === "development",
  publishableKey: process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY,
})

sdk.store.payment.initiatePaymentSession(
  cart, // assuming you already have the cart object.
  {
    provider_id: "pp_stripe_stripe",
    data: {
      // any data relevant for the provider.
    }
  }
)
.then(({ payment_collection }) => {
  console.log(payment_collection)
})
{
  "payment_collection": {
    "id": "<string>",
    "currency_code": "<string>",
    "amount": 123,
    "status": "canceled",
    "payment_providers": [
      {
        "id": "<string>"
      }
    ],
    "authorized_amount": 123,
    "captured_amount": 123,
    "refunded_amount": 123,
    "completed_at": "2023-11-07T05:31:56Z",
    "created_at": "2023-11-07T05:31:56Z",
    "updated_at": "2023-11-07T05:31:56Z",
    "metadata": {},
    "payment_sessions": [
      {
        "id": "<string>",
        "amount": 123,
        "currency_code": "usd",
        "provider_id": "<string>",
        "data": {},
        "status": "authorized",
        "context": {
          "customer": {
            "id": "cus_123"
          }
        },
        "authorized_at": "2023-11-07T05:31:56Z",
        "payment_collection": {},
        "payment": {
          "id": "<string>",
          "amount": 123,
          "currency_code": "<string>",
          "provider_id": "<string>",
          "authorized_amount": 123,
          "data": {},
          "created_at": "2023-11-07T05:31:56Z",
          "updated_at": "2023-11-07T05:31:56Z",
          "captured_at": "2023-11-07T05:31:56Z",
          "canceled_at": "2023-11-07T05:31:56Z",
          "captured_amount": 123,
          "refunded_amount": 123,
          "captures": [
            {
              "id": "<string>",
              "amount": 123,
              "created_at": "2023-11-07T05:31:56Z",
              "payment": {},
              "created_by": "<string>"
            }
          ],
          "refunds": [
            {
              "id": "<string>",
              "amount": 123,
              "created_at": "2023-11-07T05:31:56Z",
              "payment": {},
              "refund_reason_id": "<string>",
              "note": "<string>",
              "created_by": "<string>",
              "refund_reason": {
                "id": "<string>",
                "label": "<string>",
                "metadata": {},
                "created_at": "2023-11-07T05:31:56Z",
                "updated_at": "2023-11-07T05:31:56Z",
                "description": "<string>"
              }
            }
          ],
          "payment_collection": {},
          "payment_session": {}
        }
      }
    ],
    "payments": [
      {
        "id": "<string>",
        "amount": 123,
        "currency_code": "<string>",
        "provider_id": "<string>",
        "authorized_amount": 123,
        "data": {},
        "created_at": "2023-11-07T05:31:56Z",
        "updated_at": "2023-11-07T05:31:56Z",
        "captured_at": "2023-11-07T05:31:56Z",
        "canceled_at": "2023-11-07T05:31:56Z",
        "captured_amount": 123,
        "refunded_amount": 123,
        "captures": [
          {
            "id": "<string>",
            "amount": 123,
            "created_at": "2023-11-07T05:31:56Z",
            "payment": {},
            "created_by": "<string>"
          }
        ],
        "refunds": [
          {
            "id": "<string>",
            "amount": 123,
            "created_at": "2023-11-07T05:31:56Z",
            "payment": {},
            "refund_reason_id": "<string>",
            "note": "<string>",
            "created_by": "<string>",
            "refund_reason": {
              "id": "<string>",
              "label": "<string>",
              "metadata": {},
              "created_at": "2023-11-07T05:31:56Z",
              "updated_at": "2023-11-07T05:31:56Z",
              "description": "<string>"
            }
          }
        ],
        "payment_collection": {},
        "payment_session": {}
      }
    ]
  }
}

Headers

x-publishable-api-key
string
required

Publishable API Key created in the Medusa Admin.

Query Parameters

fields
string

Comma-separated fields that should be included in the returned data. if a field is prefixed with + it will be added to the default fields, using - will remove it from the default fields. without prefix it will replace the entire default fields.

Body

application/json

The details of the payment collection to create.

cart_id
string
required

The ID of the cart this payment collection is for.

Response

OK

The payment collection's details.

payment_collection
object
required

The payment collection's details.