public async Task <ActionResult> Index()
        {
            var _merchantCertificateData     = System.IO.File.ReadAllBytes(Server.MapPath("~/App_Data/1231181189.p12"));
            var _merchantCertificatePassword = "******";
            var _merchantId = "1231181189";


            var client = new SwishClient(SwishEnvironment.Test, _merchantCertificateData, _merchantCertificatePassword, _merchantId);

            // Make payment
            var ecommercePaymentModel = new ECommercePaymentModel(
                amount: "100",
                currency: "SEK",
                callbackUrl: "https://example.com/api/swishcb/paymentrequests",
                payerAlias: "1231234567890")
            {
                PayeePaymentReference = "0123456789",
                Message = "Kingston USB Flash Drive 8 GB"
            };

            try
            {
                var paymentResponse = await client.MakeECommercePaymentAsync(ecommercePaymentModel);

                return(View());
            }
            catch (Exception ex)
            {
                return(View("Error"));
            }
        }
示例#2
0
 public async Task MakeECommercePayment_Throws_Http_Exception_For_Not_Ok_Status_Codes()
 {
     var mockHttp = MockHttp.WithStatus(500);
     var client   = new SwishClient(_configuration, mockHttp);
     await Assert.ThrowsAsync <HttpRequestException>(() =>
                                                     client.MakeECommercePaymentAsync(_defaultECommercePaymentModel));
 }
        public async Task MakeMCommercePaymentAsync_Returns_Correct_ErrorMessage_For_422(string errorCode, string errorMessage, string content)
        {
            var mockHttp = MockHttp.WithStatusAndContent(422, content);
            var client   = new SwishClient(mockHttp, _merchantId);

            var result = await client.MakeMCommercePaymentAsync(_defaultMCommercePaymentModel);

            result.ErrorMessage.Should().Contain(errorMessage);
            result.ErrorCode.Should().Be(errorCode);
        }
示例#4
0
        public async Task MakeECommercePayment_Throws_Swich_Exception_When_Status_Code_Is_422()
        {
            var errorMsg  = "Testing error";
            var mockHttp  = MockHttp.WithStatusAndContent(422, errorMsg);
            var client    = new SwishClient(_configuration, mockHttp);
            var exception = await Assert.ThrowsAsync <SwishException>(() =>
                                                                      client.MakeECommercePaymentAsync(_defaultECommercePaymentModel));

            Assert.Equal(errorMsg, exception.Message);
        }
        public async Task GetPaymeentStatus_Returns_Correct_ErrorMessage(int status, string message)
        {
            var mockHttp = MockHttp.WithStatus(status);
            var client   = new SwishClient(mockHttp, _merchantId);

            var result = await client.GetPaymentStatus(SwishId);

            result.ErrorMessage.Should().Contain(message);
            result.ErrorCode.Should().Be(status.ToString());
        }
        public async Task MakeMCommercePaymentAsync_Returns_Correct_ErrorMessage(int status, string message)
        {
            var mockHttp = MockHttp.WithStatus(status);
            var client   = new SwishClient(mockHttp, _merchantId);

            var result = await client.MakeMCommercePaymentAsync(_defaultMCommercePaymentModel);

            result.ErrorMessage.Should().Contain(message);
            result.ErrorCode.Should().Be(status.ToString());
        }
        private SwishClient GetSwishClient()
        {
            var configuration = new TestConfig(Config.Test.PayeeAlias);
            var serverMapPath = Server.MapPath(@"~/App_Data/");
            var clientCert    = new X509Certificate2(serverMapPath + Config.Test.CertificateFileName, Config.Test.CertificatePassword);
            var caCert        = new X509Certificate2(serverMapPath + "Swish_TLS_RootCA.pem");

            var client = new SwishClient(configuration, clientCert, caCert);

            return(client);
        }
        public async Task <ActionResult> Index2()
        {
            var _merchantPFXForTest            = System.IO.File.ReadAllBytes(Server.MapPath("~/App_Data/1231181189.p12"));
            var _merchantCertificateDataInPEM  = System.IO.File.ReadAllBytes(Server.MapPath("~/App_Data/prod.pem"));
            var _merchantCertificateDataInPEM2 = System.IO.File.ReadAllBytes(Server.MapPath("~/App_Data/prod2.pem"));
            var _merchantPrivateKey            = System.IO.File.ReadAllText(Server.MapPath("~/App_Data/private.key"));
            var _merchantCertificatePassword   = "";
            var _merchantId = "test";

            bool test1, test2 = false, test3;

            var bytes = CertificateGenerator.GenerateP12(_merchantPrivateKey, _merchantCertificateDataInPEM, "");

            var client = new SwishClient(SwishEnvironment.Production,
                                         P12CertificateCollectionBytes: bytes,
                                         P12CertificateCollectionPassphrase: _merchantCertificatePassword,
                                         merchantId: _merchantId);

            var paymentStatus = await client.GetPaymentStatus("anything");

            test1 = true;


            var bytes2  = CertificateGenerator.GenerateP12(_merchantPrivateKey, _merchantCertificateDataInPEM2, "");
            var client2 = new SwishClient(SwishEnvironment.Production,
                                          P12CertificateCollectionBytes: bytes2,
                                          P12CertificateCollectionPassphrase: "swish",
                                          merchantId: _merchantId);

            try
            {
                var paymentStatus2 = await client2.GetPaymentStatus("anything");
            }
            catch (Exception ex)
            {
                test2 = true;
            }

            var paymentStatus3 = await client.GetPaymentStatus("anything");

            var client4 = new SwishClient(SwishEnvironment.Production,
                                          P12CertificateCollectionBytes: bytes,
                                          P12CertificateCollectionPassphrase: _merchantCertificatePassword,
                                          merchantId: _merchantId);

            var paymentStatus4 = await client.GetPaymentStatus("anything");

            test3 = true;

            return(test1 && test2 && test3?View() : View("Error"));
        }
