/// <summary> /// Multi item checkout using a cart of items, these are shown on PayPal when making the purchase /// </summary> public ActionResult CheckOutCart() { ApplicationCart cart = new ApplicationCart { Id = Guid.NewGuid(), // Unique cart Id Currency = "USD", PurchaseDescription = "Tickets", // This is what appears in the user's PayPal order history, not the individual items Items = new List <ApplicationCartItem>() { new ApplicationCartItem { Quantity = 1, Price = 5.00M, Name = "Main Event Ticket", Description = "The Main Event you've been waiting to see." }, new ApplicationCartItem { Quantity = 2, Price = 2.00M, Name = "Gun Show Ticket", // Description = "Optional for each item" } } }; Session["Cart"] = cart; return(View("CheckOut", cart)); }
public ActionResult ConfirmPayPalPayment() { WebUILogging.LogMessage("Express Checkout Confirmation"); ApplicationCart cart = (ApplicationCart)Session["Cart"]; return(View(cart)); }
public ActionResult PostPaymentSuccess() { WebUILogging.LogMessage("Post Payment Result: Success"); ApplicationCart cart = (ApplicationCart)Session["Cart"]; ViewBag.TrackingReference = cart.Id; ViewBag.Description = cart.PurchaseDescription; ViewBag.TotalCost = cart.TotalPrice; ViewBag.Currency = cart.Currency; return(View()); }
/// <summary> /// Simple single object checkout, the description is shown on PayPal when making the purchase /// </summary> public ActionResult CheckOut() { ApplicationCart cart = new ApplicationCart { Id = Guid.NewGuid(), // Unique purchase Id Currency = "USD", PurchaseDescription = "Left Handed Screwdriver", TotalPrice = 5.00M // This does NOT get shown to the user on PayPal when making a single object purchase, but is charged and appears in their order history }; // Storing this in session, you might want to store in it a database Session["Cart"] = cart; return(View(cart)); }
public SetExpressCheckoutResponse SendPayPalSetExpressCheckoutRequest(ApplicationCart cart, string serverURL, string userEmail = null) { try { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; ServicePointManager.DefaultConnectionLimit = 9999; WebUILogging.LogMessage("SendPayPalSetExpressCheckoutRequest"); // Optional handling of cart items: If there is only a single item being sold we don't need a list of expressCheckoutItems // However if you're selling a single item as a sale consider also adding it as an ExpressCheckoutItem as it looks better once you get to PayPal's site // Note: ExpressCheckoutItems are currently NOT stored by PayPal against the sale in the users order history so you need to keep your own records of what items were in a cart List <ExpressCheckoutItem> expressCheckoutItems = null; if (cart.Items != null) { expressCheckoutItems = new List <ExpressCheckoutItem>(); foreach (ApplicationCartItem item in cart.Items) { expressCheckoutItems.Add(new ExpressCheckoutItem(item.Quantity, item.Price, item.Name, item.Description)); } } SetExpressCheckoutResponse response = _payPalTransactionRegistrar.SendSetExpressCheckout(cart.Currency, cart.TotalPrice, cart.PurchaseDescription, cart.Id.ToString(), serverURL, expressCheckoutItems, userEmail); // Add a PayPal transaction record PayPalTransaction transaction = new PayPalTransaction { RequestId = response.RequestId, TrackingReference = cart.Id.ToString(), RequestTime = DateTime.Now, RequestStatus = response.ResponseStatus.ToString(), TimeStamp = response.TIMESTAMP, RequestError = response.ErrorToString, Token = response.TOKEN, }; // Store this transaction in your Database return(response); } catch (Exception ex) { WebUILogging.LogException(ex.Message, ex); } return(null); }
public ActionResult PayPalExpressCheckout() { WebUILogging.LogMessage("Express Checkout Initiated"); // SetExpressCheckout ApplicationCart cart = (ApplicationCart)Session["Cart"]; string serverURL = HttpContext.Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/"); SetExpressCheckoutResponse transactionResponse = transactionService.SendPayPalSetExpressCheckoutRequest(cart, serverURL); // If Success redirect to PayPal for user to make payment if (transactionResponse == null || transactionResponse.ResponseStatus != PayPalMvc.Enums.ResponseType.Success) { SetUserNotification("Sorry there was a problem with initiating a PayPal transaction. Please try again and contact an Administrator if this still doesn't work."); string errorMessage = (transactionResponse == null) ? "Null Transaction Response" : transactionResponse.ErrorToString; WebUILogging.LogMessage("Error initiating PayPal SetExpressCheckout transaction. Error: " + errorMessage); return(RedirectToAction("Error", "Purchase")); } return(Redirect(string.Format(PayPalMvc.Configuration.Current.PayPalRedirectUrl, transactionResponse.TOKEN))); }
public ActionResult ConfirmPayPalPayment(bool confirmed = true) { WebUILogging.LogMessage("Express Checkout Confirmed"); ApplicationCart cart = (ApplicationCart)Session["Cart"]; // DoExpressCheckoutPayment string token = TempData["token"].ToString(); string payerId = TempData["payerId"].ToString(); DoExpressCheckoutPaymentResponse transactionResponse = transactionService.SendPayPalDoExpressCheckoutPaymentRequest(cart, token, payerId); if (transactionResponse == null || transactionResponse.ResponseStatus != PayPalMvc.Enums.ResponseType.Success) { if (transactionResponse != null && transactionResponse.L_ERRORCODE0 == "10486") { // Redirect user back to PayPal in case of Error 10486 (bad funding method) // https://www.x.com/developers/paypal/documentation-tools/how-to-guides/how-to-recover-funding-failure-error-code-10486-doexpresscheckout WebUILogging.LogMessage("Redirecting User back to PayPal due to 10486 error (bad funding method - typically an invalid or maxed out credit card)"); return(Redirect(string.Format(PayPalMvc.Configuration.Current.PayPalRedirectUrl, token))); } SetUserNotification("Sorry there was a problem with taking the PayPal payment, so no money has been transferred. Please try again and contact an Administrator if this still doesn't work."); string errorMessage = (transactionResponse == null) ? "Null Transaction Response" : transactionResponse.ErrorToString; WebUILogging.LogMessage("Error initiating PayPal DoExpressCheckoutPayment transaction. Error: " + errorMessage); return(RedirectToAction("Error", "Purchase")); } if (transactionResponse.PaymentStatus == PaymentStatus.Completed) { payoutFunction(cart); return(RedirectToAction("PostPaymentSuccess")); } else { // Something went wrong or the payment isn't complete WebUILogging.LogMessage("Error taking PayPal payment. Error: " + transactionResponse.ErrorToString + " - Payment Error: " + transactionResponse.PaymentErrorToString); TempData["TransactionResult"] = transactionResponse.PAYMENTREQUEST_0_LONGMESSAGE; return(RedirectToAction("PostPaymentFailure")); } }
public DoExpressCheckoutPaymentResponse SendPayPalDoExpressCheckoutPaymentRequest(ApplicationCart cart, string token, string payerId) { try { WebUILogging.LogMessage("SendPayPalDoExpressCheckoutPaymentRequest"); DoExpressCheckoutPaymentResponse response = _payPalTransactionRegistrar.SendDoExpressCheckoutPayment(token, payerId, cart.Currency, cart.TotalPrice); // Add a PayPal transaction record PayPalTransaction transaction = new PayPalTransaction { RequestId = response.RequestId, TrackingReference = cart.Id.ToString(), RequestTime = DateTime.Now, RequestStatus = response.ResponseStatus.ToString(), TimeStamp = response.TIMESTAMP, RequestError = response.ErrorToString, Token = response.TOKEN, RequestData = response.ToString, PaymentTransactionId = response.PaymentTransactionId, PaymentError = response.PaymentErrorToString, }; // Store this transaction in your Database return(response); } catch (Exception ex) { WebUILogging.LogException(ex.Message, ex); } return(null); }
public void payoutFunction(ApplicationCart cart) { // ### Api Context // Pass in a `APIContext` object to authenticate // the call and to send a unique request id // (that ensures idempotency). The SDK generates // a request id if you do not pass one explicitly. // See [Configuration.cs](/Source/Configuration.html) to know more about APIContext. var apiContext = PaypalConfiguration.GetAPIContext(); // ### Initialize `Payout` Object // Initialize a new `Payout` object with details of the batch payout to be created. var payout = new Payout { // #### sender_batch_header // Describes how the payments defined in the `items` array are to be handled. sender_batch_header = new PayoutSenderBatchHeader { sender_batch_id = "batch_" + System.Guid.NewGuid().ToString().Substring(0, 8), email_subject = "You have a payment" }, // #### items // The `items` array contains the list of payout items to be included in this payout. // If `syncMode` is set to `true` when calling `Payout.Create()`, then the `items` array must only // contain **one** item. If `syncMode` is set to `false` when calling `Payout.Create()`, then the `items` // array can contain more than one item. items = new List <PayoutItem> { new PayoutItem { recipient_type = PayoutRecipientType.EMAIL, amount = new Currency { value = cart.TotalPrice.ToString(), currency = cart.Currency }, receiver = "*****@*****.**", note = "Thank you for shopping.", sender_item_id = "item_1" }, //new PayoutItem //{ // recipient_type = PayoutRecipientType.EMAIL, // amount = new Currency // { // value = "7", // currency = "USD" // }, // receiver = "*****@*****.**", // note = "Thank you for coming.", // sender_item_id = "item_2" //}, //new PayoutItem //{ // recipient_type = PayoutRecipientType.EMAIL, // amount = new Currency // { // value = "2.00", // currency = "USD" // }, // receiver = "ng-facilitator_api1.narola.email", // note = "Thank you.", // sender_item_id = "item_3" //} } }; // ### Payout.Create() // Creates the batch payout resource. // `syncMode = false` indicates that this call will be performed **asynchronously**, // and will return a `payout_batch_id` that can be used to check the status of the payouts in the batch. // `syncMode = true` indicates that this call will be performed **synchronously** and will return once the payout has been processed. // > **NOTE**: The `items` array can only have **one** item if `syncMode` is set to `true`. var createdPayout = payout.Create(apiContext); PayoutBatch payoutPayment = Payout.Get(apiContext, createdPayout.batch_header.payout_batch_id); }