Last active
August 29, 2015 14:16
-
-
Save billforward-alex/a6c0058106b997a12e0f to your computer and use it in GitHub Desktop.
How to ascribe initial values for a subscription's pricing components (using BillForward NodeJS SDK)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var BillForward = require('BillForward'); | |
var _ = require('lodash'); // lodash is the strongest | |
var Q = require('q'); | |
var config = { | |
"urlRoot": "https://api-sandbox.billforward.net:443/v1/", | |
"accessToken": "INSERT ACCESS TOKEN HERE", | |
// longStack: true, | |
"requestLogging": true, | |
"responseLogging": false, | |
"errorLogging": true, | |
}; | |
BillForward.Client.makeDefault(config); | |
Q.spread([ | |
BillForward.Account.getAll({records:1}), | |
BillForward.ProductRatePlan.getAll({records:1}), | |
], | |
function(accounts, ratePlans) { | |
// get 'an account' moreorless at random | |
var account = accounts[0]; | |
// get 'a rate plan' moreorless at random | |
var plan = ratePlans[0]; | |
// we can only set initial values for (at least, but not limited to) 'subscription' pricing components | |
var advanceComponents = _.where(plan.pricingComponents, | |
{ | |
'chargeType': 'subscription' // btw 'subscription' is also known as "in-advance" | |
// if you set this to 'usage', we get ServerError! | |
}); | |
var advanceComponentNames = _.pluck(advanceComponents, 'name'); | |
var advanceComponentValues = _.pluck(advanceComponents, 'defaultQuantity'); | |
var componentNameToValueMap = _.zipObject(advanceComponentNames, advanceComponentValues); | |
var subscriptionModel = new BillForward.Subscription({ | |
'productRatePlanID': plan.id, | |
'accountID': account.id, | |
'name': plan.description, | |
'description': plan.description | |
}); | |
return subscriptionModel.setValuesOfPricingComponentsByName(componentNameToValueMap); | |
}) | |
.then(function(subscriptionModel) { | |
return BillForward.Subscription.create(subscriptionModel); | |
}) | |
.then(function(subscription) { | |
console.log(subscription.toString()); | |
}) | |
.catch(function(e) { | |
if (e instanceof BillForward.BFError) // generic parent class for handled exceptions in SDK | |
console.error("Something went wrong."); | |
if (e instanceof BillForward.BFRequestError) | |
console.error("API did not like our request; failed to fulfil it."); | |
if (e instanceof BillForward.BFHTTPError) | |
console.error("Connection problem; maybe your BillForward URL or API endpoint is funky?"); | |
if (e instanceof BillForward.BFInvocationError) | |
console.error("We are invoking BillForward incorrectly."); | |
if (e instanceof BillForward.MalformedAPIResponseError) | |
console.error("Did not receive standard JSON response we expect. Maybe our BillForward URL or API endpoint is funky?"); | |
if (e instanceof BillForward.BFResponseUnserializationFailure) | |
console.error("Did not receive a JSON-formatted entity, when we expected one."); | |
if (e instanceof BillForward.BFUnauthorizedError) | |
console.error("Try logging in :P"); | |
console.error(e.toString(), e.stack); | |
}) | |
.done(); // throws unhandled errors also |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment