API sample code snippets

You are a user of a standard ecommerce shopping cart for which we don’t have a guideline or a plugin yet? You have your own custom shopping cart? We provide here some code samples to guide the developer integrating your platform, which can be helpful to use instead of starting from scratch.

Most of these code examples make a very simple POST request using the given language, which could be use to test as a starting point. Make sure to have installed the required libraries.

All samples use our test environment and a test merchant id 3406, with its test credentials. Make sure you change them after you have an initial working version.

C

CURL *hnd = curl_easy_init();

curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_URL, "https://api.sandbox.wegetfinancing.com/merchant/3406/requests");

struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "cache-control: no-cache");
headers = curl_slist_append(headers, "content-type: application/json");
headers = curl_slist_append(headers, "accept: application/json");
headers = curl_slist_append(headers, "authorization: Basic Y2R4X2FtZXJpY2Fuc3Bpcml0aW5zdGl0dXRlMTp0aGFlLmM2WA==");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);

curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{\"first_name\":\"John\",\"last_name\":\"Doe\",\"amount\":3000.00,\"shipping_address\":{\"street1\":\"15th Fantasy Street\",\"city\":\"New York\",\"state\":\"NY\",\"zipcode\":\"10007\"},\"billing_address\":{\"street1\":\"15th Fantasy Street\",\"city\":\"New York\",\"state\":\"NY\",\"zipcode\":\"10007\"},\"email\":\"johndoe.25742gf@gmail.com\",\"cart_items\":[{\"sku\":\"ref03243\",\"display_name\":\"Samsung TV 9340 DMX\",\"quantity\":1,\"unit_price\":2500,\"unit_tax\":300.00}],\"shipping_amount\":200.00,\"version\":\"1.9\", \"merchant_transaction_id\":\"order_1234\"}");

CURLcode ret = curl_easy_perform(hnd);

C-Sharp

