public async Task <IActionResult> PostPayment(string invoiceId, string cryptoCode = null)
        {
            var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId);

            if (cryptoCode == null)
            {
                cryptoCode = "BTC";
            }
            var network = _NetworkProvider.GetNetwork(cryptoCode);

            if (network == null || invoice == null || invoice.IsExpired() || !invoice.Support(new PaymentMethodId(cryptoCode, Payments.PaymentTypes.BTCLike)))
            {
                return(NotFound());
            }

            var wallet = _WalletProvider.GetWallet(network);

            if (wallet == null)
            {
                return(NotFound());
            }
            var payment = PaymentMessage.Load(Request.Body, network.NBitcoinNetwork);
            var unused  = wallet.BroadcastTransactionsAsync(payment.Transactions);
            await _InvoiceRepository.AddRefundsAsync(invoiceId, payment.RefundTo.Select(p => new TxOut(p.Amount, p.Script)).ToArray(), network.NBitcoinNetwork);

            return(new PaymentAckActionResult(payment.CreateACK(invoiceId + " is currently processing, thanks for your purchase...")));
        }
        void ListenerCallback(IAsyncResult ar)
        {
            try
            {
                var   context    = _Listener.EndGetContext(ar);
                var   type       = context.Request.QueryString.Get("type");
                var   businessId = int.Parse(context.Request.QueryString.Get("id"));
                var   now        = DateTimeOffset.UtcNow;
                var   expire     = now + TimeSpan.FromDays(1);
                TxOut txOut      = new TxOut(Money.Coins(1), new Key().ScriptPubKey);
                if (type == "Request")
                {
                    Assert.Equal(PaymentRequest.MediaType, context.Request.AcceptTypes[0]);
                    context.Response.ContentType = PaymentRequest.MediaType;
                    PaymentRequest request = new PaymentRequest();
                    request.Details.MerchantData = BitConverter.GetBytes(businessId);
                    request.Details.Network      = Network.RegTest;
                    request.Details.Expires      = expire;
                    request.Details.Time         = now;
                    request.Details.PaymentUrl   = new Uri(_Prefix + "?id=" + businessId + "&type=Payment");
                    request.Details.Outputs.Add(new PaymentOutput(txOut));
                    request.Sign(File.ReadAllBytes("data/NicolasDorierMerchant.pfx"), PKIType.X509SHA256);
                    request.WriteTo(context.Response.OutputStream);
                }
                else if (type == "Payment")
                {
                    Assert.Equal(PaymentMessage.MediaType, context.Request.ContentType);
                    Assert.Equal(PaymentACK.MediaType, context.Request.AcceptTypes[0]);

                    var payment = PaymentMessage.Load(context.Request.InputStream);
                    Assert.Equal(businessId, BitConverter.ToInt32(payment.MerchantData, 0));

                    context.Response.ContentType = PaymentACK.MediaType;
                    var ack = payment.CreateACK();
                    ack.Memo = "Thanks for your purchase";
                    ack.WriteTo(context.Response.OutputStream);
                }
                else
                {
                    Assert.False(true, "Impossible");
                }

                context.Response.Close();
                _Listener.BeginGetContext(ListenerCallback, null);
            }
            catch (Exception)
            {
                if (!_Stopped)
                {
                    throw;
                }
            }
        }
Beispiel #3
0
        public async Task <IActionResult> PostPayment(string invoiceId)
        {
            var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId);

            if (invoice == null || invoice.IsExpired())
            {
                return(NotFound());
            }
            var payment = PaymentMessage.Load(Request.Body);
            var unused  = _Wallet.BroadcastTransactionsAsync(payment.Transactions);
            await _InvoiceRepository.AddRefundsAsync(invoiceId, payment.RefundTo.Select(p => new TxOut(p.Amount, p.Script)).ToArray());

            return(new PaymentAckActionResult(payment.CreateACK(invoiceId + " is currently processing, thanks for your purchase...")));
        }
        public void CanCreatePaymentMessageAndACK()
        {
            var request = LoadPaymentRequest("data/payreq1_sha1.paymentrequest");
            var payment = request.CreatePayment();

            AssertEx.CollectionEquals(request.Details.MerchantData, payment.MerchantData);
            AssertEx.CollectionEquals(payment.ToBytes(), PaymentMessage.Load(payment.ToBytes()).ToBytes());
            payment.Memo = "thanks merchant !";
            AssertEx.CollectionEquals(payment.ToBytes(), PaymentMessage.Load(payment.ToBytes()).ToBytes());
            var ack = payment.CreateACK();

            AssertEx.CollectionEquals(ack.Payment.ToBytes(), PaymentMessage.Load(payment.ToBytes()).ToBytes());
            AssertEx.CollectionEquals(ack.ToBytes(), PaymentACK.Load(ack.ToBytes()).ToBytes());
            ack.Memo = "thanks customer !";
            AssertEx.CollectionEquals(ack.ToBytes(), PaymentACK.Load(ack.ToBytes()).ToBytes());
        }
Beispiel #5
0
        void ListenerCallback(IAsyncResult ar)
        {
            try
            {
                var context    = _Listener.EndGetContext(ar);
                var type       = context.Request.QueryString.Get("type");
                var businessId = int.Parse(context.Request.QueryString.Get("id"));
                if (type == "Request")
                {
                    Assert.Equal(PaymentRequest.MediaType, context.Request.AcceptTypes[0]);
                    context.Response.ContentType = PaymentRequest.MediaType;
                    PaymentRequest request = new PaymentRequest();
                    request.Details.MerchantData = BitConverter.GetBytes(businessId);
                    request.Details.PaymentUrl   = new Uri(_Prefix + "?id=" + businessId + "&type=Payment");
                    request.Sign(new X509Certificate2("data/NicolasDorierMerchant.pfx"), PKIType.X509SHA256);
                    request.WriteTo(context.Response.OutputStream);
                }
                else if (type == "Payment")
                {
                    Assert.Equal(PaymentMessage.MediaType, context.Request.ContentType);
                    Assert.Equal(PaymentACK.MediaType, context.Request.AcceptTypes[0]);

                    var payment = PaymentMessage.Load(context.Request.InputStream);
                    Assert.Equal(businessId, BitConverter.ToInt32(payment.MerchantData, 0));

                    context.Response.ContentType = PaymentACK.MediaType;
                    var ack = payment.CreateACK();
                    ack.WriteTo(context.Response.OutputStream);
                }
                else
                {
                    Assert.False(true, "Impossible");
                }

                context.Response.Close();
                _Listener.BeginGetContext(ListenerCallback, null);
            }
            catch (Exception)
            {
                if (!_Stopped)
                {
                    throw;
                }
            }
        }