Step 6: Create Order

Once you have the data collected from the customer, you are ready to make the Create Order request to start the order process with Banxa.

πŸ‘

Remember that the JSON string should not contain any whitespace outside of double quotes

const axios = require('axios').default;

function generateHmac(signature, nonce) {
    ...
 }

function sendPostRequest(query, payload) {
    const hostname = 'https://api.banxa-sandbox.com';
    const nonce = Date.now();
    let data = "POST\n" + query + "\n" + nonce + "\n" + payload;

    const authHeader = generateHmac(data, nonce);
    axios.post(hostname + query, payload, {
        headers: {
            'Authorization': 'Bearer ' + authHeader,
            'Content-Type': 'application/json',
        }
    }).then((response) => {
        console.log(response);
    }).catch((error) => {
        console.error(error);
    })
}

sendPostRequest('/api/orders', '{"account_reference":"test01","source":"USD","target":"BTC","source_amount":"100","return_url_on_success":"test.com","wallet_address":"[VALID_WALLET_ADDRESS]"}')
import requests
import time
import hmac

url = 'https://[PARTNER-NAME].banxa-sandbox.com'

def generateHmac(payload, nonce):
    ...
    
def sendPostRequest(query, payload):
    nonce = int(time.time())

    data = 'POST\n' + query + '\n' + str(nonce) + '\n' + payload

    authHeader = generateHmac(data, nonce)

    response = requests.post(url + query,
        data = payload,
        headers = {
                'Authorization': 'Bearer ' + authHeader,
                'Content-Type': 'application/json'
        })

    print(response.content)

sendPostRequest('/api/orders', '{"account_reference":"test01","source":"USD","target":"BTC","source_amount":"100","return_url_on_success":"test.com","wallet_address":"[VALID_WALLET_ADDRESS]"}')
package com.banxa;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Formatter;

public class BanxaService {
    private static final String BANXA_URL = "https://api.banxa-sandbox.com";

    public static void main(String[] args) throws Exception {
        BanxaService banxaService = new BanxaService();

        banxaService.sendPostRequest("/api/orders", "{" +
                "\"account_reference\":\"test01\"," +
                "\"source\":\"USD\"," +
                "\"target\":\"BTC\"," +
                "\"source_amount\":\"100\"," +
                "\"return_url_on_success\":\"test.com\"," +
                "\"wallet_address\":\"[VALID_WALLET_ADDRESS]\""+
                "}");
    }

    private void sendPostRequest(String query, String payload) throws Exception {
        String hmac = getHmac("POST", query, payload);

        HttpClient httpClient = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(BANXA_URL + query))
                .timeout(Duration.ofSeconds(10))
                .header("Authorization", "Bearer " + hmac)
                .header("content-type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(payload))
                .build();

        HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());

        if (response.statusCode() == 200) {
            System.out.println("OK");
        } else {
            System.out.println("Failed: " + response.statusCode());
        }
    }

    public String getHmac(String method, String query, String payload) throws Exception {
        ...
    }
}
require 'rest-client'
require 'openssl'

def signed_header(query, body = nil, method = 'POST', content_type = 'application/json')
  ...
end

def generate_hmac(time, query, body, method = 'POST')
  ...
end

def request_env_url(resource)
  ...
end

def post_request(query, payload = nil)
  response = RestClient.post(request_env_url(query), payload, signed_header(resource, payload, 'POST'))
  JSON.parse(response.body)
end

def create_order(account_reference, source, target, wallet_address, return_url_on_success)
  query = "/api/orders"
  payload = {
    account_reference: account_reference,
    source: source,
    target: target,
    wallet_address: wallet_address,
    return_url_on_success:return_url_on_success
  }.to_json
  post_request(query, payload)
end