Accept a card payment
Charge a card, authorize and capture, handle 3-D Secure, and send Level 2 and Level 3 fields.
POST /v1/transactions/pay charges a card in one step. To place a hold now and collect the funds later, call POST /v1/transactions/authorize, then POST /v1/transactions/{id}/capture. Read the outcome with GET /v1/transactions/{id} or from a TransactionEvent webhook.
The request
- Send either a saved card as
tokenId, or the card itself as acardobject withcardHolder,cardNumber,expirationMonth, andexpirationYear.securityCodeis optional. amountis the total charged, including any payer fee.payerFeeis required on pay and capture. Send 0 when the payer is not charged a fee.payeris the name on the payment, which can differ from the card holder.
To test, use a card from Test cards and bank accounts, or your provider's test cards if the sandbox account is connected to a Stripe or Adyen test account.
curl -s -u "$DHANGO_KEY:$DHANGO_SECRET" \ -H "Content-Type: application/json" \ -H "accountKey: $ACCOUNT_KEY" \ -H "Idempotency-Key: 1b2c3d4e-5f60-4781-9abc-def012345678" \ -d '{"tokenId":"tok_example","payer":"Ada Lovelace","amount":51.50,"payerFee":1.50,"orderId":"ord_100","taxAmount":0,"shippingAmount":0,"returnUrl":"https://example.com/paid"}' \ https://api.dhangodemo.com/v1/transactions/payawait fetch("https://api.dhangodemo.com/v1/transactions/pay", { method: "POST", headers: { Authorization: "Basic " + Buffer.from(process.env.DHANGO_KEY + ":" + process.env.DHANGO_SECRET).toString("base64"), "Content-Type": "application/json", accountKey: process.env.ACCOUNT_KEY, "Idempotency-Key": crypto.randomUUID() }, body: JSON.stringify({ tokenId: "tok_example", payer: "Ada Lovelace", amount: 51.5, payerFee: 1.5, orderId: "ord_100", returnUrl: "https://example.com/paid" })});import os, uuid, requestsauth = (os.environ["DHANGO_KEY"], os.environ["DHANGO_SECRET"])headers = {"accountKey": os.environ["ACCOUNT_KEY"]}response = requests.post( "https://api.dhangodemo.com/v1/transactions/pay", auth=auth, headers={**headers, "Idempotency-Key": str(uuid.uuid4())}, json={ "tokenId": "tok_example", "payer": "Ada Lovelace", "amount": 51.5, "payerFee": 1.5, "orderId": "ord_100", "taxAmount": 0, "shippingAmount": 0, "returnUrl": "https://example.com/paid" },)response.raise_for_status()payment = response.json()3-D Secure
Some cards need the payer to authenticate with their bank before the payment goes through. To support that, set returnUrl on pay or authorize. When authentication is needed, the response includes NextAction with type set to Redirect and a URL. Send the payer to that URL. When they finish, they come back to your returnUrl with transactionId in the query string.
Read that transaction to see the result:
- A successful payment has event type Payment and a payment date.
- A failed authentication has a reject date.
If the processor requires 3-D Secure and you did not send returnUrl, the transaction fails.
The Fake processor does not simulate 3-D Secure. Test this flow against a provider test account that returns NextAction.
Level 2 and Level 3
Sending Level 2 and Level 3 data can lower the cost of a card payment, mostly on business and purchasing cards, when the processor accepts it. Pay and authorize accept customerId, orderId, shippingAmount, taxAmount, and lineItems, and capture accepts shippingAmount, taxAmount, and lineItems. Each line item can include productCode, productDescription, unitPrice, quantity, taxAmount, discountAmount, unitOfMeasure, and commodityCode.
On Stripe, keep the numbers consistent. Stripe rejects the payment when amount does not match the line items plus shipping and tax, and it rejects Level 2 and Level 3 data for merchants outside the United States. Other processors do not reject a payment for a mismatch.
After the payment
To reverse or complete the payment, see Refund, void, or capture. To fund several merchants from one card payment, see Split a payment.