Testing the Billing API

I am working on testing the billing.js API with the mock service at mocks/billing-events.json and I am receiving the following error:

Serverless: INVOKING INVOKE
    {
        "errorMessage": "(0 , _billingLib.calculateCost) is not a function",
        "errorType": "TypeError"
    }

I have made a few custom changes to my app, nonetheless it is exactly as specified in the chapter specifications. Below are the different features I have implemented and how I have the logic setup:

here is my billing.js file:

import stripePackage from "stripe";
import { calculateCost } from "./libs/billing-lib";
import { success, failure } from "./libs/response-lib";

export async function main(event, context) {
  // source is credit card token to charge
  // txnType: SubscriptionFee or TransactionFee
  // txnQty: How many txn fees or how many subscription fees
  const {txnType, txnQty, source} = JSON.parse(event.body);
  const amount = calculateCost(txnType, txnQty);

  // TODO: Need condition to change description nased on txnType
  const description = "SpecTruMetRx Monthly Subscription Fees";

  // Load pvtKey from environment variables
  const stripe = stripePackage(process.env.stripePrivateKey);

  try {
    await stripe.charges.create({
      source,
      amount,
      description,
      currency: "usd"
    });
    return success({
      status: true
    });
  } catch ( e ) {
    return failure({
      message: e.message
    });
  }
}

here is my libs/billing-lib.js file:

export function calculcateCost(txnType, txnQty) {
  let fee;
  let amt;
  /* Check the transaction type
   * If: txnType == 'SubscriptionFee':
   * then: Fee == $50
   * return rate = $50 * txnQty (numUsers) every month
   */
  if (txnType === "SubscriptionFee") {
    /* Monthly Subscription Fee per user
     * Each user must be associated to a company
     * Company pays aggregation of monthly
     * subscription fees.
     *
     * A SubscriptionFee is triggered every
     * 30 days starting with the 15th of every
     * month.
     */
    fee = 50.00;
    amt = fee * txnQty;
  }
  /* else if: txnType == 'TransactionFee':
   * then: Fee == $5
   * --> numTxns == the weekly billable time submitted by each Provider
   * i.e. one weekly time submission from a Provider = one Transaction
   * return rate = $5 * txnQty (numTxns)
   */
  else if (txnType === "TransactionFee") {
    /* This is the per transaction fee
     * Each TreatmentLog Entry triggers a
     * TransactionFee Event
     */
    fee = 5.00;
    amt = fee * txnQty;
  }
  return amt;
}

and here is how i have my mocks/billing-event.json file setup:

{
  "body": "{\"txnType\":\"SubscriptionFee\",\"txnQty\":10,\"source\":\"tok_visa\"}",
  "requestContext": {
    "identity": {
      "cognitoIdentityId": "USER-SUB-1234"
    }
  }
}

Also, the lambda is setup in my serverless.yml file as such:

billing:
    handler: billing.main
    events:
      - http:
          path: billing
          method: post
          cors: true
          authorizer: aws_iam

It’s as if the import statement to use the billing-lib.js export function calculateCost() are not being imported properly.

When I look into the .webpack/service/billing.js file in my project directory i see that web pack is trying to require the correct lib with:

var _billingLib = __webpack_require__(/*! ./libs/billing-lib */ "./libs/billing-lib.js");

I also see where web pack is calling the calculate function correctly:

amount = (0, _billingLib.calculateCost)(txnType, txnQty);

what can I do to pinpoint this error further? how can i log statements to the console so I can check to see if the correct data is in the right variables? console.log is not outputting anything to the console right now.

What could be causing this particular problem as I cannot see what I may be doing wrong right now as it looks like I am able to obtain the correct data in the correct variables…

Thanks!!!

ahhh… forget it @jayair I had a misspelled function in my libs… sorry about that. Thanks again!

1 Like