private PayData SkapaPaysonPayData(SignMeUpVM SUPVM, Organisation org)
        {
            // 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 returnUrl = Url.Action("Returned", "Payson", new RouteValueDictionary(), scheme, host) + "/" + SUPVM.RegistreringsId;

            var cancelUrl = Url.Action("Cancelled", "Payson", new RouteValueDictionary(), scheme, host) + "/" + SUPVM.RegistreringsId;

            var sender = new Sender(SUPVM.Kontaktinformation.SenderEmail);

            sender.FirstName = SUPVM.Kontaktinformation.SenderFirstName;
            sender.LastName  = SUPVM.Kontaktinformation.SenderLastName;

            var receiver = new Receiver(org.Epost, SUPVM.AttBetala);

            receiver.FirstName = org.Namn;
            receiver.SetPrimaryReceiver(true);

            var payData = new PayData(returnUrl,
                                      cancelUrl,
                                      smuService.HamtaEvenemang(SUPVM.EvenemangsId).Namn + " - " + SUPVM.GetFaltvarde("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", "Payson", new RouteValueDictionary(), scheme, host) + "/" + SUPVM.RegistreringsId;

            payData.SetIpnNotificationUrl(ipnNotificationUrl);

            payData.SetFundingConstraints(new List <FundingConstraint> {
                FundingConstraint.Bank, FundingConstraint.CreditCard
            });
            payData.SetTrackingId(SUPVM.RegistreringsId.ToString());

            // Skapa poster för betalning
            var orderItems = new List <PaysonIntegration.Utils.OrderItem>();

            foreach (ValViewModel post in SUPVM.Betalnignsposter)
            {
                var oi = new PaysonIntegration.Utils.OrderItem(post.TypNamn + ": " + post.Namn);
                oi.SetOptionalParameters("st", 1, post.Avgift, 0);
                orderItems.Add(oi);
            }

            payData.SetOrderItems(orderItems);

            return(payData);
        }
Beispiel #2
0
        public ActionResult Pay(PayViewModel payViewModel)
        {
            var orderGuid = Guid.NewGuid().ToString();

            // 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, "") + "?orderGuid=" + orderGuid;

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

            // When the shop is hosted by Payson the IPN scheme must be http and not https
            var ipnNotificationUrl = Url.Action("IPN", "Checkout", new RouteValueDictionary(), "http", host).Replace(oldPort, "") + "?orderGuid=" + orderGuid;

            var sender = new PaysonIntegration.Utils.Sender(payViewModel.Sender.Email);

            sender.FirstName = payViewModel.Sender.FirstName;
            sender.LastName  = payViewModel.Sender.LastName;

            var totalAmount = payViewModel.OrderItems.Sum(o => o.UnitPrice * o.Quantity * (1 + o.TaxPercentage));

            var receiver = new PaysonIntegration.Utils.Receiver(payViewModel.Receiver.Email, totalAmount);

            receiver.FirstName = payViewModel.Receiver.FirstName;
            receiver.LastName  = payViewModel.Receiver.LastName;
            receiver.SetPrimaryReceiver(true);

            var payData = new PayData(returnUrl, cancelUrl, payViewModel.Memo, sender,
                                      new List <PaysonIntegration.Utils.Receiver> {
                receiver
            });

            switch (payViewModel.GuaranteeOffered)
            {
            case GuaranteeOffered.NO:
                payData.GuaranteeOffered = PaysonIntegration.Utils.GuaranteeOffered.No;
                break;

            case GuaranteeOffered.OPTIONAL:
                payData.GuaranteeOffered = PaysonIntegration.Utils.GuaranteeOffered.Optional;
                break;

            case GuaranteeOffered.REQUIRED:
                payData.GuaranteeOffered = PaysonIntegration.Utils.GuaranteeOffered.Required;
                break;

            default:
                payData.GuaranteeOffered = null;
                break;
            }

            payData.SetCurrencyCode(payViewModel.CurrencyCode);
            var fundingConstraints = new List <FundingConstraint>();

            if (payViewModel.PaymentMethod == PaymentMethod.PaysonInvoice)
            {
                fundingConstraints.Add(FundingConstraint.Invoice);
            }
            else if (payViewModel.PaymentMethod == PaymentMethod.PaysonAll)
            {
                fundingConstraints.Add(FundingConstraint.Bank);
                fundingConstraints.Add(FundingConstraint.CreditCard);
                fundingConstraints.Add(FundingConstraint.Invoice);
            }
            else
            {
                fundingConstraints.Add(FundingConstraint.Bank);
                fundingConstraints.Add(FundingConstraint.CreditCard);
            }

            payData.SetFundingConstraints(fundingConstraints);

            payData.SetInvoiceFee(payViewModel.InvoiceFee);
            payData.SetIpnNotificationUrl(ipnNotificationUrl);
            payData.SetLocaleCode(payViewModel.LocaleCode);
            if (payViewModel.PaymentMethod == PaymentMethod.PaysonInvoice || payViewModel.PaymentMethod == PaymentMethod.PaysonAll || payViewModel.IncludeOrderDetails)
            {
                var orderItems = new List <PaysonIntegration.Utils.OrderItem>();
                foreach (var orderModel in payViewModel.OrderItems)
                {
                    var oi = new PaysonIntegration.Utils.OrderItem(orderModel.Description);
                    oi.SetOptionalParameters(orderModel.Sku, orderModel.Quantity, orderModel.UnitPrice,
                                             orderModel.TaxPercentage);
                    orderItems.Add(oi);
                }
                payData.SetOrderItems(orderItems);
            }
            payData.SetTrackingId(orderGuid);

            var api = new PaysonApi(payViewModel.UserId, payViewModel.UserKey, ApplicationId, true);


            var response = api.MakePayRequest(payData);

            if (response.Success)
            {
                var state = new PurchaseState
                {
                    UserId  = payViewModel.UserId,
                    UserKey = payViewModel.UserKey,
                    Token   = response.Token,
                    Updates = new Dictionary <DateTime, string> {
                        { DateTime.Now, "Created" }
                    },
                    OrderGuid     = orderGuid,
                    LatestStatus  = PaymentStatus.Created.ToString(),
                    ReceiverEmail = receiver.Email,
                };

                repository.SavePurchaseState(state);

                string forwardUrl =
                    string.IsNullOrWhiteSpace(payViewModel.ForwardUrl)
                        ? api.GetForwardPayUrl(response.Token)
                        : payViewModel.ForwardUrl + response.Token;

                return(Redirect(forwardUrl));
            }
            ViewBag.Errors = response.ErrorMessages;

            return(View("Index", GetDefaultPayViewModel()));
        }