Ejemplo n.º 1
0
    /// <summary>
    /// Example starting the transaction with the demo basket
    /// </summary>
    protected void btn_StartCardPayment_Click(object sender, EventArgs e)
    {
        CNPMerchantWebService merchantWS = new CNPMerchantWebService();
        TransactionDetails    td         = new TransactionDetails();

        td.currencyCode          = Basket.getBasket()[0].currencyCode;
        td.description           = "My first transaction";
        td.goodsList             = Basket.getBasket().ToArray();
        td.merchantId            = "000000000000001";
        td.terminalID            = "TEST TID";
        td.returnURL             = Request.Url.OriginalString.Replace("Checkout.aspx", "Result.aspx");
        td.totalAmount           = Basket.getTotalAmount().ToString();
        td.currencyCodeSpecified = true;
        td.languageCode          = "en";
        td.orderId = (new Random()).Next(10000).ToString();
        td.merchantLocalDateTime = String.Format("{0:dd.MM.yyyy HH:mm:ss}", DateTime.Now);
        td.purchaserName         = "IVANOV IVAN";
        td.purchaserEmail        = "*****@*****.**";

        StartTransactionResult result = merchantWS.startTransaction(td);

        if (result.success)
        {
            Session["customerReference"] = result.customerReference;
            Response.Redirect(result.redirectURL);
        }
        else
        {
            lbl_Error.Text = result.errorDescription;
        }
    }