示例#9
0
        public async Task ECommerceScenario()
        {
            var clientCert = new X509Certificate2("testcertificates/SwishMerchantTestCertificate1231181189.p12", "swish");
            var caCert     = new X509Certificate2("testcertificates/Swish TLS Root CA.pem");
            var client     = new SwishClient(configuration, clientCert, caCert);

            // Make payment
            var ecommercePaymentModel = new ECommercePaymentModel(
                amount: "100",
                currency: "SEK",
                callbackUrl: "https://example.com/api/swishcb/paymentrequests",
                payerAlias: "1231234567890")
            {
                PayeePaymentReference = "0123456789",
                Message = "Kingston USB Flash Drive 8 GB"
            };

            var paymentResponse = await client.MakeECommercePaymentAsync(ecommercePaymentModel);

            Console.WriteLine("Hello");

            // Wait so that the payment request has been processed
            Thread.Sleep(5000);

            // Check payment request status
            var paymentStatus = await client.GetPaymentStatus(paymentResponse.Id);

            Assert.Equal("PAID", paymentStatus.Status);

            // Make refund
            var refundModel = new RefundModel(
                originalPaymentReference: paymentStatus.PaymentReference,
                callbackUrl: "https://example.com/api/swishcb/refunds",
                payerAlias: "1231181189",
                amount: "100",
                currency: "SEK")
            {
                PayerPaymentReference = "0123456789",
                Message = "Refund for Kingston USB Flash Drive 8 GB"
            };
            var refundResponse = await client.MakeRefundAsync(refundModel);

            // Wait so that the refund request has been processed
            Thread.Sleep(10000);

            // Check refund request status
            var refundStatus = await client.GetRefundStatus(refundResponse.Id);

            Assert.Equal("PAID", refundStatus.Status);
        }
        public async Task GenerateSwishUrl_Returns_Valid_Url()
        {
            // Arrange
            var token       = "c28a4061470f4af48973bd2a4642b4fa";
            var redirectUrl = "https://example.com/api/swishcb/mpaymentcomplete/123";
            var client      = new SwishClient(null, _merchantId);

            // Act
            string result = client.GenerateSwishUrl(token, redirectUrl);

            // Assert
            result.Should().MatchEquivalentOf("swish://paymentrequest?token=*&callbackurl=*");
            result.Should().Contain(token);
            result.Should().Contain(WebUtility.UrlEncode(redirectUrl));
        }
        public async Task ECommerceScenario()
        {
            var client = new SwishClient(SwishEnvironment.Test, _merchantCertificateData, _merchantCertificatePassword, _merchantId);

            // Make payment
            var ecommercePaymentModel = new ECommercePaymentModel(
                amount: "100",
                currency: "SEK",
                callbackUrl: "https://example.com/api/swishcb/paymentrequests",
                payerAlias: "1231234567890")
            {
                PayeePaymentReference = "0123456789",
                Message = "Kingston USB Flash Drive 8 GB"
            };

            var paymentResponse = await client.MakeECommercePaymentAsync(ecommercePaymentModel);

            // Wait so that the payment request has been processed
            await Task.Delay(5000);

            // Check payment request status
            var paymentStatus = await client.GetPaymentStatus(paymentResponse.Id);

            Assert.Equal("PAID", paymentStatus.Status);

            // Make refund
            var refundModel = new RefundModel(
                originalPaymentReference: paymentStatus.PaymentReference,
                callbackUrl: "https://example.com/api/swishcb/refunds",
                payerAlias: "1231181189",
                amount: "100",
                currency: "SEK")
            {
                PayerPaymentReference = "0123456789",
                Message = "Refund for Kingston USB Flash Drive 8 GB"
            };
            var refundResponse = await client.MakeRefundAsync(refundModel);

            // Wait so that the refund request has been processed
            await Task.Delay(10000);

            // Check refund request status
            var refundStatus = await client.GetRefundStatus(refundResponse.Id);

            Assert.Equal("PAID", refundStatus.Status);
        }
