在實際環境中測試
在生產環境中測試無浮水印。
在任何需要的地方都能運作。
Stripe.Net
是一個強大的 .NET 程式庫,讓開發者能夠將 Stripe 的支付處理功能整合到 .NET 應用程式中。Stripe 是一個受歡迎的支付閘道,能夠讓企業在線上接受支付。使用 Stripe.Net
,開發者可以利用 Stripe API 提供的強大功能來管理交易、顧客、訂閱等。在本文中,我們將討論如何使用 Stripe 與 IronPDF 來創建 PDF。
要開始使用 Stripe.Net
,我們需要建立一個新的 Visual Studio 專案或打開已經存在的專案。在本教程中,我們將使用主控台應用程式專案。
打開 Visual Studio 並點擊 "建立新專案"。
將會出現一個新視窗。選擇控制台應用程式,然後點擊下一步。
下一個視窗中,輸入你的專案名稱並選擇位置,然後點擊下一步。
![Stripe .NET(開發人員指南):圖3 - 配置你的專案,指定專案名稱、位置和解決方案名稱。然後點擊「下一步」。](/static-assets/pdf/blog/stripe-net/stripe-net-3.webp)
在下一個窗口中,選擇框架並點擊創建。
就這樣,您的新 Visual Studio 主控台應用程式專案已建立。
要在您的專案中開始使用 Stripe.Net
,您需要通過 NuGet 安裝 Stripe.Net 套件。您可以使用套件管理員控制台或 Visual Studio 中的 NuGet 套件管理器來完成此操作。
使用套件管理員控制台:
Install-Package Stripe.net
或
dotnet add package Stripe.net
使用 NuGet 套件管理器,搜索 "Stripe.net" 並安裝該套件。
安裝完成後,您需要配置您的 Stripe API 金鑰,該金鑰可以在您的 Stripe 帳戶中找到。此金鑰對於驗證您對 Stripe API 的請求至關重要。通常,該金鑰會出於安全考量存儲在配置文件或環境變量中。
以下是設置您的 API 金鑰的一個示例:
StripeConfiguration.ApiKey = "your_secret_api_key";
StripeConfiguration.ApiKey = "your_secret_api_key";
StripeConfiguration.ApiKey = "your_secret_api_key"
在使用 Stripe.Net
時,建立客戶是基本操作之一。客戶可以與支付方式和訂閱相關聯。
var options = new CustomerCreateOptions
{
Email = "customer@example.com",
Name = "John Doe",
};
var service = new CustomerService();
Customer customer = service.Create(options);
var options = new CustomerCreateOptions
{
Email = "customer@example.com",
Name = "John Doe",
};
var service = new CustomerService();
Customer customer = service.Create(options);
Dim options = New CustomerCreateOptions With {
.Email = "customer@example.com",
.Name = "John Doe"
}
Dim service = New CustomerService()
Dim customer As Customer = service.Create(options)
PaymentIntent
是代表 Stripe 中支付過程的對象。它旨在跟踪從創建到完成的支付生命週期。
var options = new PaymentIntentCreateOptions
{
Amount = 2000,
Currency = "usd",
PaymentMethodTypes = new List<string>
{
"card",
},
};
var service = new PaymentIntentService();
PaymentIntent paymentIntent = service.Create(options);
var options = new PaymentIntentCreateOptions
{
Amount = 2000,
Currency = "usd",
PaymentMethodTypes = new List<string>
{
"card",
},
};
var service = new PaymentIntentService();
PaymentIntent paymentIntent = service.Create(options);
Dim options = New PaymentIntentCreateOptions With {
.Amount = 2000,
.Currency = "usd",
.PaymentMethodTypes = New List(Of String) From {"card"}
}
Dim service = New PaymentIntentService()
Dim paymentIntent As PaymentIntent = service.Create(options)
Stripe 支援各種訂閱模式,通過 Stripe.Net
管理訂閱非常簡單。您可以創建、更新和取消訂閱。
var options = new SubscriptionCreateOptions
{
Customer = "cus_123456789",
Items = new List<SubscriptionItemOptions>
{
new SubscriptionItemOptions
{
Plan = "plan_123456789",
},
},
};
var service = new SubscriptionService();
Subscription subscription = service.Create(options);
var options = new SubscriptionCreateOptions
{
Customer = "cus_123456789",
Items = new List<SubscriptionItemOptions>
{
new SubscriptionItemOptions
{
Plan = "plan_123456789",
},
},
};
var service = new SubscriptionService();
Subscription subscription = service.Create(options);
Dim options = New SubscriptionCreateOptions With {
.Customer = "cus_123456789",
.Items = New List(Of SubscriptionItemOptions) From {
New SubscriptionItemOptions With {.Plan = "plan_123456789"}
}
}
Dim service = New SubscriptionService()
Dim subscription As Subscription = service.Create(options)
當客戶對其銀行或信用卡公司提出異議時,會發生爭議。 Stripe.Net
允許您列出、檢索和回應爭議。
var service = new DisputeService();
Dispute dispute = service.Get("dp_123456789");
var service = new DisputeService();
Dispute dispute = service.Get("dp_123456789");
Dim service = New DisputeService()
Dim dispute As Dispute = service.Get("dp_123456789")
安全性: 始終保護您的API密鑰,不要將它們硬編碼在您的源文件中。
錯誤處理: 實施穩健的錯誤處理,以管理異常和失敗的API調用。
測試: 使用Stripe的測試模式並提供測試卡號碼,徹底測試您的集成。
Stripe.Net
庫文檔,以獲取最新信息和範例。IronPDF
是一個 C# 函式庫,允許開發者創建、編輯和提取 PDF 文件中的內容。它是生成 .NET 應用程序中 PDF 的理想工具,無論是用於報告、發票,還是其他文檔需求。
IronPDF 允許開發人員輕鬆地通過將 HTML 字串、網址和 HTML 檔案轉換為 PDF 來創建 PDF 文件。
輕鬆編輯現有的 PDF 文件。IronPDF 允許您操作現有的 PDF 檔案,例如:在特定索引處添加頁面、複製或刪除頁面、拆分 PDF,以及提取頁面以創建新的 PDF 等。
IronPDF 的合併功能允許開發人員將兩個或更多 PDF 文件合併為一個。
IronPDF 讓用戶可以為 PDF 添加密碼和權限,以增強 PDF 的安全性。
IronPDF 支援 PDF 文件的 128 位元加密、解密和密碼保護。
開發人員可以使用 IronPDF 程式設計方式向 PDF 添加數位簽章。它支援使用 .pfx
和 .p12
格式的數位簽名證書來簽署 PDF 的多種方法。
讓我們創建一個實際範例,在使用 Stripe.Net
處理付款後,使用 IronPDF 生成 PDF 發票。
使用 NuGet 包管理器安裝 IronPDF 的步驟:
在 Visual Studio 中打開您的 ASP.NET 專案,然後導航到“工具”菜單。
選擇“NuGet 包管理器”,然後點擊“為解決方案管理 NuGet 包”。
在“瀏覽”選項卡中,搜索“IronPDF”並選擇所需版本。點擊“安裝”將該包添加到您的專案中。IronPDF 及其依賴項將自動下載並集成,允許您在 ASP.NET 應用程式中無縫使用其功能。
以下是一個完整的示例,展示了如何使用 Stripe.Net
API 創建新付款並使用 IronPDF
生成 PDF 發票。
using Stripe;
using IronPdf;
using System;
public class PaymentService
{
public void ProcessPaymentAndGenerateInvoice()
{
// Configure Stripe API key
StripeConfiguration.ApiKey = "your_secret_key";
// Create a PaymentIntent
var paymentIntentOptions = new PaymentIntentCreateOptions
{
Amount = 2000,
Currency = "usd",
PaymentMethodTypes = new List<string> { "card" },
};
var paymentIntentService = new PaymentIntentService();
PaymentIntent paymentIntent = paymentIntentService.Create(paymentIntentOptions);
// Assuming payment succeeded, create a PDF invoice
GeneratePdfInvoice(paymentIntent);
}
private void GeneratePdfInvoice(PaymentIntent paymentIntent)
{
// Create HTML content for the invoice
var htmlContent = $@"
<html>
<head>
<title>Invoice</title>
</head>
<body>
<h1>Invoice</h1>
<p>Payment ID: {paymentIntent.Id}</p>
<p>Amount: {paymentIntent.Amount / 100.0:C}</p>
<p>Status: {paymentIntent.Status}</p>
</body>
</html>";
// Convert HTML to PDF
var renderer = new ChromePdfRenderer();
var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Save PDF to file
var filePath = "invoice.pdf";
pdfDocument.SaveAs(filePath);
Console.WriteLine($"Invoice saved to {filePath}");
}
}
class Program
{
static void Main(string[] args)
{
var service = new PaymentService();
service.ProcessPaymentAndGenerateInvoice();
}
}
using Stripe;
using IronPdf;
using System;
public class PaymentService
{
public void ProcessPaymentAndGenerateInvoice()
{
// Configure Stripe API key
StripeConfiguration.ApiKey = "your_secret_key";
// Create a PaymentIntent
var paymentIntentOptions = new PaymentIntentCreateOptions
{
Amount = 2000,
Currency = "usd",
PaymentMethodTypes = new List<string> { "card" },
};
var paymentIntentService = new PaymentIntentService();
PaymentIntent paymentIntent = paymentIntentService.Create(paymentIntentOptions);
// Assuming payment succeeded, create a PDF invoice
GeneratePdfInvoice(paymentIntent);
}
private void GeneratePdfInvoice(PaymentIntent paymentIntent)
{
// Create HTML content for the invoice
var htmlContent = $@"
<html>
<head>
<title>Invoice</title>
</head>
<body>
<h1>Invoice</h1>
<p>Payment ID: {paymentIntent.Id}</p>
<p>Amount: {paymentIntent.Amount / 100.0:C}</p>
<p>Status: {paymentIntent.Status}</p>
</body>
</html>";
// Convert HTML to PDF
var renderer = new ChromePdfRenderer();
var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Save PDF to file
var filePath = "invoice.pdf";
pdfDocument.SaveAs(filePath);
Console.WriteLine($"Invoice saved to {filePath}");
}
}
class Program
{
static void Main(string[] args)
{
var service = new PaymentService();
service.ProcessPaymentAndGenerateInvoice();
}
}
Imports Stripe
Imports IronPdf
Imports System
Public Class PaymentService
Public Sub ProcessPaymentAndGenerateInvoice()
' Configure Stripe API key
StripeConfiguration.ApiKey = "your_secret_key"
' Create a PaymentIntent
Dim paymentIntentOptions = New PaymentIntentCreateOptions With {
.Amount = 2000,
.Currency = "usd",
.PaymentMethodTypes = New List(Of String) From {"card"}
}
Dim paymentIntentService As New PaymentIntentService()
Dim paymentIntent As PaymentIntent = paymentIntentService.Create(paymentIntentOptions)
' Assuming payment succeeded, create a PDF invoice
GeneratePdfInvoice(paymentIntent)
End Sub
Private Sub GeneratePdfInvoice(ByVal paymentIntent As PaymentIntent)
' Create HTML content for the invoice
'INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
Dim htmlContent = $"
<html>
<head>
<title>Invoice</title>
</head>
<body>
<h1>Invoice</h1>
<p>Payment ID: {paymentIntent.Id}</p>
<p>Amount: {paymentIntent.Amount / 100.0:C}</p>
<p>Status: {paymentIntent.Status}</p>
</body>
</html>"
' Convert HTML to PDF
Dim renderer = New ChromePdfRenderer()
Dim pdfDocument = renderer.RenderHtmlAsPdf(htmlContent)
' Save PDF to file
Dim filePath = "invoice.pdf"
pdfDocument.SaveAs(filePath)
Console.WriteLine($"Invoice saved to {filePath}")
End Sub
End Class
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim service = New PaymentService()
service.ProcessPaymentAndGenerateInvoice()
End Sub
End Class
Stripe.Net
是一個全面且強大的庫,它簡化了在 .NET 應用程序中整合 Stripe 的支付處理。從基本的交易處理到管理訂閱和爭議的特性,它涵蓋了廣泛的支付相關需求。
IronPDF
通過使開發人員能夠生成、編輯和管理 PDF 文檔來補充 Stripe.Net
。這些庫一起為處理支付和生成相應文檔的 .NET 應用程序提供了強大的解決方案。
通過利用 Stripe.Net
和 IronPDF
的能力,開發人員可以創建無縫且高效的工作流程,處理從支付處理到文檔生成的所有內容,增強應用程序的整體功能和用戶體驗。
IronPDF 提供免費試用頁面,讓開發人員有機會測試其廣泛的功能。
IronPDF 提供客戶支持和更新,並提供代碼示例和詳細的文檔,以幫助用戶充分利用它。要進一步探索該主題,請參閱我們關於 HTML 到 PDF 轉換的全面教程。