This sample app uses Intel® Identity Services to obtain a 3-legged authentication (3L) token, which, in turn, is used to create and purchase carts. The 3L token exposes the user information to the app, which is required in order to conduct purchases. Anonymous carts can be created with just a 2L token. This document explains the step-by-step procedure for creating 2L and 3L authentications. It also talks about creating carts, obtaining billing information, and purchasing.
Prerequisites
- An input file must be hosted on a server (any online location) in JSON/XML format with sample product data that is available for purchase. For simplicity, host the JavaScript sample (attached), also in the same location.
- You need valid Client Id and Secret obtained when you registered the app on the Intel® Cloud Services Platform beta. For your sample app to work, please replace the Client Id and Secret in the code with the values obtained when you registered the app.
- When you register the app, choose the correct Application Environment in which to create the app. If it is the Test environment, you must create at least one user account in the same environment and use fake credit card details. Follow these steps to create a billing profile in the Test environment:
- Create a Test app and store with your Intel developer account on the cloud services portal:
- Create a user account in Intel Identity Services Test environment:
- Register a user billing profile using your developer’s Client Id and Secret:
- https://test-commerce.intel.com/profile/billingprofile.aspx?apikey=<apikey>
- Be sure to grant billing scope to the store and user profile
- If you plan to use the Production environment, it is important to provide actual credit card information. Real end user accounts must also be created in the Production environment.
- A complete billing profile with credit card information is essential to conduct purchases and calculate taxes on a cart.
- Please refer to this tutorial for creating a digital store.
API Usage and code snippets
Step 1: SDK header inclusion
Include <script src="https://api.intel.com/js/v1/intel.cloudservices.js"></script>
The JS file must be included to incorporate SDK features into your code.
Step 2: 2L authentication
The following code gets the 2L authentication token. It is important to provide user details and billing profile in the scope, i.e., "user:details user:scope profile:full billing:confirm profile:basic commerce:basic"
intel.auth.init({ client_id: <YOUR_CLIENT_ID>, secret_id: <YOUR_SECRET_KEY>, scope: "user:details user:scope profile:full billing:confirm profile:basic commerce:basic" }, Success = function (data) { // data is the 2L authentication token // anonymous carts can be created in this sucesscallback function }, Error = function (data) { // data contains the error object // Error call back function. Implement error handling here } );
Cart creation can be included in the success call back function if the user wishes to create anonymous carts.
Step 3: 3L authentication
The following code snippet obtains the 3L access token, which aids in getting user information and processing purchases:
intel.auth.login({ redirect_uri: "urn:intel:identity:oauth:oob:async", provider: "", name: "myLoginWindow", specs: "location=1,status=1,scrollbars=1,width=700,height=400" }, Success = function (data) { // data is the 3L authentication token // Implement cart save, purchase and get billing profile } Error = function (data){ // data contains the error object // Error call back function. Implement error handling here }
- A successful call back function contains the valid 3L token and any action that follows the successful call back can be defined in that function.
- An error call back function is called when the token is not obtained. It could be due to environment discrepancies (Test and Production) or not specifying the scope correctly.
Step 4: Commerce APIs
Creating a cart
myCart = new intel.commerce.Cart(); cartLineId = myCart.addItem( { name:<ITEM_NAME>, externalProductCode:<PRODUCT_CODE>, quantity:<QUANTITY>, actualPrice:<ACTUAL_PRICE>, commodityCode:<COMMODITY_CODE>, listPrice:<LIST_PRICE> } );
- Values specific to a product can be retrieved from the input JSON file.
- The cart structure includes other items as well. For details on the cart structure, please refer to: http://software.intel.com/en-us/articles/cloud-services-commerce-service-rest-api-reference
- Successful execution of this code returns a cartID (cartLineId in this case) that can be used in the subsequent calls.
Save the cart in the cloud
myCart.save( successCallBack = function () { alert("cart saved to cloud sucessfully"); }, errorCallBack = function () { alert("Error in saving to cloud"); });
- myCart is the name of the cart created in the previous step.
- This code saves the cart to the cloud.
Get billing information
intel.commerce.getBillingProfile (success = function (billingProfile) { alert("Billed in " + billingProfile["data"].items[0].billingCountry); }, errorCallback = function () { alert("Error in obtaining billing profile"); } );
- This call retrieves the billing profile of the consumer which was created when an Intel login was created.
- Only if the scope specifies “user:details” and the user agrees to share this information will this call execute successfully.
- A successful call back function returns the billing profile in JSON format. A sample profile looks like this:
{"data": {"kind":"billingProfiles"," items": [{"billingCountry":"US", "billingCurrency":"USD", "paymentType":"AMEX", "paymentTypeId":3, "confirmationType":"cfm_typ_none"}]}}
Code above has just extracted the billing country from this sample output.
- An error call back function returns the error object in JSON format, which the developer must handle.
Purchase call
myCart.purchase( successPurchaseCallback = function () { alert("Successful purchase"); }, errorCallback = function (data) { alert("Error in purchase"); });
- This call goes through the payment gateway to actually purchase the items in the cart.
- For this call to work, the app must have valid credit card information (fake or real) that it can read from the user billing profile.
- Once the call successfully executes, an OrderID is returned as part of the cart structure, which can be used to track the order, request a refund, and get order information.
Sample JSON output with the orderID looks like this:
"data": { "kind": "cartLine", "items": [ { "id": 1, "name": "Some Name 1", "externalProductCode": "PP-14", "quantity": 1, "actualPrice": 1.99, "commodityCode": "4323.120", "listPrice": 1.99, "lineItemTax": 0.09, "extendedPrice": 1.99 }, { "id": 2, "name": "Some Name 2", "externalProductCode": "PP-13", "quantity": 2, "actualPrice": 2.49, "commodityCode": "551115.400", "listPrice": 2.99, "lineItemTax": 0.22, "extendedPrice": 4.98 } ], "cartId": "f776fd54-e171-42d6-a4ab-2dbf5d376db2", "orderId": "f776fd54-e171-42d6-a4ab-2dbf5d376db2", "currencyType": "USD", "locale": "en-US", "subtotal": 6.97, "isTaxCalculated": 1, "orderTotal": 7.28, "taxTotal": 0.31 "billingCountry": "US", "paymentType": "VISA", "paymentTypeId": "4", "date": "2012-06-26T05:49:36Z" }
Important Note: Be sure to replace the Client Id and Secret in the code with the values you received when you registered your app.
Additional Resources
- Sign-up tutorial for registering and creating app on the cloud services portal
http://software.intel.com/en-us/articles/intel-cloud-service-register-tutorial - Cloud Services SDK sample code snippets
http://software.intel.com/en-us/articles/cloud-services-platform-sdk-sample-snippets - Cloud Services SDK overview video
http://software.intel.com/en-us/cloud-services-sdk - Cloud Services SDK API reference
http://software.intel.com/en-us/articles/cloud-services-sdk-api-reference