Skip to main content

Credit System

buildfire.services.credits#

API accessible via the Buildfire plugin SDK that allows plugin developers to add and manage user credits. Credits can be managed and consumed via the API’s for in-app purchases.

Notes
  • The user must be logged in and authenticated for credit-system API calls to succeed.
  • Bundles are configured through the Control Panel side of a Buildfire app and are not accessible through the SDK API.

The following link is an example of how to create a plugin with the Buildfire credit-system API. https://github.com/BuildFire/simpleBuildFireJSExamples/tree/master/examplePluginCreditSystem

Examples included in demo plugin:

  • Integration with the credit-system on the control panel side to add bundles.
  • A complete example of UI integrated with the credit-system API.

Requirements#

Widget#

Include credits.js file in widget header right after the buildfire.min.js

<head>
<!-- ... -->
<script src="../../../scripts/buildfire/buildfire.min.js"></script>
<script src="../../../scripts/buildfire/services/credits/credits.js"></script>
</head>

Control#

Include credits.js file in control header right after the buildfire.min.js

<head>
<!-- ... -->
<script src="../../../../scripts/buildfire/buildfire.min.js"></script>
<script src="../../../../scripts/buildfire/services/credits/credits.js"></script>
</head>

Methods#

getBundles()
#

buildfire.services.credits.getBundles(callback)

This method will return all bundles available.

buildfire.services.credits.getBundles((err, bundles) => {
if (err) return console.err(err);
console.log("Credit bundles", bundles);
});

callback(err, bundles)#

Callback function thta will be called after all bundles have been retrieved.

NameTypeDescription
errstringerror string, null when operation is successfull
bundles[object]Array of bundles

purchaseBundle()
#

buildfire.services.credits.purchaseBundle(options, callback)

This method will facilitate the purchase of a bundle. When you call it your chosen payment provider will be called and credits will be deducted from the users balance within the credit system.

buildfire.services.credits.purchaseBundle(
{ bundleId: "my_bundle_id" },
(err, result) => {
if (err) return console.err(err);
console.log("Purchase bundle result", result);
}
);

options#

NameTypeRequiredDescriptionDefault
bundleIdstringyesId of the bundle the user is purchasing
memostringno
xRef1stringno
xRef2stringno
xRef3stringno

callback(err, result)#

Callback function that will be invoked after purchasing the bundle has been completed.

NameTypeDescription
errstringerror string, null when operation is successfull
resultobjectPurchasing bundle result

consumeCredits()
#

buildfire.services.credits.consumeCredits(options, callback)

Calling this method will subtract credits from the balance of a user.

buildfire.services.credits.consumeCredits(
{ creditAmount: 10 },
(err, result) => {
if (err) return console.err(err);
console.log("Credits consumed", result);
}
);

options#

NameTypeRequiredDescriptionDefault
creditAmountnumberyesNumberAmount of credit you would like to subtract from users account.
memostringno
xRef1stringno
xRef2stringno
xRef3stringno

callback(err, data)#

NameTypeDescription
errstringerror string, null when operation is successfull
dataobject

getUser()
#

buildfire.services.credits.getUser(callback)

This method will retrieve the details of a users account.

buildfire.services.credits.getUser((err, user) => {
if (err) return console.err(err);
console.log("User", user);
});

callback(err, user)#

Will be invoked after user info is available.

NameTypeDescription
errstringerror string, null when operation is successfull
userobjectUser object

getUserTransactions()
#

buildfire.services.credits.getUserTransactions(options, callback)

This method returns the history of transactions for the currently logged in user.

buildfire.services.credits.getUserTransactions(
{ pageSize: 20, pageIndex: 0 },
(err, transactions) => {
if (err) return console.err(err);
console.log("User transactions", transactions);
}
);

options#

NameTypeRequiredDescriptionDefault
pageSizenumberyesIndicates the amount of results to return per call.
pageIndexnumberyesPage number to return

callback(err, transactions)#

Callback function after transactions are fetched

NameTypeDescription
errstringerror string, null when operation is successfull
transactions[object]Array of users transactions

transferCredits()
#

buildfire.services.credits.transferCredits(options, callback)

This method transfer credits from current loggedIn user to another user.

buildfire.services.credits.transferCredits(
{ receiverUserEmail: "johndoe@buildfire.com", creditAmount: 10 },
(err, result) => {
if (err) return console.err(err);
console.log("Transaction result", result);
}
);

options#

NameTypeRequiredDescriptionDefault
receiverUserTokenstringyesThe userId for the receiving user. Required if receiverUserEmail is not present
receiverUserEmailstringyesThe user email for the receiving user. Required if receiverUserToken is not present
creditAmountnumberyesAmount of credits to transfer

callback(err, result)#

NameTypeDescription
errstringerror string, null when operation is successfull
resultobjectTransaction result

showTransactions()
#

buildfire.services.credits.showTransactions()

Show the transactions popup for all users

buildfire.services.credits.showTransactions();