var client = new RestClient("https://api.sandbox.wegetfinancing.com/merchant/3406/requests");
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/json");
request.AddHeader("accept", "application/json");
request.AddHeader("authorization", "Basic Y2R4X2FtZXJpY2Fuc3Bpcml0aW5zdGl0dXRlMTp0aGFlLmM2WA==");
request.AddParameter("application/json", "{\"first_name\":\"John\",\"last_name\":\"Doe\",\"amount\":3000.00,\"shipping_address\":{\"street1\":\"15th Fantasy Street\",\"city\":\"New York\",\"state\":\"NY\",\"zipcode\":\"10007\"},\"billing_address\":{\"street1\":\"15th Fantasy Street\",\"city\":\"New York\",\"state\":\"NY\",\"zipcode\":\"10007\"},\"email\":\"johndoe.25742gf@gmail.com\",\"cart_items\":[{\"sku\":\"ref03243\",\"display_name\":\"Samsung TV 9340 DMX\",\"quantity\":1,\"unit_price\":2500,\"unit_tax\":300.00}],\"shipping_amount\":200.00,\"version\":\"1.9\", \"merchant_transaction_id\":\"order_1234\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

CURL

Curl is not a programming language but a command line tool, which is available for Linux, Mac OS and Windows. This examples use the command line tool curl to make a POST request. If you login into our portal, under the section “Integration Instructions” you will find the same code, customized with your user and password.

The advantage of this is that you can run it on the command line of your server, and you can see that it actually works before you actually start digging into the specific issues of your programming language.

This is on the test environment:

curl -k  \
     -H"Content-Type: application/json" \
     -H"Accept: application/json" \
     -H"Authorization: Basic Y2R4X2FaZXJpY2Fuc3Bpcml0aW5zdGl0dXRlMTp0aGFlLmM2WA==" \
     -X POST -d'{"first_name":"John","last_name":"Doe","amount":850.25,"shipping_address":{"street1":"15th Fantasy Street","city":"New York","state":"NY","zipcode":"10007"},"billing_address":{"street1":"15th Fantasy Street","city":"New York","state":"NY","zipcode":"10007"},"email":"johndoe.25742gf@gmail.com","product_info":"Mountain Bike ATX 850USD","version":"1.9"}' \
     https://api.sandbox.wegetfinancing.com/merchant/3406/requests

And this is on the Production environment (notice only the subdomain changes)

curl -k  \
     -H"Content-Type: application/json" \
     -H"Accept: application/json" \
     -H"Authorization: Basic Y2R4X2FtZXJeY2Fuc3Bpcml0aW5zdGl0dXRlMTp0aGFlLmM2WA==" \
     -X POST -d'{"first_name":"John","last_name":"Doe","amount":850.25,"shipping_address":{"street1":"15th Fantasy Street","city":"New York","state":"NY","zipcode":"10007"},"billing_address":{"street1":"15th Fantasy Street","city":"New York","state":"NY","zipcode":"10007"},"email":"johndoe.25742gf@gmail.com","product_info":"Mountain Bike ATX 850USD","version":"1.9"}' \
     https://api.wegetfinancing.com/merchant/3406/requests

And this should be a sample of the result you can expect, where the most important part is the url that you will have to show to your customer to complete his application. You can copy and paste that url in your browser to see what it is going to look like:

{
  "amount": "850.25",
  "href": "https://partner.sandbox.wegetfinancing.com/partner.sandbox/lc/info/df0c3186b69be8aad35ff837a841d347",
  "inv_id": "df0c3186b69be8aad35ff837a841d347"
}

If you are on Windows, you will have to replace the ‘' characters for ‘^’.

If you don’t have curl installed on your system you can download it from the author’s page: http://curl.haxx.se/download.html

Go

package main

import (
    "fmt"
    "strings"
    "net/http"
    "io/ioutil"
)

func main() {

    url := "https://api.sandbox.wegetfinancing.com/merchant/3406/requests"

    payload := strings.NewReader("{\"first_name\":\"John\",\"last_name\":\"Doe\",\"amount\":3000.00,\"shipping_address\":{\"street1\":\"15th Fantasy Street\",\"city\":\"New York\",\"state\":\"NY\",\"zipcode\":\"10007\"},\"billing_address\":{\"street1\":\"15th Fantasy Street\",\"city\":\"New York\",\"state\":\"NY\",\"zipcode\":\"10007\"},\"email\":\"johndoe.25742gf@gmail.com\",\"cart_items\":[{\"sku\":\"ref03243\",\"display_name\":\"Samsung TV 9340 DMX\",\"quantity\":1,\"unit_price\":2500,\"unit_tax\":300.00}],\"shipping_amount\":200.00,\"version\":\"1.9\", \"merchant_transaction_id\":\"order_1234\"}")

    req, _ := http.NewRequest("POST", url, payload)

    req.Header.Add("authorization", "Basic Y2R4X2FtZXJpY2Fuc3Bpcml0aW5zdGl0dXRlMTp0aGFlLmM2WA==")
    req.Header.Add("accept", "application/json")
    req.Header.Add("content-type", "application/json")
    req.Header.Add("cache-control", "no-cache")

    res, _ := http.DefaultClient.Do(req)

    defer res.Body.Close()
    body, _ := ioutil.ReadAll(res.Body)

    fmt.Println(res)
    fmt.Println(string(body))

}

Java

Using OkHTTPClient:

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\"first_name\":\"John\",\"last_name\":\"Doe\",\"amount\":3000.00,\"shipping_address\":{\"street1\":\"15th Fantasy Street\",\"city\":\"New York\",\"state\":\"NY\",\"zipcode\":\"10007\"},\"billing_address\":{\"street1\":\"15th Fantasy Street\",\"city\":\"New York\",\"state\":\"NY\",\"zipcode\":\"10007\"},\"email\":\"johndoe.25742gf@gmail.com\",\"cart_items\":[{\"sku\":\"ref03243\",\"display_name\":\"Samsung TV 9340 DMX\",\"quantity\":1,\"unit_price\":2500,\"unit_tax\":300.00}],\"shipping_amount\":200.00,\"version\":\"1.9\", \"merchant_transaction_id\":\"order_1234\"}");
Request request = new Request.Builder()
  .url("https://api.sandbox.wegetfinancing.com/merchant/3406/requests")
  .post(body)
  .addHeader("authorization", "Basic Y2R4X2FtZXJpY2Fuc3Bpcml0aW5zdGl0dXRlMTp0aGFlLmM2WA==")
  .addHeader("accept", "application/json")
  .addHeader("content-type", "application/json")
  .addHeader("cache-control", "no-cache")
  .build();

Response response = client.newCall(request).execute();

Using Unirest:

