/// <summary> /// Purchase credits while adding a new card to the account /// </summary> /// <param name="accountId"></param> /// <param name="dollarAmount"></param> /// <param name="creditCard"></param> /// <returns></returns> /* * public static DataAccessResponseType BuyCredits(string accountId, int dollarAmount, NewCreditCard creditCard) * { * //Add/Update Card to the account, get back StripeCustomerID * var addUpdateNewCreditCardResponse = AccountManager.AddUpdateCreditCard(); * * if(addUpdateNewCreditCardResponse.isSuccess) * { * //if successful, make the transaction * return ExecuteCreditsTransaction(accountId, dollarAmount); * } * else * { * return addUpdateNewCreditCardResponse; * } * * }*/ #region Shared Private Methods private static DataAccessResponseType ExecuteCreditsTransaction(string accountId, decimal dollarAmount) { //Convert dollarAmount to credits var creditsToAdd = Common.Methods.Commerce.ConvertDollarAmountToCredits(dollarAmount); var dollarAmountStripeInteger = Sahara.Core.Common.Methods.Billing.ConvertDecimalToStripeAmount(dollarAmount); var account = AccountManager.GetAccount(accountId); //Make sure account has a StripeCustomerId if (account.StripeCustomerID == null) { return(new DataAccessResponseType { isSuccess = false, ErrorMessage = "Accont does not have a customerId for Stripe." }); } //Create invoice and charge the account; var stripeManager = new StripeManager(); var stripeCustomerId = account.StripeCustomerID; var chargeDescription = "Purchase of " + creditsToAdd + " credits"; var chargeAccountResponse = stripeManager.ChargeAccount(account.StripeCustomerID, chargeDescription, dollarAmountStripeInteger); if (!chargeAccountResponse.isSuccess) { //If unsuccesful, return the result: return(chargeAccountResponse); } //If successful store into SQL bool sqlResult = Sql.Statements.UpdateStatements.UpdateAccountCredits(accountId, creditsToAdd); if (!sqlResult) { //Log issue so that credits can be added manually } //Log transaction into Azure Tables for the account //Partitions: PurchasedCredits, SpentCredits //TODO //Refresh cache: RefreshCreditsCache(accountId); return(new DataAccessResponseType { isSuccess = true }); }