Authorize and Capture Transactions

Découvrez comment fonctionne le processus d'autorisation et de capture d'Affirm. Autorisez une transaction pour réserver des fonds et générer un identifiant unique, puis capturez-la pour régler les fonds et finaliser le paiement. Prend en charge à la fois les captures complètes et partielles.

Autoriser la transaction

The Authorize Transaction endpoint creates a loan and reserves the funds. When you authorize, Affirm generates a unique id that you’ll use to reference the transaction moving forward. You must authorize a transaction before capturing it.

Diagramme présentant le flux d'autorisation et de capture.

Cela représente le flux d'autorisation et de capture.

❗️

If you do not authorize a transaction, it will not be considered active. This means the user will not see the loan, and you will not be able to capture the funds. For this reason, you should authorize the loan as soon as you receive acheckout_token.

To authorize a transaction you'll need the checkout_token returned from your Checkout Integration to pass it into the transaction_id parameter.

curl https://sandbox.affirm.com/api/v1/transactions
     -X POST
     -u "{public_api_key}:{private_api_key}"
     -H "Content-Type: application/json"
     -d '{"transaction_id": "{checkout_token}","order_id": "{order_id}"}'
$endpoint = "https://sandbox.affirm.com/api/v1/transactions";
$data = '{"transaction_id": "' . $_POST["transactions_id"] . '"}';
try {
 $response = callAffirm($endpoint, $data);
 // Handle the response
} catch (Exception $e) {
 // Handle the exception
}
using(var httpClient = new HttpClient()) {
	using(var request = new HttpRequestMessage(new HttpMethod("POST"), "https://sandbox.affirm.com/api/v1/transactions")) {
		var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("<public_api_key>:<private_api_key>"));
		request.Headers.TryAddWithoutValidation("Authorization", $ "Basic {base64authorization}");
		request.Content = new StringContent("{\"transaction_id\":\"<checkout_token>\"}");
		request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
		var response = await httpClient.SendAsync(request);
		HttpContent responseContent = response.Content;
		using(var reader = new StreamReader(await responseContent.ReadAsStreamAsync())) {
			Console.WriteLine(await reader.ReadToEndAsync());
		}
		Console.ReadKey();
	}
}

Vous devriez recevoir une réponse contenant un identifiant unique (« AMLC-5X0W » dans cet exemple).

{
    "status": "authorized",
    "amount_refunded": 0,
    "provider_id": 1,
    "created": "2021-06-23T23:25:55Z",
    "order_id": "ABC123",
    "checkout_id": "7WYDR0M83CGE47GJ",
    "currency": "USD",
    "amount": 49999,
    "events": [
        {
            "currency": "USD",
            "amount": 49999,
            "type": "auth",
            "id": "7WYDR0M83CGE47GJ",
            "created": "2021-06-23T23:26:28Z"
        }
    ],
    "remove_tax": false,
    "authorization_expiration": "2021-07-23T23:26:28Z",
    "id": "AMLC-5X0W"
}

Une fois que vous avez autorisé une transaction et reçu l'objet de réponse, votre site doit effectuer les opérations suivantes :

  • Valider que le montant autorisé est égal au total de la commande
  • Store the id (transaction identifier).
  • Marquez le paiement de la commande comme étant en attente.

Si l'autorisation échoue, votre site peut potentiellement enregistrer cette tentative de paiement, car elle n'est pas requise de notre côté.

📘

Lignes directrices sur l'autorisation

Autorisez chaque prêt Affirm une seule fois pour le montant total de la transaction en cours d'achat. Si vous avez un cas d'utilisation spécifique où cela peut être difficile, veuillez communiquer avec nous à l'adresse [email protected] ou utiliser le widget au bas de la page.

Capturer une transaction

Once an order has been fulfilled, you must send a Capture Transaction request to Affirm in order to capture or settle the funds. You will want to perform this activity from your secure back-end systems. To capture an authorized transaction, you'll need the id provided in the Authorization API response. There are no required fields that need to be stored from the Capture response.

La capture des fonds est similaire à celle d'une transaction par carte de crédit. Après avoir saisi le prêt, voici ce qui se produit :

  • Affirm informe le client que le prêt a été saisi et que son premier paiement est dû à Affirm dans 30 jours (pour les mensualités) ou dans 2 semaines (pour Pay in 4), ou tout autre échéancier de paiement.
  • Affirm paie le commerçant dans les 2 à 3 jours ouvrables par virement ACH vers le compte bancaire fourni.
curl https://sandbox.affirm.com/api/v1/transactions/{id}/capture
     -X POST
     -u {public_api_key}:{private_api_key}"
     -H "Content-Type: application/json"
     -d '{"order_id": "{order_id}", "shipping_carrier": "USPS", "shipping_confirmation": "1Z23223", "amount":49999}'
$endpoint = "https://sandbox.affirm.com/api/v1/transactions";
$url = $endpoint . "/" . $_GET["id"] . "/capture";
$data = '';
try {
 $response = callAffirm($url, $data);
// Handle the response
} catch (Exception $e) {
 // Handle the exception
📘

Capture partielle

Note that you can perform a partial capture by specifying a value in cents within the amount parameter.
La somme cumulée des montants de capture partielle ne peut pas dépasser le total initialement autorisé.

Vous recevrez ensuite une réponse avec la confirmation.

{
    "fee": 1500,
    "created": "2021-06-23T23:27:13Z",
    "order_id": "ABC123",
    "currency": "USD",
    "amount": 49999,
    "reference_id": "6789",
    "type": "capture",
    "id": "7WYDR0M83CGE47GJ"
}