# Medusa

## API Connection

The base URLs for Medusa are listed below according to the environment:

| Environment | Base URL                                |
| ----------- | --------------------------------------- |
| Sandbox     | <https://apis.sandbox.flexshopper.com/> |
| Production  | <https://apis.flexshopper.com/>         |

FlexShopper must issue you an API token. You must provide your API token in the Authorization header with every call to Medusa.

The following guide walks you through the end-to-end journey of the customer through Medusa, and outlines the required steps to complete this journey.

## Customer

The customer must exist in Medusa and have a payment method on file. You'll need a customer authentication token for actions such as applying for a spending limit, creating a lease, and signing the lease.

{% stepper %}
{% step %}

### Check if customer exists

You must first check if your customer already exists in Medusa, which you can do by calling the [verify-user-exists](https://dev.flexshopper.com/medusa/customer/verify-user-exists "mention") endpoint. If the customer exists, their ID will be returned.
{% endstep %}

{% step %}

### Authenticate an existing customer

If your customer aleady exists in Medusa, you can obtain a customer authentication token by calling the [create-authorization-token](https://dev.flexshopper.com/medusa/customer/create-authorization-token "mention") endpoint. The user's ID you'll need for this endpoint is returned from the [verify-user-exists](https://dev.flexshopper.com/medusa/customer/verify-user-exists "mention") endpoint.
{% endstep %}

{% step %}

### Create a customer

If your customer doesn't exist and you need to perform a customer action, then you must create a customer by calling the [create-user](https://dev.flexshopper.com/medusa/customer/create-user "mention") endpoint. If the customer is successfully created, their ID and customer authentication token will be returned.
{% endstep %}

{% step %}

### Collect a payment method

Once a customer is created, you must collect their payment method(s) and provide them to Medusa.

You can provide the payment methods in the payload when applying for a spending limit, or you can interact with the [add-payment-method](https://dev.flexshopper.com/medusa/customer/add-payment-method "mention") endpoint. You may use that endpoint at any point after creating a customer.

{% hint style="warning" %}
Generally, the customer must have at least one payment method in Medusa to complete this flow.
{% endhint %}
{% endstep %}
{% endstepper %}

## Decision & Spending Limit

Once your customer is in Medusa, it's time to ensure they have an active decision, a spending limit, and are not on a spending hold.

An active decision ensures your customer has is allowed to create and sign leases.

The spending limit amount helps you understand what your customer's maximum order value is, as you won't be able to create a lease for them with a total higher than the spending limit.

A spending hold may be applied by FlexShopper to customers for various reasons. If a customer has a spending hold applied, no new leases will be created for them.

{% stepper %}
{% step %}

### Check if decision exists

You can check the decision status and spending limit by calling the [get-user-info](https://dev.flexshopper.com/medusa/customer/get-user-info "mention") endpoint.
{% endstep %}

{% step %}

### Apply for a decision

If no decision is returned, then you must apply for a spending limit for this customer. You can do that by calling the [apply-for-spending-limit](https://dev.flexshopper.com/medusa/decision/apply-for-spending-limit "mention") endpoint.

If your customer is approved to spend, you can proceed with creating a lease. If the customer is denied, then they unfortuantely cannot proceed.
{% endstep %}
{% endstepper %}

## Lease & Fulfillment

Now that your customer is created, and has an active decision and an available spending limit, you can proceed with the lease process.

{% stepper %}
{% step %}

### Create a lease

Firstly, you must create a lease, which you can do by calling the [create-lease](https://dev.flexshopper.com/medusa/lease/create-lease "mention") endpoint.

The response will include the FlexShopper lease document in HTML format. You must render and present this to the customer and enable them to read it in full.
{% endstep %}

{% step %}

### Sign the lease

Once a lease is created and presented to the user, you must give them the option to sign it virtually. Then, when the customer signs the lease on your end, you must subsequently sign the lease through Medusa by calling the [sign-lease](https://dev.flexshopper.com/medusa/lease/sign-lease "mention") endpoint.
{% endstep %}

{% step %}

### Finalize the lease

After the lease is signed, the lease must be finalized (confirmed). This can be done by calling the [finalize-lease](https://dev.flexshopper.com/medusa/lease/finalize-lease "mention") endpoint.

Once a lease is finalized, we begin the fulfillment and billing processes.
{% endstep %}

{% step %}

### Confirm fulfillment

Finally, the fulfillment of the lease must be confirmed. For E-commerce integrations, this step must be done after you've confirmed the order is shipped.\
Confirmation is generally done by calling the [confirm-shipment](https://dev.flexshopper.com/medusa/lease/confirm-shipment "mention") endpoint.
{% endstep %}
{% endstepper %}