示例#12
0
        public async Task MakeECommercePayment_Returns_Location_Header_Values()
        {
            string paymentId      = "AB23D7406ECE4542A80152D909EF9F6B";
            string locationHeader = $"https://mss.swicpc.bankgirot.se/swishcpcapi/v1/paymentrequests/{paymentId}";
            var    headerValues   = new Dictionary <string, string>()
            {
                { "Location", locationHeader }
            };
            var responseMessage = Create201HttpJsonResponseMessage(_defaultECommercePaymentModel, headerValues);
            var client          = new SwishClient(_configuration, MockHttp.WithResponseMessage(responseMessage));

            // Act
            var response = await client.MakeECommercePaymentAsync(_defaultECommercePaymentModel);

            // Assert
            Assert.Equal(locationHeader, response.Location);
            Assert.Equal(paymentId, response.Id);
        }
        public async Task MakeRefund_Returns_Location_Header_Values()
        {
            string refundId       = "ABC2D7406ECE4542A80152D909EF9F6B";
            string locationHeader = $"https://mss.swicpc.bankgirot.se/swishcpcapi/v1/refunds/{refundId}";
            var    headerValues   = new Dictionary <string, string>()
            {
                { "Location", locationHeader }
            };
            var responseMessage = Create201HttpJsonResponseMessage(_defaultRefund, headerValues);
            var client          = new SwishClient(MockHttp.WithResponseMessage(responseMessage), _merchantId);

            // Act
            var response = await client.MakeRefundAsync(_defaultRefund);

            // Assert
            Assert.Equal(response.Location, locationHeader);
            Assert.Equal(response.Id, refundId);
        }
        public async Task CertificateAcceptedTest23()
        {
            var bytes = CertificateGenerator.GenerateP12(_merchantPrivateKey, _merchantCertificateDataInPEM, "");

            var client = new SwishClient(SwishEnvironment.Production,
                P12CertificateCollectionBytes: bytes,
                P12CertificateCollectionPassphrase: "",
                merchantId: "123");

            var paymentStatus = await client.GetPaymentStatus("test");

            var client2 = new SwishClient(SwishEnvironment.Production,
                P12CertificateCollectionBytes: bytes,
                P12CertificateCollectionPassphrase: "",
                merchantId: "123");

            var paymentStatus2 = await client2.GetPaymentStatus("test");
        }
        public async Task MakeMCommercePayment_Returns_Location_And_Token_Header_VaLues()
        {
            string paymentId      = "AB23D7406ECE4542A80152D909EF9F6B";
            string locationHeader = $"https://mss.swicpc.bankgirot.se/swishcpcapi/v1/paymentrequests/{paymentId}";
            var    headerValues   = new Dictionary <string, string>()
            {
                { "Location", locationHeader },
                { "PaymentRequestToken", "f34DS34lfd0d03fdDselkfd3ffk21" }
            };
            var responseMessage = Create201HttpJsonResponseMessage(_defaultMCommercePaymentModel, headerValues);
            var client          = new SwishClient(MockHttp.WithResponseMessage(responseMessage), _merchantId);

            // Act
            var response = await client.MakeMCommercePaymentAsync(_defaultMCommercePaymentModel);

            // Assert
            Assert.Equal(locationHeader, response.Location);
            Assert.Equal(paymentId, response.Id);
            Assert.Equal("f34DS34lfd0d03fdDselkfd3ffk21", response.Token);
        }
        public async Task CertificateAcceptedTest2()
        {
            var bytes = CertificateGenerator.GenerateP12(_merchantPrivateKey, _merchantCertificateDataInPEM, "");

            var client = new SwishClient(SwishEnvironment.Production,
                P12CertificateCollectionBytes: bytes,
                P12CertificateCollectionPassphrase: _merchantCertificatePassword,
                merchantId: _merchantId);

            var paymentStatus = await client.GetPaymentStatus("anything");

            // should be "not found" in the error message, which states that connection is established but obviously transaction was not found
            Assert.NotNull(paymentStatus.ErrorMessage);

            // now test misconfigured client (should throw exception)
            var clientWithWrongPfx = new SwishClient(SwishEnvironment.Production,
                P12CertificateCollectionBytes: _merchantPFXForTest,
                P12CertificateCollectionPassphrase: "swish",
                merchantId: _merchantId);

            try
            {
                var _ = await clientWithWrongPfx.GetPaymentStatus("anything");
            }
            catch (Exception ex)
            {

            }

            // now create working client to check if any of the certificates sticked from last one
            var client2 = new SwishClient(SwishEnvironment.Production,
                P12CertificateCollectionBytes: bytes,
                P12CertificateCollectionPassphrase: _merchantCertificatePassword,
                merchantId: _merchantId);

            var paymentStatus2 = await client.GetPaymentStatus("anything");

            // should be "not found" in the error message, which states that connection is established but obviously transaction was not found
            Assert.NotNull(paymentStatus.ErrorMessage);
        }
        public async Task MakeECommercePayment_Should_Fix_PayerAlias()
        {
            string paymentId      = "AB23D7406ECE4542A80152D909EF9F6B";
            string locationHeader = $"https://mss.swicpc.bankgirot.se/swishcpcapi/v1/paymentrequests/{paymentId}";
            var    headerValues   = new Dictionary <string, string>()
            {
                { "Location", locationHeader }
            };

            _defaultECommercePaymentModel.PayerAlias = "0701234567";
            var responseMessage = Create201HttpJsonResponseMessage(_defaultECommercePaymentModel, headerValues);
            var mockHttp        = MockHttp.WithResponseMessage(responseMessage);
            var client          = new SwishClient(mockHttp, _merchantId);

            // Act
            var response = await client.MakeECommercePaymentAsync(_defaultECommercePaymentModel);

            // Assert
            var body = await mockHttp.LastRequest.Content.ReadAsStringAsync();

            var request = JsonConvert.DeserializeObject <ECommercePaymentModel>(body);

            Assert.Equal("46701234567", request.PayerAlias);
        }
示例#18
0
 public SwishController(SwishClient client, IMemoryCache memoryCache, IOptionsMonitor <SwishSettings> settingsAccessor)
 {
     _client      = client;
     _memoryCache = memoryCache;
     _settings    = settingsAccessor.CurrentValue;
 }