public RedirectResult CreatePurchase([FromForm(Name = "recurly-token")] string tokenId, [FromForm(Name = "account-code")] string accountCode, [FromForm(Name = "first-name")] string firstName, [FromForm(Name = "last-name")] string lastName)
        {
            // If our form specifies an account code, we can use that; otherwise,
            // create an account code with a uniq id
            accountCode = accountCode ?? Guid.NewGuid().ToString();

            var purchaseReq = new PurchaseCreate()
            {
                Currency = "USD",
                Account  = new AccountPurchase()
                {
                    Code        = accountCode,
                    FirstName   = firstName,
                    LastName    = lastName,
                    BillingInfo = new BillingInfoCreate()
                    {
                        TokenId = tokenId
                    }
                },
                Subscriptions = new List <SubscriptionPurchase>()
                {
                    new SubscriptionPurchase()
                    {
                        PlanCode = "basic"
                    }
                }
            };

            try
            {
                InvoiceCollection collection = _client.CreatePurchase(purchaseReq);
                _logger.LogInformation($"Created ChargeInvoice with Number: {collection.ChargeInvoice.Number}");
            }
            catch (Recurly.Errors.Transaction e)
            {
                /**
                 * Note: This is not an example of extensive error handling,
                 * it is scoped to handling the 3DSecure error for simplicity.
                 * Please ensure you have proper error handling before going to production.
                 */
                TransactionError transactionError = e.Error.TransactionError;

                if (transactionError != null && transactionError.Code == "three_d_secure_action_required")
                {
                    string actionTokenId = transactionError.ThreeDSecureActionTokenId;
                    return(Redirect($"/3d-secure/authenticate.html#token_id={tokenId}&action_token_id={actionTokenId}&account_code={accountCode}"));
                }

                return(HandleError(e));
            }
            catch (Recurly.Errors.ApiError e)
            {
                return(HandleError(e));
            }

            return(Redirect(SuccessURL));
        }
        public async Task <ActionResult> Create(PurchaseCreate model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var service = CreatePurchaseService();

            if (await service.CreatePurchase(model))
            {
                TempData["SaveResult"] = "The purchase was succesfully made.";
                return(RedirectToAction("Index"));
            }

            ModelState.AddModelError("", "The purchase could not be completed, please try again.");
            return(View(model));
        }
Example #3
0
        public async Task <bool> CreatePurchase(PurchaseCreate model)
        {
            var entity =
                new Purchase()
            {
                OwnerID  = _userId,
                AcctID   = model.AcctID,
                Symbol   = (Purchase.PCryptoSymbolEnum)model.Symbol,
                Quantity = model.Quantity,
                Price    = model.Price,
                //Total = model.Total,
                PurchaseDate = DateTimeOffset.Now
            };

            using (var ctx = new ApplicationDbContext())
            {
                ctx.Purchases.Add(entity);
                return(await ctx.SaveChangesAsync() == 1);
            }
        }
Example #4
0
 public PurchaseCreateBuilder()
 {
     _purchaseCreate = new PurchaseCreate {
         LineItems = new List <LineItemCreate>(), Subscriptions = new List <SubscriptionPurchase>()
     };
 }
Example #5
0
 public InvoiceCollection CreateSubscriptionViaPurchase(PurchaseCreate purchaseCreate)
 {
     return(client.CreatePurchase(purchaseCreate));
 }