Client script
Create a token from the browser and calculate payer fees with a separate API call.
The client script sends payment details from the browser straight to dhango and hands back a token id. Your server stores that id, never the card or account number.
Load https://api.dhangodemo.com/js/clientscript.v2.js, create the client with new dhango(), and set baseUrl to https://api.dhangodemo.com/v1. Two settings take an account key:
paymentMethodAccountKeyis the merchant being paid. It decides which payment methods the drop-in form offers, and it is used for payer fees.tokenOwnerAccountKeyis the account that owns the new token. Use the merchant's key to keep the token with that merchant, or your platform's key to share it across your merchants. When you leave it out, the token belongs to thepaymentMethodAccountKeyaccount. See Token ownership.
Replace YOUR_ACCOUNT_KEY in the samples with your own key. The live forms on this page call the sandbox API with a public demo account key, so you can try them as you read.
Creating a token does not calculate payer fees. To show a fee, call GET /v1/payerFees, which also works from the browser with the account key. Then, on the pay request from your server, send amount including the fee, and send the fee itself as payerFee.
GET /v1/payerFees takes amount and either cardNumber or tokenId when the fee depends on the card. Do not send both. The response lists payerFees by payment method. See Calculate payer fees.
Custom form
Use a custom form when you want full control over the markup. Collect the card holder, number, expiration month and year, security code, and postal code in your own fields. On submit, createToken posts the card to /v1/tokens, and your success handler receives the token id as postTokenResponse.id. Send only that id to your server. On the payer's next visit, GET /v1/tokens/{id} returns the brand and last four digits.
The script can tokenize other payment methods too. This sample shows a card.
Sandbox This form calls the sandbox Payments API with the public demo account key.
<form id="createToken"> <label for="cardHolder">Card holder</label> <input name="cardHolder" id="cardHolder" /> <label for="cardNumber">Card number</label> <input name="cardNumber" id="cardNumber" /> <label for="expirationMonth">Expiration month</label> <input name="expirationMonth" id="expirationMonth" /> <label for="expirationYear">Expiration year</label> <input name="expirationYear" id="expirationYear" /> <label for="securityCode">Security code</label> <input name="securityCode" id="securityCode" /> <label for="postalCode">Postal code</label> <input name="postalCode" id="postalCode" /> <input type="hidden" id="tokenId" /> <button type="submit" id="submit">Submit</button> <label id="confirmation"></label></form><script src="https://api.dhangodemo.com/js/clientscript.v2.js"></script>const d = new dhango();d.paymentMethodAccountKey = "YOUR_ACCOUNT_KEY";d.baseUrl = "https://api.dhangodemo.com/v1";document.getElementById("createToken").addEventListener("submit", submit);function submit(event) { document.getElementById("confirmation").textContent = ""; event.preventDefault(); var postTokenRequest = { card: { cardHolder: document.getElementById("cardHolder").value, cardNumber: document.getElementById("cardNumber").value, expirationMonth: document.getElementById("expirationMonth").value, expirationYear: document.getElementById("expirationYear").value, securityCode: document.getElementById("securityCode").value }, address: { postalCode: document.getElementById("postalCode").value } }; d.createToken(postTokenRequest, postTokenHandler, postTokenErrorHandler); return false;}function postTokenHandler(postTokenResponse) { document.getElementById("tokenId").value = postTokenResponse.id; document.getElementById("confirmation").textContent = "Token id: " + postTokenResponse.id;}function postTokenErrorHandler(errors) { document.getElementById("confirmation").textContent = "The payment method was not saved.";}Drop-in form
Use the drop-in form when you would rather not build the fields. Add an empty element to the page, and displayTokenForm fills it with the payment fields. On submit, saveToken reads them and creates the token. The success handler works the same way as the custom form, with the token id in postTokenResponse.id.
To match your platform, load your own stylesheet. The script has no styling option of its own. The injected payment methods use the class payment-method-container, and the live form on this page uses the sandbox styles.
Sandbox This form calls the sandbox Payments API with the public demo account key.
<link rel="stylesheet" href="your-form.css" /><form id="createToken"> <div id="dhangoTokenSection"></div> <input type="hidden" id="tokenId" /> <button type="submit" id="submit">Submit</button> <label id="confirmation"></label></form><script src="https://api.dhangodemo.com/js/clientscript.v2.js"></script>const d = new dhango();d.baseUrl = "https://api.dhangodemo.com/v1";d.tokenOwnerAccountKey = "YOUR_ACCOUNT_KEY";d.paymentMethodAccountKey = "YOUR_ACCOUNT_KEY";d.culture = "en";d.validateAccount = false;d.collectBillingAddress = false;d.displayTokenForm("dhangoTokenSection");document.getElementById("createToken").addEventListener("submit", submit);function submit(event) { document.getElementById("confirmation").textContent = ""; event.preventDefault(); d.saveToken(postTokenHandler, postTokenErrorHandler); return false;}function postTokenHandler(postTokenResponse) { document.getElementById("tokenId").value = postTokenResponse.id;}function postTokenErrorHandler(errors) { document.getElementById("confirmation").textContent = "The payment method was not saved.";}Payer fees
getPayerFees calls GET /v1/payerFees on baseUrl, using paymentMethodAccountKey as the accountKey header. Pass amount, plus cardNumber or tokenId when the fee depends on the card. The handler below reads the ACH and Card rows from payerFees.
Sandbox This form calls the sandbox Payments API with the public demo account key.
const d = new dhango();d.paymentMethodAccountKey = "YOUR_ACCOUNT_KEY";d.baseUrl = "https://api.dhangodemo.com/v1";function updateFees() { d.getPayerFees({ amount: document.getElementById("amount").value, cardNumber: document.getElementById("cardNumber").value, tokenId: document.getElementById("tokenId").value }, getPayerFeesHandler);}function getPayerFeesHandler(getPayerFeesResponse) { if (getPayerFeesResponse.achFee == null) { document.getElementById("confirmation").textContent = ""; return; } var achFee = getPayerFeesResponse.payerFees.find(function (fee) { return fee.paymentMethodType === "ACH"; }); var cardFee = getPayerFeesResponse.payerFees.find(function (fee) { return fee.paymentMethodType === "Card"; }); document.getElementById("confirmation").textContent = "ACH fee of " + achFee.total + " and card fee of " + cardFee.total;}Account key in the page
Everything on this page runs with the account key alone. That key can create tokens and read payer fees, but it cannot take a payment. Keep your API secret on your server, and make the pay call from there. See API authentication.