public static OurOrdersBuilder UsePostFinance(this OurOrdersBuilder builder, string PSPID, string COM, string USERID, string PSWD, string SHASIGN, bool sandbox = false)
        {
            if (string.IsNullOrEmpty(PSPID))
            {
                throw new ArgumentNullException(nameof(PSPID));
            }

            if (string.IsNullOrEmpty(USERID))
            {
                throw new ArgumentNullException(nameof(USERID));
            }

            if (string.IsNullOrEmpty(PSWD))
            {
                throw new ArgumentNullException(nameof(PSWD));
            }

            if (string.IsNullOrEmpty(SHASIGN))
            {
                throw new ArgumentNullException(nameof(SHASIGN));
            }

            if (string.IsNullOrEmpty(COM))
            {
                throw new ArgumentNullException(nameof(COM));
            }


            var postfinanceConfiguration = new PostFinanceConfiguration(SHASIGN, PSPID, COM, USERID, PSWD, sandbox);


            builder.AppEvents.Configure += (sender, services) =>
            {
                services.AddTransient <IPaymentProvider, PostFinancePaymentProvider>();
                services.AddTransient <PostFinancePaymentProvider>();
                services.AddSingleton(postfinanceConfiguration);
            };

            builder.HostServices.AddSingleton(postfinanceConfiguration);
            builder.HostServices.AddTransient <IPaymentProvider, PostFinancePaymentProvider>();
            builder.HostServices.AddTransient <PostFinancePaymentProvider>();

            builder.AppSettings.ExternalControllers.Add(typeof(PostFinancePaymentController));
            return(builder);
        }
Exemple #2
0
        public async Task <HttpResponseMessage> PostFeedback([FromServices] PostFinancePaymentProvider provider, [FromServices] PostFinanceConfiguration configuration, [FromServices] OrderService orderService, [FromBody] PostFinanceFeedbackBindings bindings, CancellationToken cancellationToken = default(CancellationToken))
        {
            var           message   = new HttpResponseMessage();
            var           paymentID = bindings.orderID;
            var           orderId   = bindings.ORDER;
            var           amount    = bindings.amount;
            var           pm        = bindings.PM;//obj.Get("PM");
            var           infos     = "";
            PaymentStatus finalStatus;


            var order = await orderService.GetByIdAsync(orderId, cancellationToken);

            var payment = order.Payments.FirstOrDefault(t => t.Id == paymentID);

            if (payment != null && payment.Status == PaymentStatus.Paid)
            {
                // already marked as paid
                return(message);
            }


            //transaction.Details += ApiLogEntry.Create(controller, controller.OwinContext).ToJson();
            switch (bindings.status)
            {
            case 5:
            case 9:
                finalStatus = PaymentStatus.Paid;
                break;

            default:
                finalStatus = PaymentStatus.Failed;
                infos       = $"transaction feedback status: {bindings.status} on {DateTime.UtcNow.ToShortDateString()} {DateTime.UtcNow.ToShortTimeString()} \n";
                break;
            }

            if (payment == null)
            {
                payment = new Payment
                {
                    Title     = "PostFinance Payment",
                    Provider  = provider.Name,
                    Reference = $"{paymentID}",
                    Status    = finalStatus,
                    Date      = DateTime.UtcNow,
                    Method    = PaymentMethod.Electronic,
                    Details   = $"Payment Order #{order.Reference ?? order.Id} {infos}",
                    Currency  = order.Currency,
                    Amount    = amount,
                    Id        = paymentID
                };
                await orderService.AddPayment(order, payment, cancellationToken);
            }
            else
            {
                payment.Status   = finalStatus;
                payment.Details += infos;
                await orderService.UpdatePayment(order, payment, cancellationToken);
            }
            return(message);
        }
