public ActionResult Purchase(GroveCM.Models.Order order) { order.OrderDate = System.DateTime.Now; //Save order details to database if (ModelState.IsValid) { orderRepository.SaveOrder(order); } var baseUrl = string.Concat(this.Request.Url.Scheme, "://", this.Request.Url.Authority); try { var guid = Convert.ToString((new Random()).Next(100000)); var cancelUrl = this.Url.Action("PurchaseUnsucessful", "Payment"); var returnUrl = this.Url.Action("PurchaseComplete", "Payment", new { orderNumber = order.OrderNumber.ToLower(), guid = guid }); var payment = paymentRepository.CreatePayment(order, string.Concat(baseUrl, cancelUrl), string.Concat(baseUrl, returnUrl)); if (!payment.WasCreated()) { return this.RedirectToAction("PurchaseUnsucessful"); } // Save order with payment.id somewhere on it TempData["PaymentId"] = payment.id; return this.Redirect(payment.GetApprovalUrl()); } catch (PayPalException ex) { this.ViewBag.Error = ((ConnectionException)ex.InnerException).Response; } catch (Exception ex) { this.ViewBag.Error = ex.Message; } return this.RedirectToAction("purchaseunsucessful"); }
public Payment CreatePayment(GroveCM.Models.Order order, string cancelUrl, string returnUrl) { var amount = CreateAmount(order); var itemList = new ItemList() { items = new List<Item>() }; itemList.items.Add(new Item() { name = order.ItemName, currency = order.Currency, price = order.SubTotal.ToString(), quantity = order.Quantity.ToString(), sku = order.SKU }); var payment = new Payment { transactions = new List<Transaction> { new Transaction { amount = amount, description = "Purchase From Widget Store", item_list = itemList } }, intent = "sale", payer = new Payer { payment_method = "paypal" }, redirect_urls = new RedirectUrls { cancel_url = cancelUrl, return_url = returnUrl } }; payment = payment.Create(Api); return payment; }
private Amount CreateAmount(GroveCM.Models.Order order) { var details = new Details { subtotal = order.SubTotal.ToString("0.00", CultureInfo.CurrentCulture), shipping = order.Shipping.ToString("0.00", CultureInfo.CurrentCulture), tax = order.Tax.ToString("0.00", CultureInfo.CurrentCulture), //fee = order.Fees.ToString("0.00", CultureInfo.CurrentCulture) }; var total = order.SubTotal + order.Shipping + order.Tax; //+ order.Fees; var amount = new Amount { currency = "GBP", details = details, total = total.ToString("0.00", CultureInfo.CurrentCulture) }; return amount; }