# Refund a card payment through the gateway

```http
PUT https://secure.yoprint.com/v2/api/store/{team_slug}/v2/sales_order/{hash_id}/payment_gateway_refund/{id}
```

- API version: v2
- Operation ID: `v2_public_sales_order_payment_gateway_refund`
- Section: Invoices and payments › [Payments](https://developer.yoprint.com/reference/payments.md)
- Web page: https://developer.yoprint.com/reference/payments/v2-sales-order-payment-gateway-refund/
- Authentication: `Authorization: Bearer YOUR_API_KEY`

Refunds a card payment through the gateway that took it: real money goes back to the card. `{id}` is the card payment's `id`; card payments the customer made in the customer portal can be refunded here too. Send a whole number of thousandths: `12500` is 12.50. See [Money and quantities](/concepts/money-and-quantities/). Always send `amount`: without it the call fails with HTTP 500 before the gateway is called. The answer has no data. `{hash_id}` is the order's `id` (for example `V4keXwwxMQyXw7`); the `ord_` unique key is not accepted here.

## Permission

Any member of the team.

## Business rules

- The payment must be a card payment of this order: "Payment does not belong to the requested sales order" (`refund_failed`). Another kind of record of this order gets "This transaction does not support a provider refund." (`refund_failed`), and a payment from customer credit that came from a Customer Portal payment gets 422 `customer_payment_parent_refund_required` ("Open the original customer payment to review the source allocation and request a refund.").
- The gateway must support refunds: Charge It Pro payments get "Payment Gateway Refund is not supported" (`refund_failed`). A Customer Portal payment can be refunded only when it went through `stripe`, `paypal`, `square_v2` or `authorize_net`: otherwise "This Customer Portal payment does not support a provider refund." (`refund_failed`).
- A payment is frozen while a dispute its provider reported is open or under review, or while a Customer Portal payment has an open review case. Its money can't then be refunded through YoPrint or moved to customer credit: 422 `customer_payment_disputed_funds_unavailable` ("A dispute is open on this payment. Refunds and Issue credit are paused until <provider> decides.") or `invoice_payment_funding_withheld` ("This payment has an open case to review. Refunds and Issue credit are paused until it's settled."), with `error.reason` (`chargeback_open` or `payment_under_review`), `error.provider` and `error.provider_label`. A payment's `payment_freeze_reason` shows it.
- The amount can be at most what the payment can still refund plus the customer credit issued from it that is still unspent: "Refund Amount greater than what payment can be refunded" (`validation_error`). This is checked when the refund is recorded, after the provider refunded.
- Authorize.Net refunds only settled payments: 422 `invoice_payment_refund_requires_settlement` ("Wait until Authorize.Net settles this payment before refunding it.") while it settles, and `invoice_payment_refund_provider_state_invalid` (with `error.provider_status`) in any other state.
- The order must be published, but that is checked when the refund is recorded, after the provider refunded.

## Side effects

- Refunds the card through the provider.
- Records the refund (as made by the API key's user) and sends the `payment.refund_issued` webhook.
- Customer credit issued from this payment and still unspent is taken back first, which lowers the customer's credit; the order gets an `ORDER_CREDIT_RECOVERY` record for it.
- On a Customer Portal payment, its card surcharge on the invoice is reduced to match.
- If the provider later reports that the refund failed, the refund stays and a `REFUND_REVERSAL` record gives the payment its money back; the order's history notes it.
- Customer Portal pages the order's customer has open refresh.

## Path parameters

- `team_slug` (string, required): Your team slug, shown as Team ID next to your API key in Profile. Example `your-team`.
- `hash_id` (string, required): The sales order's or quote's `id` (a hash id). The `ord_` unique key and the order number do not work here. Example `V4keXwwxMQyXw7`.
- `id` (string, required): The card payment's `id` (a hash id). Example `JMQ642WFe4sANJ`.

## Request body

Content type `application/json`, required.

Fields:

- `amount` (integer, required): How much to refund, in thousandths. Required in practice: see above. Minimum 1.
- `locking_token` (string, required): Your one-time token for this refund. Required.
- `intent` (string, optional): Accepted and not used: the refund is the same whatever you send. Any other value is refused with 422. One of: `"provider_refund_only"`, `"cancel_and_return_credit"`, `"return_to_source_credit"`.

### Example request

```bash
curl -X PUT 'https://secure.yoprint.com/v2/api/store/your-team/v2/sales_order/V4keXwwxMQyXw7/payment_gateway_refund/JMQ642WFe4sANJ' \
  -H 'Authorization: Bearer YOUR_API_KEY' \
  -H 'Accept: application/json' \
  -H 'X-Yoprint-Decimal-Format: exact-v1' \
  -H 'Content-Type: application/json' \
  -d '{
    "amount": 25000,
    "locking_token": "refund-1001-a"
  }'
```

## Response

`200` Successful response.

Fields:

- `data` (null, required): No data.
- `status` (integer, required)
- `success` (boolean, required)

Example response:

```json
{
  "data": null,
  "status": 200,
  "success": true
}
```

## Errors

- `401` The API key is missing or wrong (`unauthenticated`).
- `403` You are not allowed to do this. `api_access_not_in_plan`: your team's plan doesn't include API access (Pro and above, with an active subscription or trial), so every request answers this until the team upgrades. When your role lacks the permission the operation needs, the body is only `{"message": "Insufficient Permission"}`. Other refusals use the error envelope, with `unauthorized` or `user_is_not_authorized`.
- `404` Not found. `page_not_found`: the team slug is unknown, you are not a member of the team, or an id in the path is not one of this team's. `code: null`, with a message that starts "No query results for": there is no record with that id.
- `422` The request was refused and changed nothing. `validation_failed` lists what to fix in `error.fields`; other codes name a business rule, explained in `error.message`. A few checks answer with only `{"message": "..."}`. An operation that calls another service (a payment provider, a carrier, an accounting app) can answer `connect_timeout` ("Please retry again") when that service does not answer in time: the change may or may not have happened there, so read the record back before you retry.
  - When: Another order's payment, a record that isn't a card payment, a Charge It Pro payment or a Customer Portal payment its provider can't refund (`refund_failed`); a payment from Customer Portal credit (`customer_payment_parent_refund_required`). Fix: Check the payment, or refund it in the provider's dashboard and record it with Record a refund.
  - When: The provider refused (`refund_processing_failed`). For Stripe the message says why (for example a disputed charge, or more than is left to refund) and `error.data` has `provider` `stripe` and `provider_error_code`. Fix: Read the message; check the payment in the provider's dashboard.
  - When: A payment is frozen by an open dispute or review (`customer_payment_disputed_funds_unavailable` or `invoice_payment_funding_withheld`, with `error.reason`). Fix: Wait for the provider's decision, then try again.
  - When: An Authorize.Net payment that hasn't settled (`invoice_payment_refund_requires_settlement`, `invoice_payment_refund_provider_state_invalid`), or the Customer Portal payment's provider account changed (`invoice_payment_provider_identity_mismatch`). Fix: Wait for settlement, or refund it in the provider's dashboard.
  - When: More than the payment can refund (`validation_error`, "Refund Amount greater than what payment can be refunded"). This is found after the provider refunded: check the provider before you do anything else. Fix: Refund at most the payment's `refundable_amount` plus unspent credit issued from it.
- `500` A business rule refused the request (see the operation's errors), or something failed inside YoPrint. Refusals use the error envelope with a code such as `action_not_allowed`; an unexpected failure answers `{"message": "Server Error"}`. Repeating a refusal gets the same answer; after an unexpected failure, read the record back before you retry a change.
  - When: No `amount`, an id that is no record of the team, or a payment through the old Square connection (`{"message": "Server Error"}`). Fix: Send `amount` and a card payment `id` (`payment_gateway` not null).

## Related

- [Record a refund of a payment](https://developer.yoprint.com/reference/payments/v2-sales-order-manual-payment-refund.md): `PUT /v2/api/store/{team_slug}/v2/sales_order/{hash_id}/payment/{id}/refund`