HttpResponse<String> response = Unirest.post("https://api.sandbox.wegetfinancing.com/merchant/3406/requests")
  .header("authorization", "Basic Y2R4X2FtZXJpY2Fuc3Bpcml0aW5zdGl0dXRlMTp0aGFlLmM2WA==")
  .header("accept", "application/json")
  .header("content-type", "application/json")
  .header("cache-control", "no-cache")
  .body("{\"first_name\":\"John\",\"last_name\":\"Doe\",\"amount\":3000.00,\"shipping_address\":{\"street1\":\"15th Fantasy Street\",\"city\":\"New York\",\"state\":\"NY\",\"zipcode\":\"10007\"},\"billing_address\":{\"street1\":\"15th Fantasy Street\",\"city\":\"New York\",\"state\":\"NY\",\"zipcode\":\"10007\"},\"email\":\"johndoe.25742gf@gmail.com\",\"cart_items\":[{\"sku\":\"ref03243\",\"display_name\":\"Samsung TV 9340 DMX\",\"quantity\":1,\"unit_price\":2500,\"unit_tax\":300.00}],\"shipping_amount\":200.00,\"version\":\"1.9\", \"merchant_transaction_id\":\"order_1234\"}")
  .asString();

NodeJS

var http = require("https");

var options = {
  "method": "POST",
  "hostname": "api.sandbox.wegetfinancing.com",
  "port": null,
  "path": "/merchant/3406/requests",
  "headers": {
    "authorization": "Basic Y2R4X2FtZXJpY2Fuc3Bpcml0aW5zdGl0dXRlMTp0aGFlLmM2WA==",
    "accept": "application/json",
    "content-type": "application/json",
    "cache-control": "no-cache",
  }
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.write(JSON.stringify({ first_name: 'John',
  last_name: 'Doe',
  amount: 3000,
  shipping_address:
   { street1: '15th Fantasy Street',
     city: 'New York',
     state: 'NY',
     zipcode: '10007' },
  billing_address:
   { street1: '15th Fantasy Street',
     city: 'New York',
     state: 'NY',
     zipcode: '10007' },
  email: 'johndoe.25742gf@gmail.com',
  cart_items:
   [ { sku: 'ref03243',
       display_name: 'Samsung TV 9340 DMX',
       quantity: 1,
       unit_price: 2500,
       unit_tax: 300 } ],
  shipping_amount: 200,
  version: '1.9',
  merchant_transaction_id: 'order_1234' }));
req.end();

Objective-C

#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"authorization": @"Basic Y2R4X2FtZXJpY2Fuc3Bpcml0aW5zdGl0dXRlMTp0aGFlLmM2WA==",
                           @"accept": @"application/json",
                           @"content-type": @"application/json",
                           @"cache-control": @"no-cache",
NSDictionary *parameters = @{ @"first_name": @"John",
                              @"last_name": @"Doe",
                              @"amount": @3000,
                              @"shipping_address": @{ @"street1": @"15th Fantasy Street", @"city": @"New York", @"state": @"NY", @"zipcode": @"10007" },
                              @"billing_address": @{ @"street1": @"15th Fantasy Street", @"city": @"New York", @"state": @"NY", @"zipcode": @"10007" },
                              @"email": @"johndoe.25742gf@gmail.com",
                              @"cart_items": @[ @{ @"sku": @"ref03243", @"display_name": @"Samsung TV 9340 DMX", @"quantity": @1, @"unit_price": @2500, @"unit_tax": @300 } ],
                              @"shipping_amount": @200,
                              @"version": @"1.9",
                              @"merchant_transaction_id": @"order_1234" };

NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.sandbox.wegetfinancing.com/merchant/3406/requests"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

PHP

<?php

$data = array(
  'first_name' => 'John',
  'last_name' => 'Doe',
  'amount' => 850.25,
  'shipping_address' => array(
    'street1' => '15th Fantasy Street',
    'city' => 'New York',
    'state' => 'NY',
    'zipcode' => '10007'
  ),
  'billing_address' => array(
    'street1' => '15th Fantasy Street',
    'city' => 'New York',
    'state' => 'NY',
    'zipcode' => '10007'
  ),
  'email' => 'johndoe.25742gf@gmail.com',
  'product_info' => 'Mountain Bike ATX 850USD',
  'version' =>'1.9'
);

$json = json_encode($data);

$process = curl_init('https://api.sandbox.wegetfinancing.com/merchant/3406/requests');
curl_setopt($process, CURLOPT_HTTPHEADER, array(
  'Content-Type: application/json',
  'Accept: application/json',
  'Authorization: Basic Y2R4X2FtZXJpY2Fuc3Bpcml0aW5zdGl0dXRlMTp0aGFlLmM2WA=='
));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, $json);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
curl_close($process);
print($return);

