# Generate Sale with Token

This is a variant of [POST /generate-sale — Generate Payment](/payments-migrated/hosted-payment-page/post-generate-sale); the request and responses below are the ones documented for Tokenization.

- Sandbox: `POST https://sandbox.payme.io/api/generate-sale`
- Production: `POST https://live.payme.io/api/generate-sale`

## Credentials

No credentials are documented for this endpoint.

## Overview

#### Generating tokens
Please note that you can receive a token representing the buyer (including credit card information) as part of the [`generate-sale`](https://docs.paymeservice.com/docs/payme-public/b3A6MjYyMzkxODU-generate-sale) API call. This token can be used to generate future server-to-server sales without requiring the buyer to re-enter his credit card information. The token will be returned only as part of the callback.

#### Obtaining Tokens

There are two ways of obtaining a token:

In order to receive a token in addition to charging the credit card use the `capture_buyer` attribute.
In order to receive a token without charging the credit card use the dedicated `sale_type` attribute and set the value to token.

Attribute| Description
---|---
`capture_buyer`|	`1` Flag for requesting the buyer's token in addition to charging the credit card (0 - do not capture token, 1 - capture token)
`sale_type`	|`token` Flag for requesting the buyer's token without charging the credit card

#### Charging Tokens
After generating a token you may charge the buyer at any point by using the buyer_key attribute when generating a new sale.

Attribute|	Description
---|---
`buyer_key`	|`XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX` Buyer key for an instant-payment with the token

## Parameters

This endpoint takes no path, query or header parameters.

## Request body

Content type: `application/json`

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `seller_payme_id` | string | no | MPL. Your private key in PayMe system. |
| `sale_price` | integer | no | For example, if the price is 50.75 (max 2 decimal points) the value that needs to be sent is 5075. Note that the minimum value is 500. |
| `currency` | string | no | Sale currency. [3-letter ISO 4217 name](https://en.wikipedia.org/wiki/ISO_4217). |
| `product_name` | string | no | Short name and description of the product/service. This text will be shown in the invoice as well, if the seller has enabled the invoices module in his account panel. Limited to 500 characters |
| `transaction_id` | string | no | Merchant's unique sale ID for correlation with us |
| `installments` | string | no | Amount of installments for the sale. We allow you to manually set the number of installments or to specify a top limit of installments for the buyer to choose from while paying. Setting a fixed number of installments is simply entering a number between 1 and 12. The sale will be initiated with this number of installments and the buyer will not be able to change it. To set a range for the buyer to choose from, enter the following: 103 - allow up to 3 installments 106 - allow up to 6 installments 109 - allow up to 9 installments 112 - allow up to 12 installments |
| `market_fee` | number | no | A decimal representing the percent of the sale price that is collected for the marketplace (includes VAT). This fee is added on top of our fees and transferred to the marketplace once a month. Default value is the market fee of the Seller, as set upon Seller creation. |
| `sale_send_notification` | boolean | no | Flag to send email and/or SMS notifications |
| `sale_callback_url` | string | no | Callback response to your page regarding call to our API. Default value is taken from the Merchant's settings. Note that you may not send a "localhost" URL as value |
| `sale_email` | string | no | In case sale send notification is true provide the address to send email notification |
| `sale_return_url` | string | no | We will redirect the IFRAME and the buyer to this URL upon payment success. Default value is taken from the Merchant's settings |
| `sale_mobile` | string | no | In case sale send notification is true, provide the phone number to send SMS notification, if the seller has enabled the SMS module in his account panel |
| `sale_name` | string | no | The name that will be displayed when sending a notification |
| `capture_buyer` | boolean | no | Flag for requesting the buyer's token for this payment (0 - do not capture token, 1 - capture token). For additional information see Tokens explanation below |
| `buyer_perform_validation` | boolean | no | Flag for performing an online validation of the card with the Issuer. |
| `sale_type` | string | no | You can find all the sale types [Sale Types](/guides-and-use-cases-migrated/lists-statuses/sale-types). |
| `sale_payment_method` | string | no | the Payment method used to pay the sale. For all the payment methods see [Available Payment Methods](/payments-migrated/direct-api/available-payment-methods). |
| `layout` | string | no | IFRAME payment page layout. Optional attribute which may be used with "bit" sale_payment_method. Avai lable layouts are:dynamic, qr-sms or dynamic-loose dynamic loose removes the validation for the social ID and CVV. |
| `language` | string | no | Changes the language of the payment IFRAME to English, as well as the error messages. Default value is Hebrew (he) |

### Request example

```json
{
  "seller_payme_id": "MPLDEMO-MPLDEMO-MPLDEMO-1234567",
  "sale_price": 1000,
  "currency": "ILS",
  "product_name": "Smartphone",
  "transaction_id": "12345",
  "installments": "1",
  "market_fee": 0.5,
  "sale_send_notification": true,
  "sale_callback_url": "https://www.payme.io",
  "sale_email": "test@payme.io",
  "sale_return_url": "https://www.payme.io",
  "sale_mobile": "+9725254448888",
  "sale_name": "Test User",
  "capture_buyer": false,
  "buyer_perform_validation": false,
  "sale_type": "sale",
  "sale_payment_method": "credit-card",
  "layout": "string",
  "language": "he"
}
```

## Code samples

### curl

```bash
curl -X POST 'https://sandbox.payme.io/api/generate-sale' \
  -H 'Content-Type: application/json' \
  --data-raw '{
  "seller_payme_id": "MPLDEMO-MPLDEMO-MPLDEMO-1234567",
  "sale_price": 1000,
  "currency": "ILS",
  "product_name": "Smartphone",
  "transaction_id": "12345",
  "installments": "1",
  "market_fee": 0.5,
  "sale_send_notification": true,
  "sale_callback_url": "https://www.payme.io",
  "sale_email": "test@payme.io",
  "sale_return_url": "https://www.payme.io",
  "sale_mobile": "+9725254448888",
  "sale_name": "Test User",
  "capture_buyer": false,
  "buyer_perform_validation": false,
  "sale_type": "sale",
  "sale_payment_method": "credit-card",
  "layout": "string",
  "language": "he"
}'
```

### Node.js

```javascript
const response = await fetch("https://sandbox.payme.io/api/generate-sale", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "seller_payme_id": "MPLDEMO-MPLDEMO-MPLDEMO-1234567",
    "sale_price": 1000,
    "currency": "ILS",
    "product_name": "Smartphone",
    "transaction_id": "12345",
    "installments": "1",
    "market_fee": 0.5,
    "sale_send_notification": true,
    "sale_callback_url": "https://www.payme.io",
    "sale_email": "test@payme.io",
    "sale_return_url": "https://www.payme.io",
    "sale_mobile": "+9725254448888",
    "sale_name": "Test User",
    "capture_buyer": false,
    "buyer_perform_validation": false,
    "sale_type": "sale",
    "sale_payment_method": "credit-card",
    "layout": "string",
    "language": "he"
  }),
});

console.log(await response.text());
```

### Python

```python
import requests

response = requests.post(
    "https://sandbox.payme.io/api/generate-sale",
    json={
        "seller_payme_id": "MPLDEMO-MPLDEMO-MPLDEMO-1234567",
        "sale_price": 1000,
        "currency": "ILS",
        "product_name": "Smartphone",
        "transaction_id": "12345",
        "installments": "1",
        "market_fee": 0.5,
        "sale_send_notification": True,
        "sale_callback_url": "https://www.payme.io",
        "sale_email": "test@payme.io",
        "sale_return_url": "https://www.payme.io",
        "sale_mobile": "+9725254448888",
        "sale_name": "Test User",
        "capture_buyer": False,
        "buyer_perform_validation": False,
        "sale_type": "sale",
        "sale_payment_method": "credit-card",
        "layout": "string",
        "language": "he",
    },
)

print(response.text)
```

### PHP

```php
<?php

$body = <<<'BODY'
{
  "seller_payme_id": "MPLDEMO-MPLDEMO-MPLDEMO-1234567",
  "sale_price": 1000,
  "currency": "ILS",
  "product_name": "Smartphone",
  "transaction_id": "12345",
  "installments": "1",
  "market_fee": 0.5,
  "sale_send_notification": true,
  "sale_callback_url": "https://www.payme.io",
  "sale_email": "test@payme.io",
  "sale_return_url": "https://www.payme.io",
  "sale_mobile": "+9725254448888",
  "sale_name": "Test User",
  "capture_buyer": false,
  "buyer_perform_validation": false,
  "sale_type": "sale",
  "sale_payment_method": "credit-card",
  "layout": "string",
  "language": "he"
}
BODY;

$ch = curl_init('https://sandbox.payme.io/api/generate-sale');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => $body,
]);

$response = curl_exec($ch);
curl_close($ch);

echo $response;
```

## Responses

### 200 — OK (success)

| Field | Type | Description |
| --- | --- | --- |
| `status_code` | integer | Status of the request (0 - success, 1 - error) |
| `sale_url` | string | The URL of the IFRAME secured payment form to display to the buyer |
| `payme_sale_id` | string | Our unique sale ID |
| `payme_sale_code` | integer | Our unique sale code (for display purposes only) |
| `price` | integer | Sale final price. For example, if the price is 50.75 (max 2 decimal points) the value that needs to be sent is 5075 |
| `transaction_id` | string | Merchant's unique sale ID for correlation with us |
| `currency` | string | Sale currency. [3-letter ISO 4217 name](https://en.wikipedia.org/wiki/ISO_4217). |

```json
{
  "status_code": 0,
  "sale_url": "string",
  "payme_sale_id": "string",
  "payme_sale_code": 12345678,
  "price": 10000,
  "transaction_id": "12345",
  "currency": "string"
}
```
