Example #1
0
        } // End of the payex_confirmation method

        #endregion

        #region PayPal payment

        /// <summary>
        /// Create a paypal payment
        /// </summary>
        public ActionResult CreatePayPalPayment(Order order, List<OrderRow> orderRows, Domain domain, KeyStringList tt)
        {
            // Create the string to return
            string error_message = "";

            // Get the currency
            Currency currency = Currency.GetOneById(order.currency_code);

            // Get the webshop settings
            KeyStringList webshopSettings = WebshopSetting.GetAllFromCache();

            // Get credentials
            string paypalClientId = webshopSettings.Get("PAYPAL-CLIENT-ID");
            string paypalClientSecret = webshopSettings.Get("PAYPAL-CLIENT-SECRET");
            string paypalMode = webshopSettings.Get("PAYPAL-MODE");
            Dictionary<string, string> config = new Dictionary<string, string> { { "mode", paypalMode } };

            // Create a payment variable
            PayPal.Api.Payments.Payment createdPayment = null;

            try
            {
                // Create the credential token
                PayPal.OAuthTokenCredential tokenCredential = new PayPal.OAuthTokenCredential(paypalClientId, paypalClientSecret, config);

                // Create the api context
                PayPal.APIContext paypalContext = new PayPal.APIContext(tokenCredential.GetAccessToken());
                paypalContext.Config = config;

                // Create the amount details
                decimal subTotal = order.net_sum + order.rounding_sum - order.gift_cards_amount;
                PayPal.Api.Payments.Details amountDetails = new PayPal.Api.Payments.Details();
                amountDetails.subtotal = subTotal.ToString("F2", CultureInfo.InvariantCulture);
                amountDetails.tax = order.vat_sum.ToString("F2", CultureInfo.InvariantCulture);

                // Create the amount
                decimal totalAmount = order.total_sum - order.gift_cards_amount;
                PayPal.Api.Payments.Amount amount = new PayPal.Api.Payments.Amount();
                amount.total = totalAmount.ToString("F2", CultureInfo.InvariantCulture);
                amount.currency = order.currency_code;
                amount.details = amountDetails;

                // Create a transaction
                PayPal.Api.Payments.Transaction transaction = new PayPal.Api.Payments.Transaction();
                transaction.item_list = new PayPal.Api.Payments.ItemList();
                transaction.item_list.items = new List<PayPal.Api.Payments.Item>(10);

                // Add order rows to the transaction
                for (int i = 0; i < orderRows.Count; i++)
                {
                    // Create a new item
                    PayPal.Api.Payments.Item item = new PayPal.Api.Payments.Item();
                    item.sku = orderRows[i].product_code.Length > 50 ? orderRows[i].product_code.Substring(0, 50) : orderRows[i].product_code;
                    item.name = orderRows[i].product_name.Length > 100 ? orderRows[i].product_name.Substring(0, 50) : orderRows[i].product_name;
                    item.price = orderRows[i].unit_price.ToString("F2", CultureInfo.InvariantCulture);
                    item.quantity = Convert.ToInt32(orderRows[i].quantity).ToString();
                    item.currency = order.currency_code;

                    // Add the item to the list
                    transaction.item_list.items.Add(item);
                }

                // Add the rounding
                if(order.rounding_sum != 0)
                {
                    PayPal.Api.Payments.Item roundingItem = new PayPal.Api.Payments.Item();
                    roundingItem.sku = "rd";
                    roundingItem.name = tt.Get("rounding");
                    roundingItem.price = order.rounding_sum.ToString("F2", CultureInfo.InvariantCulture);
                    roundingItem.quantity = "1";
                    roundingItem.currency = order.currency_code;
                    transaction.item_list.items.Add(roundingItem);
                }

                // Add the gift cards amount
                if (order.gift_cards_amount > 0)
                {
                    decimal giftCardAmount = order.gift_cards_amount * -1;
                    PayPal.Api.Payments.Item giftCardsItem = new PayPal.Api.Payments.Item();
                    giftCardsItem.sku = "gc";
                    giftCardsItem.name = tt.Get("gift_cards");
                    giftCardsItem.price = giftCardAmount.ToString("F2", CultureInfo.InvariantCulture);
                    giftCardsItem.quantity = "1";
                    giftCardsItem.currency = order.currency_code;
                    transaction.item_list.items.Add(giftCardsItem);
                }
                
                // Set the transaction amount
                transaction.amount = amount;
                List<PayPal.Api.Payments.Transaction> transactions = new List<PayPal.Api.Payments.Transaction>();
                transactions.Add(transaction);

                // Create the payer
                PayPal.Api.Payments.Payer payer = new PayPal.Api.Payments.Payer();
                payer.payment_method = "paypal";

                // Create redirect urls
                string hostUrl = Request.Url.Host;
                PayPal.Api.Payments.RedirectUrls redirectUrls = new PayPal.Api.Payments.RedirectUrls();
                redirectUrls.return_url = domain.web_address + "/order/paypal_confirmation/" + order.id;
                redirectUrls.cancel_url = domain.web_address + "/order/confirmation/" + order.id;

                // Create the payment
                PayPal.Api.Payments.Payment payment = new PayPal.Api.Payments.Payment();
                payment.intent = "sale";
                payment.payer = payer;
                payment.redirect_urls = redirectUrls;
                payment.transactions = transactions;

                // Create the payment
                createdPayment = payment.Create(paypalContext);

            }
            catch (Exception ex)
            {
                error_message = ex.Message;
            }

            // Check if there is any errors in the payment
            if (createdPayment != null)
            {
                // Save the paypal payment id
                Order.SetPaymentToken(order.id, createdPayment.id);

                // Get the link
                string link = "";
                foreach(PayPal.Api.Payments.Links url in createdPayment.links)
                {
                    if (url.rel == "approval_url")
                    {
                        link = url.href;
                        break;
                    }
                }

                // Redirect the user to the paypal page
                return Redirect(link);
            }
            else
            {
                // Redirect the user to the order confirmation page
                return RedirectToAction("confirmation", "order", new { id = order.id });
            }

        } // End of the CreatePayPalPayment method
        private Int32 AndNowItIsTimeToTalkToPayPal(Int32 productCost,
                                                   String productDescription,
                                                   String yourname,
                                               out String redirect_approvalUrlFromCreatedPayment)
        {
            // ................................
            //   Miscellaneous Error Messages from PayPal ... *****  "debug_id" is NOT a constant, examples:  "67f86e19d300d",  "36f998b61c785"
            String response_VALIDATION_ERROR =
            "{\"name\":\"VALIDATION_ERROR\",\"details\":[{\"field\":\"transactions[0].amount\",\"issue\":\"Required field missing\"}],\"message\":\"Invalid request - see details\",\"information_link\":\"https://developer.paypal.com/webapps/developer/docs/api/#VALIDATION_ERROR\",\"debug_id\":\"";
            Int32  response_VALIDATION_ERROR_Length = response_VALIDATION_ERROR.Length;
            // https://developer.paypal.com/webapps/developer/docs/api/#VALIDATION_ERROR
            // VALIDATION_ERROR
            // Invalid request                                - see details
            // There was a validation issue with your request - see details
            // ==> https://developer.paypal.com/webapps/developer/docs/api/#validation-issues

            // ................................
            String response_INTERNAL_SERVICE_ERROR =
                 "{\"name\":\"INTERNAL_SERVICE_ERROR\",\"message\":\"An internal service error has occurred\",\"information_link\":\"https://developer.paypal.com/webapps/developer/docs/api/#INTERNAL_SERVICE_ERROR\",\"debug_id\":\"";
            Int32  response_INTERNAL_SERVICE_ERROR_Length = response_INTERNAL_SERVICE_ERROR.Length;
            // https://developer.paypal.com/webapps/developer/docs/api/#INTERNAL_SERVICE_ERROR
            // INTERNAL_SERVICE_ERROR
            // An internal service error has occurred
            // Resend the request at another time. If this error continues,
            //      contact PayPal Merchant Technical Support.
            //      https://developer.paypal.com/webapps/developer/support

            // ................................
            Int32 payPalSuccessLevel = 999; // 999 =. unknown
            redirect_approvalUrlFromCreatedPayment = String.Empty;

            // THINGS WE NEED TO USE THE PAYPAL REST API
            String argumentsPlusPayPalClentID = "AU ... 6G"; // todo   ... your PayPal REST API SDK user ID
            String argumentsPlusPayPalSecret  = "EA ... -f"; // todo   ... your PayPal REST API SDK PASSWORD
            //          String payPalRestApiEndpoint      = "api.sandbox.paypal.com"; // For TESTING !!!
                                           // = "api.paypal.com";         // For LIVE    !!!
            // ................................
            Dictionary<String, String> sdkConfig = new Dictionary<String, String>();
            sdkConfig.Add("mode", "sandbox");
            // ................................
            // POTENTIAL POINT OF FAILURE
            // example:  "Exception in HttpConnection Execute: Invalid HTTP response The operation has timed out"
            String oAuthAccessTokenForPayPal = String.Empty; // scope outside of try/catch
            try
            {
                oAuthAccessTokenForPayPal
                    = new PayPal
                          .OAuthTokenCredential(argumentsPlusPayPalClentID,
                                                argumentsPlusPayPalSecret,
                                                sdkConfig).GetAccessToken();
            }                                          // .GetAccessToken takes us over to PayPal
            catch (Exception)
            {
                throw;  // todo ... make this code more robust
            }
            // ................................
            // We're back from PayPal with our access token.
            PayPal.APIContext apiContext // use the "Bearer" token from PayPal
                = new PayPal.APIContext(oAuthAccessTokenForPayPal);
            apiContext.Config = sdkConfig;
            // ................................
            PayPal.Api.Payments.Amount argumentsPlusPayPalPaymentAmount
                = new PayPal.Api.Payments.Amount();
            // https://developer.paypal.com/docs/classic/api/currency_codes/
            argumentsPlusPayPalPaymentAmount.currency = "GBP"; // UK Pound Sterling; "USD" for US$
            argumentsPlusPayPalPaymentAmount.total = productCost.ToString();
            // ................................
            String description = String.Empty; // 127 character maximum length
            description        = productDescription + " "
                               + DateTime.Now.ToLongDateString()
                               + " -- " + DateTime.Now.ToLongTimeString()
                               + " -- " + productCost + " U.K. pounds"
                               + " -- Thank you " + yourname
                               + " for your purchase.";
            // ................................
            // borrowed code from:  http://msdn.microsoft.com/en-us/library/xwewhkd1(v=vs.110).aspx
            // Regex.Replace Method (String, String)
            // ...  match ALL one or more white-space characters in our description text,
            //      replace EACH instance found with just one space.
            System.Text.RegularExpressions.Regex regexPattern
                        = new System.Text.RegularExpressions.Regex(@"\s+");
            description = regexPattern.Replace(description, " ");
            //  PayPal's HTML will drop extra whilespace, so we do not send any extra whitespace.
            // ................................
            PayPal.Api.Payments.Transaction argumentsSale
                = new PayPal.Api.Payments.Transaction();
            if (description.Length > 127) description = description.Substring(0, 127);
                                       // allow no more that 127 characters (PayPal's Limit)
            argumentsSale.description                 = description;
            argumentsSale.amount = argumentsPlusPayPalPaymentAmount;
            // ................................
            List<PayPal.Api.Payments.Transaction> transactionList
                = new List<PayPal.Api.Payments.Transaction>();
            transactionList.Add(argumentsSale);
            // ................................
            PayPal.Api.Payments.Payer argumentBuyer
                = new PayPal.Api.Payments.Payer();
            // The payment method "paypal" lets the customer choose
            // whether to pay us using a PayPal account
            // or to pay us with their credit card as a PayPal guest.
            argumentBuyer.payment_method        = "paypal";
            // ................................
            // Note:  we need some way to connect PayPal's response
            //        back to our customer ... TIMTOWTDI
            Guid reconnectToOurBuyer = Guid.NewGuid();
            PayPal.Api.Payments.RedirectUrls redirUrls
                = new PayPal.Api.Payments.RedirectUrls();
            String cancelURL = "http://localhost:53326/home/argumentsnotsold/"
                             + reconnectToOurBuyer.ToString("N");  // customer changes her/his mind
            String returnURL = "http://localhost:53326/home/argumentssold/"
                             + reconnectToOurBuyer.ToString("N");  // Hurrah!  A sale!
            redirUrls.cancel_url = cancelURL;
            redirUrls.return_url = returnURL;
            // ................................
            // Next, we create a Payment object to send to PayPal
            PayPal.Api.Payments.Payment getInvoiceFromPayPal = new PayPal.Api.Payments.Payment();
            getInvoiceFromPayPal.intent        = "sale";
            getInvoiceFromPayPal.payer         = argumentBuyer;
            getInvoiceFromPayPal.transactions  = transactionList;
            getInvoiceFromPayPal.redirect_urls = redirUrls;
            // ................................

            PayPal.Api.Payments.Payment createdInvoice = null; // scope outside of try/catch
            try
            {
                // the next line can fail, for example:  http://paradigmmentors.net//files/code/argumentsPlus/500_internal_Server_Error_catch.txt
                createdInvoice = getInvoiceFromPayPal.Create(apiContext); //  <==
                // SUCCESSFUL CREATION
                // we must persist createdInvoice.id (the Payment.id value)
                payPalSuccessLevel = 100; // successful
                SavePayPalInvoice(reconnectToOurBuyer, createdInvoice);
            }

            catch (PayPal.Exception.ConnectionException connectionExceptionPayPal)
             {
                 payPalSuccessLevel = 111;
                if(String.Compare(connectionExceptionPayPal.Message,
                    "Invalid HTTP response The remote server returned an error: (400) Bad Request.")
                   == 0)
                {
                    payPalSuccessLevel += 10000;
                    if(String.Compare(connectionExceptionPayPal.Response.Substring(0, response_VALIDATION_ERROR_Length),
                                      response_VALIDATION_ERROR) == 0)
                    {
                        // todo Take appropriate action
                    }
                    else
                    {
                        payPalSuccessLevel += 20000;
                        // todo Take appropriate action
                    }
                }
                else if(String.Compare(connectionExceptionPayPal.Message,
                    "Invalid HTTP response The remote server returned an error: (500) Internal Server Error.")
                   == 0)
                {
                    payPalSuccessLevel = 500;
                    if (String.Compare(connectionExceptionPayPal.Response.Substring(0, response_INTERNAL_SERVICE_ERROR_Length),
                                       response_INTERNAL_SERVICE_ERROR) == 0)
                    {
                        payPalSuccessLevel += 30000;
                        // todo Take appropriate action
                    }
                    else
                    {
                        payPalSuccessLevel += 40000;
                        // todo Take appropriate action
                    }
                }
                else
                {
                    payPalSuccessLevel = 666;
                    // todo Take appropriate action
                }
            //              throw;        // todo complete
            }
            catch (PayPal.Exception.PayPalException paypalException)
            {
                if (String.Compare(paypalException.Message,
                    "Exception in HttpConnection Execute: Invalid HTTP response The remote server returned an error: (500) Internal Server Error.")
                   == 0)
                {   // the "may" be an inner exception
                    payPalSuccessLevel = 777;
                    if (paypalException.InnerException != null) // we take a chance that the InnerException is PayPal.Exception.ConnectionException
                    {
                        try
                        {
                            PayPal.Exception.ConnectionException payPalInnerException = (PayPal.Exception.ConnectionException)paypalException.InnerException;
                            if (String.Compare(payPalInnerException.Response.Substring(0, response_INTERNAL_SERVICE_ERROR_Length),
                   response_INTERNAL_SERVICE_ERROR) == 0)
                            {
                                payPalSuccessLevel = 30777;       // fine tune our error code
                                // todo Take appropriate action
                            }
                            else
                            {
                                payPalSuccessLevel = 40777;       // fine tune our error code
                                // todo Take appropriate action
                            }
                        }
                        catch (Exception)
                        {
                            payPalSuccessLevel = 50777;           // fine tune our error code
                            // we probably guessed wrong
                            // todo ........... handle appropriately
                        }
                    }
                }
                else
                {
                    payPalSuccessLevel = 888;
                    // todo Take appropriate action
                }
                //              throw;        // todo complete
            }
            catch (Exception genericException)  // todo complete
            {
                // example causes:
                // (a) "No connection string named 'ArgumentPlusContext'
                //      could be found in the application config file."
                // (b) "Directory lookup for the file
                //      c:\argumentsPlus\argumentClinic.mdf\" failed
                //      with the operating system error
                //      The system cannot find the file specified.
                //         CREATE DATABASE failed.
                payPalSuccessLevel = 2222;
                // todo Take appropriate action
            }

            // ................................
            Boolean approval_url_found = false;
            if(payPalSuccessLevel == 100)
            {
                List<PayPal.Api.Payments.Links> linksFromCreatedPayment = createdInvoice.links;
                foreach (PayPal.Api.Payments.Links rel_approval in linksFromCreatedPayment)
                {
                    if (rel_approval.rel.ToLower().Equals("approval_url"))
                    {
                        redirect_approvalUrlFromCreatedPayment = Server.UrlDecode(rel_approval.href);
                        approval_url_found = true;
                        break;
                    }
                }
                if (!approval_url_found) payPalSuccessLevel = 69;
            }
            return payPalSuccessLevel;
        }