?>

Python

This code example makes a very simple POST request using Python. Make sure to have installed the required libraries.

import requests
import json
data = {
    "first_name":"John",
    "last_name":"Doe",
    "amount":850.25,
    "shipping_address":{"street1":"15th Fantasy Street","city":"New York","state":"NY","zipcode":"10007"},
    "billing_address":{"street1":"15th Fantasy Street","city":"New York","state":"NY","zipcode":"10007"},
    "email":"johndoe.25742gf@gmail.com",
    "product_info":"Mountain Bike ATX 850USD",
    "version":"1.9"}
headers = {
 'Content-Type': 'application/json',
 'Accept': 'application/json',
 'Authorization': 'Basic Y2R4X2FtZXJpY2Fuc3Bpcml0aW5zdGl0dXRlMTp0aGFlLmM2WA=='}

url = 'https://api.sandbox.wegetfinancing.com/merchant/3406/requests'
r = requests.post(url, data=json.dumps(data), headers=headers)
print(r.json())

Ruby

require 'uri'
require 'net/http'

url = URI("https://api.sandbox.wegetfinancing.com/merchant/3406/requests")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Post.new(url)
request["authorization"] = 'Basic Y2R4X2FtZXJpY2Fuc3Bpcml0aW5zdGl0dXRlMTp0aGFlLmM2WA=='
request["accept"] = 'application/json'
request["content-type"] = 'application/json'
request["cache-control"] = 'no-cache'
request.body = "{\"first_name\":\"John\",\"last_name\":\"Doe\",\"amount\":3000.00,\"shipping_address\":{\"street1\":\"15th Fantasy Street\",\"city\":\"New York\",\"state\":\"NY\",\"zipcode\":\"10007\"},\"billing_address\":{\"street1\":\"15th Fantasy Street\",\"city\":\"New York\",\"state\":\"NY\",\"zipcode\":\"10007\"},\"email\":\"johndoe.25742gf@gmail.com\",\"cart_items\":[{\"sku\":\"ref03243\",\"display_name\":\"Samsung TV 9340 DMX\",\"quantity\":1,\"unit_price\":2500,\"unit_tax\":300.00}],\"shipping_amount\":200.00,\"version\":\"1.9\", \"merchant_transaction_id\":\"order_1234\"}"

response = http.request(request)
puts response.read_body

Swift

import Foundation

let headers = [
  "authorization": "Basic Y2R4X2FtZXJpY2Fuc3Bpcml0aW5zdGl0dXRlMTp0aGFlLmM2WA==",
  "accept": "application/json",
  "content-type": "application/json",
  "cache-control": "no-cache",
]
let parameters = [
  "first_name": "John",
  "last_name": "Doe",
  "amount": 3000,
  "shipping_address": [
    "street1": "15th Fantasy Street",
    "city": "New York",
    "state": "NY",
    "zipcode": "10007"
  ],
  "billing_address": [
    "street1": "15th Fantasy Street",
    "city": "New York",
    "state": "NY",
    "zipcode": "10007"
  ],
  "email": "johndoe.25742gf@gmail.com",
  "cart_items": [
    [
      "sku": "ref03243",
      "display_name": "Samsung TV 9340 DMX",
      "quantity": 1,
      "unit_price": 2500,
      "unit_tax": 300
    ]
  ],
  "shipping_amount": 200,
  "version": "1.9",
  "merchant_transaction_id": "order_1234"
]

let postData = NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: nil)

var request = NSMutableURLRequest(URL: NSURL(string: "https://api.sandbox.wegetfinancing.com/merchant/3406/requests")!,
                                        cachePolicy: .UseProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.HTTPMethod = "POST"
request.allHTTPHeaderFields = headers
request.HTTPBody = postData

let session = NSURLSession.sharedSession()
let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    println(error)
  } else {
    let httpResponse = response as? NSHTTPURLResponse
    println(httpResponse)
  }
})

dataTask.resume()