Ejemplo n.º 2
0
        public async Task Execute_HaveOCCExceptionsAndAbortFailuresWithinRetryLimit_Succeeded()
        {
            var mockCreator = new Mock <Func <Task <Session> > >();
            var mockSession = new Mock <Session>(null, null, null, null, null);

            mockCreator.Setup(x => x()).ReturnsAsync(mockSession.Object);
            var retry = new Mock <Func <int, Task> >();

            var sendCommandResponseStart = new StartTransactionResult
            {
                TransactionId = "testTransactionIdddddd"
            };

            var h1 = QldbHash.ToQldbHash("testTransactionIdddddd");

            var sendCommandResponseCommit = new CommitTransactionResult
            {
                CommitDigest  = new MemoryStream(h1.Hash),
                TransactionId = "testTransactionIdddddd"
            };

            var abortResponse    = new AbortTransactionResult {
            };
            var serviceException = new AmazonServiceException();

            mockSession.Setup(x => x.StartTransaction(It.IsAny <CancellationToken>()))
            .ReturnsAsync(sendCommandResponseStart);
            mockSession.Setup(x => x.CommitTransaction(It.IsAny <string>(), It.IsAny <MemoryStream>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(sendCommandResponseCommit);
            mockSession.SetupSequence(x => x.AbortTransaction(It.IsAny <CancellationToken>()))
            .ThrowsAsync(serviceException)
            .ReturnsAsync(abortResponse)
            .ThrowsAsync(serviceException);

            var mockFunction = new Mock <Func <TransactionExecutor, Task <int> > >();

            mockFunction.SetupSequence(f => f.Invoke(It.IsAny <TransactionExecutor>()))
            .ThrowsAsync(new OccConflictException("occ"))
            .ThrowsAsync(new OccConflictException("occ"))
            .ThrowsAsync(new OccConflictException("occ"))
            .ReturnsAsync(1);
            var mockRetry = new Mock <Action <int> >();

            var pool = new SessionPool(mockCreator.Object, QldbDriverBuilder.CreateDefaultRetryHandler(NullLogger.Instance), 2, NullLogger.Instance);

            var session1 = await pool.GetSession();

            var session2 = await pool.GetSession();

            session1.Release();
            session2.Release();

            await pool.Execute(mockFunction.Object, Driver.RetryPolicy.Builder().Build(), retry.Object);

            mockCreator.Verify(x => x(), Times.Exactly(2));
            retry.Verify(r => r.Invoke(It.IsAny <int>()), Times.Exactly(3));
            Assert.AreEqual(2, pool.AvailablePermit());
        }
        public void Execute_HaveISE_Succeeded()
        {
            var mockCreator = new Mock <Func <Session> >();
            var mockSession = new Mock <Session>(null, null, null, null, null);

            mockCreator.Setup(x => x()).Returns(mockSession.Object);
            var retry = new Mock <Action <int> >();

            var sendCommandResponseStart = new StartTransactionResult
            {
                TransactionId = "testTransactionIdddddd"
            };

            var h1 = QldbHash.ToQldbHash("testTransactionIdddddd");

            var sendCommandResponseCommit = new CommitTransactionResult
            {
                CommitDigest  = new MemoryStream(h1.Hash),
                TransactionId = "testTransactionIdddddd"
            };

            mockSession.Setup(x => x.StartTransaction())
            .Returns(sendCommandResponseStart);
            mockSession.Setup(x => x.CommitTransaction(It.IsAny <string>(), It.IsAny <MemoryStream>()))
            .Returns(sendCommandResponseCommit);

            var mockFunction = new Mock <Func <TransactionExecutor, int> >();

            mockFunction.SetupSequence(f => f.Invoke(It.IsAny <TransactionExecutor>()))
            .Throws(new InvalidSessionException("invalid"))
            .Throws(new InvalidSessionException("invalid"))
            .Throws(new InvalidSessionException("invalid"))
            .Throws(new InvalidSessionException("invalid"))
            .Returns(1);
            var mockRetry = new Mock <Action <int> >();

            var pool = new SessionPool(mockCreator.Object, QldbDriverBuilder.CreateDefaultRetryHandler(NullLogger.Instance), 2, NullLogger.Instance);

            var session1 = pool.GetSession();
            var session2 = pool.GetSession();

            session1.Release();
            session2.Release();

            pool.Execute(mockFunction.Object, Driver.RetryPolicy.Builder().Build(), retry.Object);

            mockCreator.Verify(x => x(), Times.Exactly(6));
            retry.Verify(r => r.Invoke(It.IsAny <int>()), Times.Exactly(4));
            Assert.AreEqual(2, pool.AvailablePermit());
        }
Ejemplo n.º 4
0
        public void TestStartTransactionReturnsResponse()
        {
            var testStartTransactionResult = new StartTransactionResult
            {
                TransactionId = "testTxnIdddddddddddddd"
            };
            var testStartTransactionResponse = new SendCommandResponse
            {
                StartTransaction = testStartTransactionResult
            };

            SetResponse(testStartTransactionResponse);

            Assert.AreEqual(testStartTransactionResult, session.StartTransaction());
        }
        public void Execute_HaveOCCExceptionsAboveRetryLimit_ThrowOCC()
        {
            var mockCreator = new Mock <Func <Session> >();
            var mockSession = new Mock <Session>(null, null, null, null, null);

            mockCreator.Setup(x => x()).Returns(mockSession.Object);
            var retry = new Mock <Action <int> >();

            var sendCommandResponseStart = new StartTransactionResult
            {
                TransactionId = "testTransactionIdddddd"
            };

            var h1 = QldbHash.ToQldbHash("testTransactionIdddddd");

            var sendCommandResponseCommit = new CommitTransactionResult
            {
                CommitDigest  = new MemoryStream(h1.Hash),
                TransactionId = "testTransactionIdddddd"
            };

            mockSession.Setup(x => x.StartTransaction())
            .Returns(sendCommandResponseStart);
            mockSession.Setup(x => x.CommitTransaction(It.IsAny <string>(), It.IsAny <MemoryStream>()))
            .Returns(sendCommandResponseCommit);

            var mockFunction = new Mock <Func <TransactionExecutor, int> >();

            mockFunction.SetupSequence(f => f.Invoke(It.IsAny <TransactionExecutor>()))
            .Throws(new OccConflictException("occ"))
            .Throws(new OccConflictException("occ"))
            .Throws(new OccConflictException("occ"))
            .Throws(new OccConflictException("occ"))
            .Throws(new OccConflictException("occ"));
            var mockRetry = new Mock <Action <int> >();

            var pool = new SessionPool(mockCreator.Object, QldbDriverBuilder.CreateDefaultRetryHandler(NullLogger.Instance), 1, NullLogger.Instance);

            Assert.ThrowsException <OccConflictException>(() => pool.Execute(mockFunction.Object, Driver.RetryPolicy.Builder().Build(), retry.Object));

            mockCreator.Verify(x => x(), Times.Once);
            retry.Verify(r => r.Invoke(It.IsAny <int>()), Times.Exactly(4));
        }
        public void Execute_NoException_ReturnFunctionValue()
        {
            var mockCreator = new Mock <Func <Session> >();
            var mockSession = new Mock <Session>(null, null, null, null, null);

            mockCreator.Setup(x => x()).Returns(mockSession.Object);
            var retry = new Mock <Action <int> >();

            var sendCommandResponseStart = new StartTransactionResult
            {
                TransactionId = "testTransactionIdddddd"
            };

            var h1 = QldbHash.ToQldbHash("testTransactionIdddddd");

            var sendCommandResponseCommit = new CommitTransactionResult
            {
                CommitDigest  = new MemoryStream(h1.Hash),
                TransactionId = "testTransactionIdddddd"
            };

            mockSession.Setup(x => x.StartTransaction())
            .Returns(sendCommandResponseStart);
            mockSession.Setup(x => x.CommitTransaction(It.IsAny <string>(), It.IsAny <MemoryStream>()))
            .Returns(sendCommandResponseCommit);

            var mockFunction = new Mock <Func <TransactionExecutor, int> >();

            mockFunction.Setup(f => f.Invoke(It.IsAny <TransactionExecutor>())).Returns(1);
            var mockRetry = new Mock <Action <int> >();

            var pool = new SessionPool(mockCreator.Object, QldbDriverBuilder.CreateDefaultRetryHandler(NullLogger.Instance), 1, NullLogger.Instance);

            pool.Execute(mockFunction.Object, Driver.RetryPolicy.Builder().Build(), retry.Object);

            mockCreator.Verify(x => x(), Times.Once);
            retry.Verify(r => r.Invoke(It.IsAny <int>()), Times.Never);
        }