Exemple #3
0
        public async Task <IActionResult> FormAsync([FromServices] PostFinancePaymentProvider provider, [FromServices] PostFinanceConfiguration configuration, [FromServices] OrderService orderService, [FromBody] FromBindings bindings, CancellationToken cancellationToken = default(CancellationToken))
        {
            var order = await orderService.GetByIdAsync(bindings.OrderID, cancellationToken);

            var     message = new HttpResponseMessage();
            dynamic resp    = new System.Dynamic.ExpandoObject();

            resp.method = "POST";

            resp.action = configuration.Sandbox ? FORM_ACTION_TEST : FORM_ACTION_PROD;
            var amount         = bindings.Amount;
            var transactionUID = order.Reference + DateTime.Now.ToString("hh:mm:ss:ff").ShortMD5();

            // var payment = new Payment()
            // {
            //     Title = "PostFinance Payment",
            //     Provider = provider.Name,
            //     Reference = $"{transactionUID}",
            //     Status = PaymentStatus.Pending,
            //     Date = DateTime.UtcNow,
            //     Method = PaymentMethod.Electronic,
            //     Details = $"Payment Order #{order.Reference ?? order.Id}",
            //     Currency = order.Currency,
            //     Amount = amount,
            //     UID = transactionUID
            // };
            // await orderService.AddPayment(order, payment, cancellationToken);

            resp.amount = amount.ToString("0.00", CultureInfo.InvariantCulture);
            resp.txid   = transactionUID;


            var formParams = new Dictionary <string, string>
            {
                { "PSPID", configuration.PSPID },
                { "orderID", transactionUID },
                { "amount", Math.Ceiling(amount * 100).ToString("0", CultureInfo.InvariantCulture) },
                { "currency", "CHF" },
                { "language", "de" },
                { "CN", (order.Client?.FirstName ?? "") + " " + (order.Client?.LastName ?? "") },
                { "EMAIL", order.Client?.Email ?? "" },
                { "COM", configuration.COM },
                { "PARAMPLUS", "ORDER=" + order.Id }
            };

            // if (PMLIST != null)
            //     formParams.Add("PMLIST", string.Join(";", PMLIST));
            // var redirectUri = bindings.RedirectUri;
            // formParams.Add("backurl", redirectUri + "/Orders/" + member.UserName + "#back");
            // formParams.Add("accepturl", redirectUri + "/Thanks/" + member.UserName);
            // formParams.Add("declineurl", redirectUri + "/Pay/" + member.UserName + "#decline");
            // formParams.Add("cancelurl", redirectUri + "/Pay/" + member.UserName + "#cancel");
            // formParams.Add("exceptionurl", redirectUri + "/Pay/" + member.UserName + "#exception");


            ////if (PARAMPLUS != null)
            //    formParams.Add("PARAMPLUS", PARAMPLUS(member));

            var strb = new StringBuilder();

            foreach (var kv in formParams.Where(kv => !string.IsNullOrEmpty(kv.Value as string)).OrderBy(k => k.Key))
            {
                strb.Append(kv.Key.ToUpperInvariant());
                strb.Append("=");
                strb.Append(formParams[kv.Key].ToString());
                strb.Append(configuration.SHASIGN);
            }

            var bytes  = Encoding.UTF8.GetBytes(strb.ToString());
            var sha    = new SHA1CryptoServiceProvider();
            var hashed = sha.ComputeHash(bytes);

            strb = new StringBuilder(hashed.Length * 2);
            foreach (byte b in hashed)
            {
                strb.Append(b.ToString("X2"));
            }

            formParams.Add("SHASIGN", strb.ToString());

            resp.param = formParams;


            message.Content = new StringContent(JsonConvert.SerializeObject(resp));
            message.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            return(Ok(ApiModel.AsSuccess(resp)));
        }
Exemple #4
0
 public PostFinancePaymentProvider(OrderService orderService, PostFinanceConfiguration configuration)
 {
     this.orderService  = orderService;
     this.configuration = configuration;
 }