Save a card and charge it later

Prerequisites

Save a buyer's card once as a token, the buyer_key, and charge it from your server whenever you need to: a repeat order, a top-up, or a charge after delivery. The buyer doesn't have to be present, and the card number never reaches your servers.

This guide covers the ways to save a card, the two settings that decide what a token can do, how to charge it, and how to keep tokens safe. To charge a saved card on a fixed schedule, see Build recurring billing with subscriptions.

Before you start

1. Save the card

Way Charges the buyer? Who handles the card Use it when
capture_buyer: 1 on generate-sale Yes, the sale amount PayMe's payment page (sale_url) The first order should also save the card.
sale_type: token on generate-sale No PayMe's payment page (sale_url) You want a card on file before any charge, for example at sign-up.
Hosted Fields tokenize() No PayMe's card fields inside your own checkout Your checkout must look native to your site.
capture-buyer-token No Your server sends the card number You are PCI DSS Level 1 certified.

Saving works with the payment methods credit-card, apple-pay, il-direct-debit and multi. Other methods return error 4.

Save while charging (capture_buyer)

Add capture_buyer: 1 to a regular generate-sale and show the buyer the sale_url. When the buyer pays, PayMe charges the card, saves it, and sends the buyer_key in the sale-complete callback.

{
  "seller_payme_id": "MPLDEMO-MPLDEMO-MPLDEMO-1234567",
  "sale_price": 5000,
  "currency": "ILS",
  "product_name": "First order",
  "transaction_id": "order-1001",
  "capture_buyer": 1,
  "buyer_is_private": true,
  "sale_callback_url": "https://your-server.example.com/payme/callback",
  "language": "en"
}

Save without charging (sale_type: token)

Send sale_type: token on generate-sale and show the buyer the sale_url. Nothing is charged: PayMe sets the price to 100 and doesn't charge it, but product_name and a numeric sale_price are still required. The buyer_key arrives in the sale-complete callback. For a page that can be opened only once, add layout: token-capture and sale_should_be_opened_once: true.

{
  "seller_payme_id": "MPLDEMO-MPLDEMO-MPLDEMO-1234567",
  "sale_type": "token",
  "sale_price": 100,
  "currency": "ILS",
  "product_name": "Save card",
  "buyer_is_private": true,
  "sale_callback_url": "https://your-server.example.com/payme/callback",
  "language": "en"
}

Save in your own checkout (Hosted Fields)

In the browser, instance.tokenize() returns a token: that is the buyer_key. Send it to your server and charge it there. A Hosted Fields token is permanent unless you create the instance with tokenIsPermanent: false, and account-level unless you pass buyerIsPrivate: true (both explained below). Setup and code: Hosted Fields JSAPI guide.

Save from your server (capture-buyer-token)

capture-buyer-token takes the card number, expiry and CVV, so use it only if you are PCI DSS Level 1 certified. It returns the buyer_key in the response. The token is single-use unless you send buyer_is_permanent: true.

2. Decide what the token can do

Two settings, sent when you save the card, decide how often the token can be charged and who can charge it.

Single-use or permanent

Saved with Default
capture_buyer or sale_type: token Permanent.
Hosted Fields Permanent. tokenIsPermanent: false makes it single-use.
capture-buyer-token Single-use. buyer_is_permanent: true makes it permanent.

A permanent token can be charged any number of times and doesn't expire in PayMe. A single-use token is deactivated by the first charge, even when that charge is declined. Later charges return error 604 ("Buyer inactive"), and a subscription refuses it with error 376. Save permanent tokens unless you want exactly one charge.

Seller-level or account-level (buyer_is_private)

buyer_is_private: true (seller-level) Not sent (account-level, the default)
Who can charge it Only the seller (MPL) that saved it Any seller of the same partner
Read it back with get-buyer-key seller_payme_id, or a partner's payme_client_key payme_client_key only. Sending seller_payme_id returns error 603.

In Hosted Fields the option is buyerIsPrivate (default false).

3. Charge the saved card

Send the buyer_key to generate-sale from your server. The call charges the card at once: there is no payment page, no sale_url, no CVV and no 3D Secure.

{
  "seller_payme_id": "MPLDEMO-MPLDEMO-MPLDEMO-1234567",
  "buyer_key": "BUYERDEMO-BUYERDEMO-BUYERDEMO-1234567",
  "sale_price": 5000,
  "currency": "ILS",
  "product_name": "Repeat order",
  "transaction_id": "order-1002",
  "sale_callback_url": "https://your-server.example.com/payme/callback",
  "language": "en"
}

The response holds the result (abridged):

{
  "status_code": 0,
  "payme_status": "success",
  "status_error_code": 0,
  "payme_sale_id": "SALE1700-000000AA-EXAMPLE1-SALE0001",
  "payme_sale_code": 12345678,
  "sale_status": "completed",
  "sale_payment_method": "credit-card",
  "price": 5000,
  "currency": "ILS",
  "transaction_id": "order-1002",
  "payme_transaction_id": "TRAN1700-000000AA-EXAMPLE1-TXN00001",
  "buyer_card_mask": "532610******5846",
  "sale_3ds": false
}

A declined charge also returns HTTP 200, with status_code: 1 (abridged):

{
  "status_code": 1,
  "status_error_code": 20004,
  "status_error_details": "Refusal by credit company. Please try a different card",
  "payme_sale_id": "SALE1700-000000AA-EXAMPLE1-SALE0002",
  "sale_status": "failed"
}

Because the buyer isn't there, nothing is checked with them when you charge: no CVV and no 3D Secure. Charge a saved card only for payments the buyer has agreed to.

4. Get a token back

5. Store tokens safely

6. Keep saved cards up to date (Card Updater)

Cards expire, and card companies replace lost or stolen ones. A charge on the old card then fails, and you have to save the new card and replace the stored buyer_key.

With the Card Updater service active on the seller's account, PayMe updates saved cards when an Israeli card company reports a replacement card. The buyer_key stays the same, so stored tokens and subscriptions keep working. The new masked number and expiry show the next time you read the token with get-buyer-key. PayMe sends no callback when it updates a card. To activate Card Updater, contact PayMe.

Errors

Code When What to do
601 buyer_key sent with capture_buyer Send one or the other.
602 buyer_key sent with sale_type token or seller-token Charge with a regular sale type.
606 buyer_key sent with sale_type: template A template can't be charged with a token.
357 buyer_key sent with sale_type: multi-capture Capture the original authorization instead.
604 The token is inactive, or a single-use token was already charged Save the card again, as a permanent token.
605 The token belongs to another seller or another partner Charge with the seller that saved it.
603 get-buyer-key with seller_payme_id for an account-level token Send payme_client_key instead.
600 get-buyer-key found no saved card on the sale or subscription, or the sale isn't paid Check the ID, and that the sale was paid.
130 The charge is over the seller's processing limit Contact PayMe.
4 Saving isn't supported for the sale's payment method Use one of the methods listed in step 1.
608 buyer_perform_validation sent without capture_buyer or a token sale Remove it, or save a card in the same request.
A decline code, for example 20004 The card issuer declined the charge (HTTP 200, status_code: 1) Ask the buyer for another card.

All codes: Errors.

Test it in the sandbox

More cards: Test Cards and Payment Methods.