Charging repeatedly
Use the AffiniPay Payment Gateway API to collect payments from customers on an ongoing, scheduled basis. You can either:
- Charge with recurrence. Charge your customer now and then in the future on a schedule. When you use this method to schedule payments, your payment method is verified with the initial charge; there’s no need to wait until the first scheduled charge.
- Create a recurring charge. Charge your customer in the future on a schedule.
After you’ve created the recurring charge, you can update it.
Charging with recurrence
To charge a customer now and then in the future on a schedule, your charge request must include a recur element. In a charge with recurrence, you’ll include details about how often to charge the customer, such as every month or every two weeks. After the initial charge succeeds, a recurring charge is created.
Create a monthly recurring charge from a saved Card for $20.00 starting today
curl -X POST -H "Content-Type:application/json" --user <secret_key>: https://api.affinipay.com/v1/charges -d '
{
"amount": "2000",
"method": "K_5tUK78SKKpNKu9F8fvIw",
"account_id": "diON4KOPnesamprmrxA8Iuo",
"reference": "Donation Plan #1234",
"recur": {
"description": "Monthly donation from Sample Customer",
"interval_unit": "MONTH",
"interval_delay": 1
}
}'
Example response
{
"id": "PUZt8B_fTCWCO25SJfMY6A",
"account_id": "diON4KOPnesamprmrxA8Iuo",
"status": "AUTHORIZED",
"auto_capture": true,
"amount": 2000,
"currency": "USD",
"reference": "Donation Plan #1234",
"authorization_code": "FLCF19",
"method": {
"number": "************4242",
"fingerprint": "GunPelYVthifNV63LEw1",
"card_type": "VISA",
"exp_month": 10,
"exp_year": 2025,
"name": "Sample Customer",
"address1": "123 Main St",
"city": "Austin",
"state": "TX",
"postal_code": "78730",
"type": "card"
},
"amount_refunded": 0,
"cvv_result": "MATCHED",
"avs_result": "ADDRESS_AND_POSTAL_CODE",
"recurring_charge_id": "Ou5d_4XxTBi1hWKKF1iY5g",
"recurring_charge_occurrence_id": "tO6EA6oPRtSa7mX3vaupuQ",
"type": "CHARGE"
}
Creating a recurring charge
To charge a customer in the future on a schedule, you create a new recurring charge that includes details about how often to charge the customer, such as every month or every two weeks.
A recurring charge cannot be scheduled to start on the same day it’s created. In addition, because there’s no initial charge, the payment method isn’t verified up front. Use the events API to detect and respond to any failures that occur in future scheduled payments.
Creating a monthly recurring charge from a saved credit card:
curl -X POST -H "Content-Type:application/json" --user <secret_key>: https://api.affinipay.com/v1/recurring/charges -d '
{
"description": "Monthly recurring charge",
"account_id": "wKgFeDz5HF-BPPl08dcADQ",
"amount": "1250",
"method": "OtmNJP6YRpKrcJ0RdZxGcw",
"schedule": {
"start": "2018-01-01",
"interval_unit": "MONTH",
"interval_delay": 1
}
}'
Example response
{
"id": "xicilh0vTAeZiThRtHU2Ow",
"status": "ACTIVE",
"account_id": "wKgFeDz5HF-BPPl08dcADQ",
"method": {
"type": "card",
"number": "************4242",
"fingerprint": "GunPelYVthifNV63LEw1",
"card_type": "VISA",
"exp_month": 10,
"exp_year": 2025,
"name": "Test Customer"
},
"schedule": {
"interval_unit": "MONTH",
"interval_delay": 1,
"start": "2016-01-01"
},
"description": "Monthly recurring charge",
"amount" : 1250,
"currency": "USD",
"total_occurrences": 0,
"total_amount": 0,
"next_payment": "2018-01-01",
"occurrences" : [ {
"id": "_LIG1tsDQZ21oBgPYTRJdQ",
"amount": 1250,
"status": "PENDING",
"due_date": "2018-01-01",
"attempts": 0
} ]
}
Updating a recurring charge
You can PATCH a recurring charge to update payment or scheduling information.
Updating sensitive data
For PCI compliance reasons, you must update sensitive payment data using a new payment token. Use this method to update the card number or account number or to change the type of payment method.
To update sensitive payment data:
- Gather new payment data in a payment form.
- Create a one-time token.
- PATCH the recurring charge with the new token.
Update the card number of a recurring charge via PATCH
curl -X PATCH -H "Content-Type:application/json" --user <secret_key>: https://api.affinipay.com/v1/recurring/charges/be0t6cgpSyGYz6cxCmAB1A -d '
{
"method": "m_bla2RHSUioRDb29imFmA",
}'
Example response
{
"id": "be0t6cgpSyGYz6cxCmAB1A",
"status": "ACTIVE",
"account_id": "XLu8EABYEeS91ehmSDUzYg",
"method": {
"type": "card"
"number": "************4242",
"fingerprint": "GunPelYVthifNV63LEw1",
"card_type": "VISA",
"exp_month": 7,
"exp_year": 2025
},
"schedule": {
"interval_unit": "MONTH",
"interval_delay": 1,
"start": "2018-01-01"
},
"amount": 5000,
"currency": "USD",
"total_occurrences": 0,
"total_amount": 0,
"next_payment": "2018-01-01"
}
Updating non-sensitive data
To update other data, PATCH the recurring charge with the type of method as well as the data that you want to update.
Update the payment method expiration date of a recurring charge via PATCH
curl -X PATCH -H "Content-Type:application/json" --user <secret_key>: https://api.affinipay.com/v1/recurring/charges/be0t6cgpSyGYz6cxCmAB1A -d '
{
"method": {
"type": "card",
"exp_month": 7,
"exp_year": 2025
}
}'
Example response
{
"id": "be0t6cgpSyGYz6cxCmAB1A",
"status": "ACTIVE",
"account_id": "XLu8EABYEeS91ehmSDUzYg",
"method": {
"type": "card"
"number": "************4242",
"fingerprint": "GunPelYVthifNV63LEw1",
"card_type": "VISA",
"exp_month": 7,
"exp_year": 2025
},
"schedule": {
"interval_unit": "MONTH",
"interval_delay": 1,
"start": "2018-01-01"
},
"amount": 5000,
"currency": "USD",
"total_occurrences": 0,
"total_amount": 0,
"next_payment": "2018-01-01"
}