/// <summary>
        /// The arguments are the required parameters
        /// </summary>
        public PayData(string returnUrl, string cancelUrl, string memo, Sender sender, List<Receiver> receivers)
        {
            SetReturnUrl(returnUrl);
            SetCancelUrl(cancelUrl);
            SetMemo(memo);
            SetSender(sender);
            SetReceivers(receivers);

            _fundingConstraints = new List<FundingConstraint>();
            _orderItems = new List<OrderItem>();
        }
        protected void InitiatePaymentDetails(IDictionary<string, string> nvpContent)
        {
            if (nvpContent.ContainsKey("purchaseId"))
                PurchaseId = nvpContent["purchaseId"];
            PaymentType = GetPaymentTypeFromString(nvpContent["type"]);
            PaymentStatus = GetPaymentStatusFromString(nvpContent["status"]);

            if (PaymentType.HasValue && PaymentType.Value == Utils.PaymentType.Guarantee)
            {
                GuaranteeStatus = GetGuaranteeStatusFromString(nvpContent["guaranteeStatus"]);
                GuaranteeDeadlineTimestamp = nvpContent["guaranteeDeadlineTimestamp"];
            }

            if (PaymentType.HasValue && PaymentType.Value == Utils.PaymentType.Invoice)
            {
                InvoiceStatus = GetInvoiceStatusFromString(nvpContent["invoiceStatus"]);
                if (InvoiceStatus == Utils.InvoiceStatus.Done ||
                    InvoiceStatus == Utils.InvoiceStatus.OrderCreated ||
                    InvoiceStatus == Utils.InvoiceStatus.Shipped ||
                    InvoiceStatus == Utils.InvoiceStatus.Credited)
                    ShippingAddress = new ShippingAddress
                                          {
                                              City = nvpContent["shippingAddress.city"],
                                              Country = nvpContent["shippingAddress.country"],
                                              Name = nvpContent["shippingAddress.name"],
                                              PostalCode = nvpContent["shippingAddress.postalCode"],
                                              StreetAddress = nvpContent["shippingAddress.streetAddress"]
                                          };
            }

            Sender = new Sender(nvpContent["senderEmail"]);
            Receivers = GetReceivers(nvpContent);

            Custom = nvpContent.GetValueOrNull("custom");
            TrackingId = nvpContent.GetValueOrNull("trackingId");
            CurrencyCode = nvpContent["currencyCode"];
        }
 private void SetSender(Sender sender)
 {
     Sender = sender;
 }
        public ActionResult Pay()
        {
            try
            {
                var checkout = (CheckoutModel)Session["checkout"];

                if (checkout == null || checkout.Registrering == null)
                {
                    return ShowError("Missing data, checkout or checkout.Registrering is null at Pay()");
                }

                log.Debug("Checkout, pay. Lagnamn: " + checkout.Registrering.Lagnamn);

                SaveNewRegistration(checkout.Registrering);
                FillRegistrering(checkout.Registrering);

                checkout.RegId = checkout.Registrering.ID;

                // We remove port info to help when the site is behind a load balancer/firewall that does port rewrites.
                var scheme = Request.Url.Scheme;
                var host = Request.Url.Host;
                //var oldPort = Request.Url.Port.ToString();
                var returnUrl = Url.Action("Returned", "Checkout", new RouteValueDictionary(), scheme, host)/*.Replace(oldPort, "")*/ + "?regId=" + checkout.RegId;

                var cancelUrl = Url.Action("Cancelled", "Checkout", new RouteValueDictionary(), scheme, host)/*.Replace(oldPort, "")*/;

                var sender = new Sender(checkout.SenderEmail);
                sender.FirstName = checkout.SenderFirstName;
                sender.LastName = checkout.SenderLastName;

                var totalAmount = Avgift.Kalk(checkout.Registrering);

                var receiver = new Receiver(CheckoutModel.PaysonRecieverEmail, totalAmount);
                receiver.FirstName = CheckoutModel.PaysonRecieverFirstName;
                receiver.LastName = CheckoutModel.PaysonRecieverLastName;
                receiver.SetPrimaryReceiver(true);

                var payData = new PayData(returnUrl, cancelUrl, "Utmaningen 2014 - " + checkout.Registrering.Lagnamn, sender, new List<Receiver> { receiver });

                // Set IPN callback URL
                // When the shop is hosted by Payson the IPN scheme must be http and not https
                var ipnNotificationUrl = Url.Action("IPN", "Checkout", new RouteValueDictionary(), scheme, host)/*.Replace(oldPort, "")*/ + "?regId=" + checkout.RegId;
                payData.SetIpnNotificationUrl(ipnNotificationUrl);

                payData.SetFundingConstraints(new List<FundingConstraint> { FundingConstraint.Bank, FundingConstraint.CreditCard });
                payData.SetTrackingId(checkout.Registrering.ID.ToString());

                var orderItems = new List<PaysonIntegration.Utils.OrderItem>();
                var reg = checkout.Registrering;
                // Lägg in värden på kvitto
                var oi1 = new PaysonIntegration.Utils.OrderItem("Utmaningen " + DateTime.Now.Year + ", bana " + reg.Banor.Namn);
                oi1.SetOptionalParameters("st", 1, reg.Banor.Avgift, 0);
                orderItems.Add(oi1);
                if (reg.Kanoter.Avgift != 0)
                {
                    var oi2 = new PaysonIntegration.Utils.OrderItem("Kanot, " + reg.Kanoter.Namn);
                    oi2.SetOptionalParameters("st", 1, (decimal)reg.Kanoter.Avgift, 0);
                    orderItems.Add(oi2);
                }
                if (reg.Forseningsavgift != 0)
                {
                    var oi3 = new PaysonIntegration.Utils.OrderItem("Avgift för sen anmälan");
                    oi3.SetOptionalParameters("st", 1, (decimal)reg.Forseningsavgift, 0);
                    orderItems.Add(oi3);
                }
                if (reg.Rabatt != 0)
                {
                    var oi4 = new PaysonIntegration.Utils.OrderItem("Rabatt");
                    oi4.SetOptionalParameters("st", 1, -(decimal)reg.Rabatt, 0);
                    orderItems.Add(oi4);
                }
                payData.SetOrderItems(orderItems);

                var api = new PaysonApi(CheckoutModel.PaysonUserId, CheckoutModel.PaysonUserKey, ApplicationId, false);
            #if DEBUG
                api = new PaysonApi("4", "2acab30d-fe50-426f-90d7-8c60a7eb31d4", ApplicationId, true);
            #endif

                var response = api.MakePayRequest(payData);

                if (response.Success)
                {
                    checkout.Token = response.Token;
                    checkout.Registrering.PaysonToken = response.Token;
                    SaveChanges(checkout.Registrering);

                    var forwardUrl = api.GetForwardPayUrl(response.Token);

                    Session["checkout"] = checkout;

                    return Redirect(forwardUrl);
                }

                return ShowPaymentError("Error when sending payment to payson.", response.NvpContent, checkout.Registrering);
            }
            catch (Exception exception)
            {
                log.Error("Exception in pay.", exception);
                return ShowError("Error in Pay().", exception);
            }
        }
        private void SetSender(Sender sender)
        {
            if (sender == null)
                throw new ArgumentException("Sender must be provided");

            Sender = sender;
        }