Creating a Purchase
Use this tutorial to edit:
- Create a purchase on a customer
- Override any default values from the catalog (Optional)
- Finalize the Purchase
Step 1 - Create a purchase on the customer
POST Purchases
To post a purchase, a customerId, productId, and name must be provided. The resulting purchase will be in a draft state.
Sample Request
curl -X POST "https://stg-secure.fusebill.com/v1/Purchases" \
-H "Content-Type: application/json" \
-H "Authorization: Basic {APIKey}" \
-d "{customerId:{CustomerId},productId:{ProductId},name:'Name',quantity:3.0}"//Json Payload
string jsonData = "{customerId:{CustomerId}, productId:{ProductId}, name:'purchase', quantity:3.0}";
//Setup API key
string apiKey = "{APIKey}";
//Configure URI
WebRequest request = WebRequest.Create("HTTPS://secure.fusebill.com/v1/Purchases");
//Add Content type
request.ContentType = "application/json";
//Add Api key authorization
request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + apiKey);
//Set request method
request.Method = "POST";
//Add the json data to request
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(jsonData);
streamWriter.Flush();
streamWriter.Close();
}
//Perform the request
var httpResponse = (HttpWebResponse)request.GetResponse();
//Record the response from our request
var result = "";
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}import json
import requests
#Pass in a dictionary to the Headers parameter
headers = {'Authorization' : 'Basic {APIKey}', 'Content-Type' : 'application/json'}
#Pass in a dictionary to the Payload parameter
payload = {"customerId":{id},"productId":{id},"name": "product-3"."quantity":2.0}
#Pass in your URI, Payload and Headers
r = requests.post('https://secure.fusebill.com/v1/purchases', data=json.dumps(payload), headers=headers)
print(r.content)Sample Response
{
"customerId": 317725,
"accountId": 98765,
"code": "routermodel123",
"effectiveTimestamp": "2018-03-23T14:55:22.0008836Z",
"taxableAmount": 10.5,
"amount": 10.5,
"productId": 42408,
"name": "Router Purchase",
"description": null,
"status": "Draft",
"quantity": 3,
"isTrackingItems": false,
"pricingModelType": "Standard",
"customFields": [],
"discounts": [],
"priceRanges": [
{
"min": 0,
"max": null,
"amount": 3.5
}
],
"productItems": [],
"couponCodes": [],
"earningSettings": {
"earningInterval": "",
"earningNumberOfIntervals": null,
"earningTimingInterval": "EarnImmediately",
"earningTimingType": "StartOfInterval"
},
"id": 79522,
"uri": "https://secure.fusebill.com/v1/Purchases/79522"
}Step 2 - Override catalog default values
The purchase has now been created using the default values from the product catalog.
To modify a purchase, we must supply the ID of the purchase that is to be modified.
Modify Purchase
Sample Request
This example will change the quantity of the product from 3 to 2, as well as update the price
curl -X PUT "https://stg-secure.fusebill.com/v1/Purchases" \
-H "Content-Type: application/json" \
-H "Authorization: Basic {APIKey}" \
-d "{id:{PurchaseId},customerId:{CustomerId},productId:{ProductId},name:'purchase1updated', overridePriceRanges:[
{
"min": 0,
"max": null,
"amount": 375.0
}]}"//Json Payload
string jsonData = "{id:{PurchaseId},customerId:{CustomerId},productId:{ProductId},name:'purchase1updated', overridePriceRanges:[{'min': 0, 'max': null, 'amount': 375.0 }]}";
//Setup API key
string apiKey = "{APIKey}";
//Configure URI
WebRequest request = WebRequest.Create("HTTPS://secure.fusebill.com/v1/Purchases");
//Add Content type
request.ContentType = "application/json";
//Add Api key authorization
request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + apiKey);
//Set request method
request.Method = "PUT";
//Add the json data to request
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(jsonData);
streamWriter.Flush();
streamWriter.Close();
}
//Perform the request
var httpResponse = (HttpWebResponse) request.GetResponse();
//Record the response from our request
var result = "";
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}import json
import requests
#Pass in a dictionary to the Headers parameter
headers = {'Authorization' : 'Basic {APIKey}', 'Content-Type' : 'application/json'}
#Pass in a dictionary to the Payload parameter
payload =
{"customerId":{id},"productId":{id},"name":"purchaseUpdated","description":None,"status":"Draft","quantity":1,"isTrackingItems":"false","pricingModelType":"Standard","customFields":[],"discounts":[],"priceRanges":[{"min":0.000000,"max":None,"amount":99.000000}],"productItems":[],"couponCodes":[],"earningSettings":{"earningInterval":"","earningNumberOfIntervals":None,"earningTimingInterval":"EarnImmediately","earningTimingType":"StartOfInterval"},"id":{id},"uri":"https://stg-secure.fusebill.com/v1/purchases/{id}"}
#Pass in your URI, Payload and Headers
r = requests.put('https://stg-secure.fusebill.com/v1/purchases', data=json.dumps(payload), headers=headers)
print(r.content)Sample Response
{
"customerId": 317725,
"accountId": 98765,
"code": "routermodel123",
"effectiveTimestamp": "2018-03-23T14:55:22",
"taxableAmount": 7,
"amount": 7,
"productId": 42408,
"name": "Router Purchase",
"description": null,
"status": "Draft",
"quantity": 2,
"isTrackingItems": false,
"pricingModelType": "Standard",
"customFields": [],
"discounts": [],
"priceRanges": [
{
"min": 0,
"max": null,
"amount": 3.5
}
],
"productItems": [],
"couponCodes": [],
"earningSettings": {
"earningInterval": "",
"earningNumberOfIntervals": null,
"earningTimingInterval": "EarnImmediately",
"earningTimingType": "StartOfInterval"
},
"id": 79522,
"uri": "https://secure.fusebill.com/v1/Purchases/79522"
}Step 3 - Finalize the Purchase
The purchase can be finalized with the call below.
Finalize PurchaseWhen a purchase is finalized with
previewset to false, the response body is null. A successful call would be indicated by the 200 OK status. Ifpreviewis set to True, the resulting invoice object will be returned.
URL Parameters
| Property | Type | Description |
|---|---|---|
preview | Boolean | If preview is set to True, the resulting invoice object will be returned. |
temporarilyDisableAutoPost | Boolean | This will specify whether or not the resulting invoice from this call will post the related invoice or not. |
sample request
curl -X POST "https://secure.fusebill.com/v1/Purchases/Purchase?preview=true&showZeroDollarCharges=true&temporarilyDisableAutoPost=true" \
-H "Content-Type: application/json" \
-H "Authorization: Basic {APIKey}" \
-d "{customerId:{CustomerId},id:[{PurchaseId1},{PurchaseId2}]}"//Json Payload
string jsonData = "{customerId:{CustomerId},id:[{PurchaseId1},{PurchaseId2}]}";
//Setup API key
string apiKey = "{APIKey}";
//Configure URI
WebRequest request = WebRequest.Create("HTTPS://secure.fusebill.com/v1/Purchases/Purchase?preview=true&showZeroDollarCharges=true&temporarilyDisableAutoPost=true");
//Add Content type
request.ContentType = "application/json";
//Add Api key authorization
request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + apiKey);
//Set request method
request.Method = "POST";
//Add the json data to request
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(jsonData);
streamWriter.Flush();
streamWriter.Close();
}
//Perform the request
var httpResponse = (HttpWebResponse)request.GetResponse();
//Record the response from our request
var result = "";
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}#Import library JSON
import json
#Import library Requests
import requests
#Pass in a dictionary to the Headers parameter
headers = {'Authorization' : 'Basic {APIKey}', 'Content-Type' : 'application/json'}
#Pass in a dictionary to the Payload parameter
payload = {"customerId":{id},"purchaseIds":[{id}]}
#Pass in your URI, Payload and Headers
r = requests.post('https://secure.fusebill.com/v1/Purchases/Purchase?preview=true&showZeroDollarCharges=true&temporarilyDisableAutoPost=true',
data=json.dumps(payload), headers=headers)
print(r.content)sample response
{
"invoicePreview":{
"draftCharges":[
{
"transactionId":0,
"chargeTypeId":null,
"transactionType":"Purchase",
"quantity":1.0,
"unitPrice":1.00,
"amount":1.00,
"taxableAmount":1.00,
"subscriptionPeriodId":0,
"draftInvoiceId":0,
"name":"Manual Charge",
"description":"One off charges",
"effectiveTimestamp":"2017-02-13T18:54:52.0009642Z",
"proratedUnitPrice":null,
"startServiceDate":"2017-02-13T18:54:52.0009642Z",
"endServiceDate":"2017-02-13T18:54:52.0009642Z",
"rangeQuantity":null,
"draftDiscount":null,
"draftDiscounts":[
],
"status":"Active",
"productItems":null,
"canProrateCharge":false,
"postable":true,
"id":0,
"uri":null
}
],
"subtotal":1.00,
"total":1.00,
"draftTaxes":[
],
"totalTaxes":0.00,
"totalDiscount":0.0,
"poNumber":null,
"effectiveTimestamp":"0001-01-01T00:00:00",
"status":"Ready",
"draftChargeGroups":[
{
"name":"Purchases",
"description":null,
"reference":null,
"draftCharges":[
{
"transactionId":0,
"chargeTypeId":null,
"transactionType":"Purchase",
"quantity":1.0,
"unitPrice":1.00,
"amount":1.00,
"taxableAmount":1.00,
"subscriptionPeriodId":0,
"draftInvoiceId":0,
"name":"Manual Charge",
"description":"One off charges",
"effectiveTimestamp":"2017-02-13T18:54:52.0009642Z",
"proratedUnitPrice":null,
"startServiceDate":"2017-02-13T18:54:52.0009642Z",
"endServiceDate":"2017-02-13T18:54:52.0009642Z",
"rangeQuantity":null,
"draftDiscount":null,
"draftDiscounts":[
],
"status":"Active",
"productItems":null,
"canProrateCharge":false,
"postable":true,
"id":0,
"uri":null
}
]
}
],
"notes":null,
"shippingInstructions":null,
"openingArBalance":16.4600,
"closingArBalance":17.4600
}
}{
"ErrorId": 0,
"HttpStatusCode": 400,
"Errors": [
{
"Key": "Api Error",
"Value": "Unable to complete purchase. The following items do not exist: 123"
}
]
}Updated 4 months ago
