/// <summary> /// Creates a basket and pushes a product request to Pushpay /// </summary> /// <param name="products"></param> /// <returns></returns> public async Task <ActionResult> SaveOrder(string products, string merchantKey) { if (string.IsNullOrWhiteSpace(merchantKey)) { throw new UserException("Please select a merchant"); } // Open our cart var cart = new ShoppingCart(); // The products are just strung together like |product1=3|product2=6.7| etc etc foreach (string productDesc in products.Split('|')) { string[] parts = productDesc.Split('='); if (parts.Length != 2) { continue; } int productID = int.Parse(parts[0]); double quantity = double.Parse(parts[1]); cart.UpdateQuantity(productID, quantity); } // Now process and redirect to our URL AnticipatedPaymentRepresentation paymentResponse = await cart.FinalizePayment(merchantKey); return(Json(paymentResponse, JsonRequestBehavior.AllowGet)); }
/// <summary> /// Calculates the payment info via Pushpay, then redirects the user to the secure URL where they can enter their /// payment details /// </summary> public async Task <AnticipatedPaymentRepresentation> FinalizePayment(string merchantKey) { // Create a model for PP var paymentDetails = new EditAnticipatedPaymentModel(); paymentDetails.Payer = new PayerDetails { EmailAddress = "*****@*****.**", FullName = "Pushpay API" }; // The amount is stored in a config field paymentDetails.Fields.Add(new FieldConfigModel { Key = "amount", Value = GrandTotal.ToString("0.00"), ReadOnly = true }); // Other info paymentDetails.Description = GetBasketContentsDescription(); paymentDetails.DescriptionTitle = "Fruit purchase"; paymentDetails.MerchantKey = merchantKey; paymentDetails.Reference = Guid.NewGuid().ToString(); // Would typically be your own invoice/reference number according to your e-commerce shopping cart paymentDetails.ReturnTitle = "Return to the example site"; paymentDetails.ReturnUrl = new WebEnvironment().GetFullUrl("home/paymentcomplete", true); // Send to pushpay var connection = new PushpayConnection(); AnticipatedPaymentRepresentation response = await connection.SendPaymentToPushpay(paymentDetails); return(response); }
/// <summary> /// Returns information about the requested payment /// </summary> /// <param name="anticipatedPaymentToken"></param> /// <returns></returns> public async Task <AnticipatedPaymentRepresentation> GetPaymentInfo(string anticipatedPaymentToken) { ApiClient client = await CreateClient(); AnticipatedPaymentRepresentation result = await client.Init("anticipatedpayment/" + anticipatedPaymentToken, "Loading payment information").SetMethod(RequestMethodTypes.GET).Execute <AnticipatedPaymentRepresentation>(); return(result); }
/// <summary> /// Sends the payment details to Pushpay, which returns with a URL where we redirect the user for payment /// </summary> /// <param name="paymentDetails"></param> /// <returns></returns> public async Task <AnticipatedPaymentRepresentation> SendPaymentToPushpay(EditAnticipatedPaymentModel paymentDetails) { ApiClient client = await CreateClient(); AnticipatedPaymentRepresentation result = await client.Init("anticipatedpayments", "Sending payment to Pushpay").SetMethod(RequestMethodTypes.POST).SetContent(paymentDetails).Execute <AnticipatedPaymentRepresentation>(); return(result); }