Initiate Transaction
note
You can use testPartnerId
as your partnerId and testPrivateKey
as your API Key for
testing purposes.
#
Serverwarning
Never share your API Keys with anyone. Anyone with the API Key will be able to create transactions on your behalf. It should reside solely on your servers.
- Request
- Response
POST https://web.bharatx.tech/api/transaction
Headers:Content-Type: application/jsonX-Signature: Base-64 encoded SHA256 hash of request body + request URL without https:// and domain name (For example, in http://example.org/api/example1/?a=b, you should consider only /api/example1/?a=b) + privateApiKey.X-Partner-Id: partnerId
Body:{ "id": "txnId01", // unique "amount": 10000, // in paise "user": { "name": "string", "phoneNumber": "string", // of the format +91xxxxxxxxxx "email": "string" }, // optional "redirect": { // optional - the URL to redirect user to after transaction completion. // By default, this will be your homepage URL "url": "string", // optional - The logo URL to use instead of your default logo // (don't use this unless necessary) "logoOverride": "string", // optional - The hex color of the format \#xxxxxx to use instead of // your default hex color (don't use this unless necessary) "colorOverride": "string" }}
Headers:-
Status = 200: Successfully created the transaction
Body:{ "message": string, // will only be present if you supply "redirect" params in the request "redirectUrl": string}
Status = 400: Bad RequestStatus = 401: Failed to authorize youStatus = 5xx: Unknown failure
Body:{ "message": string}
#
ClientIf you are in a non-browser context, you can use the redirectUrl returned in Initiate Transaction API and take the users to that URL to complete the transaction.
If you are in a browser context, you can use the following code easily:
<script src="https://web.bharatx.tech/sdk.js">
<script> const getTxnId = async () => { // generate a txn id in your server // You should do the server-side initiate // transcation in this server call const txnId = await yourServerCall();
// this method should return the transaction // ID that was supplied in server side initiate return txnId; };
const bharatxButtonSelector = "#bharatx-button"; const partnerId = "testPartnerId"; const amountInPaise = 300*100; const phoneNumber = "+911234567890";
// POPUP flow initiateBharatXTransactionOnElementClick({ btnSelector: bharatxButtonSelector, type: "popup", getTransactionId: getTxnId, partnerId: partnerId, amount: amountInPaise, phoneNumber: phoneNumber, successCallback: (signature) => { // status SUCCESS // you can use this signature to check if the status // update has indeed come from BharatX }, failureCallback: (status, signature) => { // status FAILURE or CANCELLED // you can use this signature to check if the status // update has indeed come from BharatX
// status GET_TXN_ID_FAILED // occurs when the async function you passed // failed or the transaction id // it resolved to is null // signature will not be present in this case } });
// REDIRECT flow initiateBharatXTransactionOnElementClick({ btnSelector: bharatxButtonSelector, type: "redirect", getTransactionId: getTxnId, partnerId: partnerId, amount: amountInPaise, phoneNumber: phoneNumber, // a query parameter "tid" will be attached with this // redirectUrl that is the transaction ID returned by // the function you supply to getTransactionId redirectUrl: "https://your-domain.com/txn-callback", redirectFailureCallback: (status) => { // this failure callback fires ONLY when // transaction fails before the redirect // this failure callback DOES NOT FIRE when // transaction fails after the redirect happens
// status GET_TXN_ID_FAILED // occurs when the async function you passed // failed or the transaction id // it resolved to is null } });</script>