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; } } }
public void CanCreatePaymentRequest() { var cert = new X509Certificate2("Data/NicolasDorierMerchant.pfx", (string)null, X509KeyStorageFlags.Exportable); var request = new PaymentRequest(); request.Details.Memo = "hello"; request.Sign(cert, PKIType.X509SHA256); Assert.NotNull(request.MerchantCertificate); Assert.True(request.VerifySignature()); Assert.False(request.VerifyChain(X509VerificationFlags.IgnoreNotTimeValid)); Assert.True(PaymentRequest.Load(request.ToBytes()).VerifySignature()); }
private static void CanCreatePaymentRequestCore(object cert) { var request = new PaymentRequest(); request.Details.Memo = "hello"; request.Sign(cert, PKIType.X509SHA256); Assert.NotNull(request.MerchantCertificate); Assert.False(new X509Certificate2(request.MerchantCertificate, "", X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable).HasPrivateKey); Assert.True(request.VerifySignature()); Assert.False(request.VerifyChain()); AssertEx.CollectionEquals(request.ToBytes(), PaymentRequest.Load(request.ToBytes()).ToBytes()); Assert.True(PaymentRequest.Load(request.ToBytes()).VerifySignature()); }
public async Task <IActionResult> GetInvoiceRequest(string invoiceId) { var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId); if (invoice == null || invoice.IsExpired()) { return(NotFound()); } var dto = invoice.EntityToDTO(); PaymentRequest request = new PaymentRequest { DetailsVersion = 1 }; request.Details.Expires = invoice.ExpirationTime; request.Details.Memo = invoice.ProductInformation.ItemDesc; request.Details.Network = _Network; request.Details.Outputs.Add(new PaymentOutput() { Amount = dto.BTCDue, Script = BitcoinAddress.Create(dto.BitcoinAddress, _Network).ScriptPubKey }); request.Details.MerchantData = Encoding.UTF8.GetBytes(invoice.Id); request.Details.Time = DateTimeOffset.UtcNow; request.Details.PaymentUrl = new Uri(invoice.ServerUrl.WithTrailingSlash() + ($"i/{invoice.Id}"), UriKind.Absolute); var store = await _StoreRepository.FindStore(invoice.StoreId); if (store == null) { throw new BitpayHttpException(401, "Unknown store"); } if (store.StoreCertificate != null) { try { request.Sign(store.StoreCertificate, PKIType.X509SHA256); } catch (Exception ex) { Logs.PayServer.LogWarning(ex, "Error while signing payment request"); } } return(new PaymentRequestActionResult(request)); }
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; } } }
public void CanCreatePaymentRequest() { foreach (var provider in new ICertificateServiceProvider[] { #if WIN new WindowsCertificateServiceProvider(X509VerificationFlags.IgnoreNotTimeValid) #endif }) { PaymentRequest.DefaultCertificateServiceProvider = provider; var cert = File.ReadAllBytes("Data/NicolasDorierMerchant.pfx"); var request = new PaymentRequest(); request.Details.Memo = "hello"; request.Sign(cert, PKIType.X509SHA256); Assert.NotNull(request.MerchantCertificate); Assert.True(request.VerifySignature()); Assert.False(request.VerifyChain()); AssertEx.CollectionEquals(request.ToBytes(), PaymentRequest.Load(request.ToBytes()).ToBytes()); Assert.True(PaymentRequest.Load(request.ToBytes()).VerifySignature()); } }
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(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.WriteTo(context.Response.OutputStream); } else Assert.False(true, "Impossible"); context.Response.Close(); _Listener.BeginGetContext(ListenerCallback, null); } catch(Exception) { if(!_Stopped) throw; } }
private static void CanCreatePaymentRequestCore(object cert) { var request = new PaymentRequest(); request.Details.Memo = "hello"; request.Sign(cert, PKIType.X509SHA256); Assert.NotNull(request.MerchantCertificate); #if WIN Assert.False(new X509Certificate2(request.MerchantCertificate, "", X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable).HasPrivateKey); #endif Assert.True(request.VerifySignature()); Assert.False(request.VerifyChain()); AssertEx.CollectionEquals(request.ToBytes(), PaymentRequest.Load(request.ToBytes()).ToBytes()); Assert.True(PaymentRequest.Load(request.ToBytes()).VerifySignature()); }