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;

public func generateHmac(
    method: String, 
    query: String, 
    data: Optional<String> = nil
    ) -> String 
{    
  ...
 }

public func postRequest(query: String, payload: String) {
    let method = "POST"
    let hmac = generateHmac(method: method, query: query, data: payload)
    var request = URLRequest(url: URL(string: hostname + query)!)
    
    request.httpMethod = method
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("Bearer " + hmac, forHTTPHeaderField: "Authorization")
    request.httpBody = payload.data(using: String.Encoding.utf8)
    
    let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
        guard error == nil else {
            return
        }
        
        guard let data = data else {
            return
        }
        
        do {
            //create json object from data
            if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
                print (json)
            }
        } catch let error {
            print(error.localizedDescription)
        }
    })
    task.resume()
}

postRequest(query: "api/orders", payload: "{\"account_reference\":\"test01\",\"source\":\"AUD\",\"target\":\"BTC\",\"wallet_address\":\"[VALID_WALLET_ADDRESS]\",\"return_url_on_success\":\"https://banxa.com\",\"source_amount\":300}")
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
import CryptoKit
import Foundation

let hostname = "https://api.banxa-sandbox.com/"
let key = "[YOUR_MERCHANT_KEY]"
let secret = "[YOUR_MERCHANT_SECRET]".data(using: .utf8)!
let secretKey = SymmetricKey(data: secret)

public func generateHmac(
    method: String, 
    query: String, 
    data: Optional<String> = nil
    ) -> String {
    ...
}

public func postRequest(query: String, payload: String) {
    let method = "POST"
    let hmac = generateHmac(method: method, query: query, data: payload)
    var request = URLRequest(url: URL(string: hostname + query)!)
    request.httpMethod = method
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("Bearer " + hmac, forHTTPHeaderField: "Authorization")
    request.httpBody = payload.data(using: String.Encoding.utf8)
    
    let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
        guard error == nil else {
            return
        }
        
        guard let data = data else {
            return
        }
        
        do {
            //create json object from data
            if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
                print (json)
            }
        } catch let error {
            print(error.localizedDescription)
        }
    })
    task.resume()
}

postRequest(query: "api/orders", payload: "{\"account_reference\":\"123test11123a456iasusoa567h\",\"source\":\"AUD\",\"target\":\"BTC\",\"wallet_address\":\"37RdJDa8nbpQRN6e1Ub6uMev55X5uVcqKx\",\"return_url_on_success\":\"https://banxa.com\",\"source_amount\":300}")

What’s Next