Stripe .NET(對於開發者的運行原理)
Stripe.Net 與 IronPDF 的整合
Stripe.Net 是一個功能強大的 .NET 程式庫,允許開發者將 Stripe 的支付處理能力整合到 .NET 應用程式中。 Stripe 是一個流行的支付網關,使企業能夠接受線上付款。使用 Stripe.Net,開發者可以使用 Stripe API 提供的強大功能來管理交易、客戶、訂閱等。 在本文中,我們將討論如何使用 Stripe 與 IronPDF 來建立 PDF。
開始使用 Stripe.Net
建立一個新的Visual Studio項目
要開始使用 Stripe.Net,我們需要建立一個新的 Visual Studio 專案或打開現有的專案。 在本教程中,我們將使用一個控制台應用程式專案。
-
打開 Visual Studio 並點擊 "建立新專案"。

-
新窗口將出現。 選擇控制台應用程式並點擊下一步。

-
在下一個窗口中,輸入您的專案名稱並選擇位置,然後點擊下一步。

-
在下一個窗口中,選擇框架並點擊建立。

就這樣,您的新 Visual Studio 控制台應用程式專案已建立。
安裝
要在您的專案中開始使用 Stripe.Net,您需要通過 NuGet 安裝 Stripe.Net 套件。 您可以使用套件管理器控制台或 Visual Studio 中的 NuGet 套件管理器執行此操作。
使用套件管理器控制台:
Install-Package Stripe.net
或
dotnet add package Stripe.net
using 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 的基本操作
建立客戶
在使用 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)
輸出 Stripe 儀表板

建立支付意向
一個 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 API 文件和
Stripe.Net程式庫文件以獲取最新的資訊和範例。
Introducing IronPDF for C

IronPDF 是一個 C# 程式庫,允許開發者建立、編輯和提取 PDF 文件中的內容。 這是一個在 .NET 應用程式中生成 PDF 的理想工具,無論是報告、發票或其他文件需求。
IronPDF 可以準確地將網頁、URL 和 HTML 轉換為 PDF 格式,使其成為從線上內容(如報告和發票)建立 PDF 文件的理想工具。
using IronPdf;
class Program
{
static void Main(string[] args)
{
var renderer = new ChromePdfRenderer();
// 1. Convert HTML String to PDF
var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");
// 2. Convert HTML File to PDF
var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");
// 3. Convert URL to PDF
var url = "http://ironpdf.com"; // Specify the URL
var pdfFromUrl = renderer.RenderUrlAsPdf(url);
pdfFromUrl.SaveAs("URLToPDF.pdf");
}
}
using IronPdf;
class Program
{
static void Main(string[] args)
{
var renderer = new ChromePdfRenderer();
// 1. Convert HTML String to PDF
var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");
// 2. Convert HTML File to PDF
var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");
// 3. Convert URL to PDF
var url = "http://ironpdf.com"; // Specify the URL
var pdfFromUrl = renderer.RenderUrlAsPdf(url);
pdfFromUrl.SaveAs("URLToPDF.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim renderer = New ChromePdfRenderer()
' 1. Convert HTML String to PDF
Dim htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>"
Dim pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent)
pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf")
' 2. Convert HTML File to PDF
Dim htmlFilePath = "path_to_your_html_file.html" ' Specify the path to your HTML file
Dim pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath)
pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf")
' 3. Convert URL to PDF
Dim url = "http://ironpdf.com" ' Specify the URL
Dim pdfFromUrl = renderer.RenderUrlAsPdf(url)
pdfFromUrl.SaveAs("URLToPDF.pdf")
End Sub
End Class
主要功能
1. HTML 轉 PDF
IronPDF 允許開發者通過將 HTML 字串、URL 和 HTML 文件轉換為 PDF 來輕鬆建立 PDF 文件。
2. PDF 編輯
輕鬆編輯現有的 PDF 文件。 IronPDF 允許您通過使使用者能夠在特定索引位置新增頁面、複製或刪除頁面、拆分 PDF 並提取頁面以建立新的 PDF 等方式對現有 PDF 進行操作。
3. PDF 合併
IronPDF 的合併功能允許開發者將兩個或多個 PDF 文件合併成一個。
4. PDF 安全
IronPDF 使使用者能夠為 PDF 新增密碼和權限以增強 PDF 安全性。
5. PDF 加密和解密
IronPDF 支援 128 位加密、解密和 PDF 文件的密碼保護。
6. 數位簽署 PDF 文件
開發者可以通過編程方式使用 IronPDF 向 PDF 新增數位簽名。 它支援使用 .pfx 和 .p12 格式的數位簽名證書以多種方式簽署 PDF。
範例:使用 Stripe.Net 和 IronPDF 生成 PDF 發票
讓我們建立一個實用的範例,在使用 Stripe.Net 處理付款後使用 IronPDF 生成 PDF 發票。
安裝 IronPDF .NET 程式庫
using NuGet 套件管理器安裝 IronPDF 的步驟:
- 在 Visual Studio 中打開您的 ASP.NET 專案並前往 "工具" 選單。
- 選擇 "NuGet 套件管理器",然後點擊 "管理解決方案的 NuGet 套件"。
-
在 "瀏覽" 標籤中,搜尋 "IronPDF" 並選擇所需版本。 點擊 "安裝" 將套件新增到您的專案中。 IronPDF 和其依賴項將自動下載和整合,允許您在 ASP.NET 應用程式中無縫利用其功能。

處理付款並生成發票
這是一個完整的範例,展示使用 Stripe.Net API 建立一個新的支付,並使用 IronPDF 生成 PDF 發票。
using Stripe;
using IronPdf;
using System;
using System.Collections.Generic;
public class PaymentService
{
public void ProcessPaymentAndGenerateInvoice()
{
// Configure Stripe API key
StripeConfiguration.ApiKey = "your_secret_key";
// Create a PaymentIntent
var paymentIntentOptions = new PaymentIntentCreateOptions
{
Amount = 2000, // Amount in cents
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 the HTML content to a PDF document
var renderer = new ChromePdfRenderer();
var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF document to a 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;
using System.Collections.Generic;
public class PaymentService
{
public void ProcessPaymentAndGenerateInvoice()
{
// Configure Stripe API key
StripeConfiguration.ApiKey = "your_secret_key";
// Create a PaymentIntent
var paymentIntentOptions = new PaymentIntentCreateOptions
{
Amount = 2000, // Amount in cents
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 the HTML content to a PDF document
var renderer = new ChromePdfRenderer();
var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF document to a 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
Imports System.Collections.Generic
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 the HTML content to a PDF document
Dim renderer = New ChromePdfRenderer()
Dim pdfDocument = renderer.RenderHtmlAsPdf(htmlContent)
' Save the PDF document to a 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 是一個全面且強大的程式庫,簡化了將 Stripe 的支付處理整合到 .NET 應用程式的過程。 其功能從基本交易處理到管理訂閱和爭議,涵蓋了廣泛的支付相關需求。
IronPDF 補充了 Stripe.Net,使開發者能夠生成、編輯和管理 PDF 文件。 這些程式庫一起提供了一個強大的解決方案,用於處理支付和生成對應的文件在 .NET 應用程式中。
通過利用 Stripe.Net 和 IronPDF 的功能,開發者可以建立流暢且高效的工作流程,涵蓋從支付處理到文件生成的各個方面,提升他們應用程式的整體功能和使用者體驗。
IronPDF 提供了開發者一個機會來測試其豐富的功能,通過提供免費試用 IronPDF。
IronPDF 提供客戶支持和更新,以及程式碼範例和完整的文件,以幫助使用者充分利用它。 要進一步探索此主題,請參閱我們的HTML 到 PDF 轉換與 IronPDF的詳盡教程。
常見問題
我如何在 .NET 應用程式中整合支付處理?
您可以使用 `Stripe.Net` 程式庫在 .NET 應用程式中整合支付處理,這允許您管理付款、建立客戶、並處理應用程式中的計費。
在新的 Visual Studio 專案中設定 Stripe.Net 需要遵循哪些步驟?
要在新的 Visual Studio 專案中設定 Stripe.Net,首先建立一個新專案,然後透過 NuGet 安裝 `Stripe.Net` 套件,並配置您的 Stripe API 金鑰以驗證 API 請求。
如何在 .NET 應用程式中處理訂閱管理?
Stripe.Net 提供內建的方法來管理訂閱,允許您直接透過 API 建立、更新和取消訂閱。
在 Stripe.Net 中保護 API 金鑰的最佳實踐是什麼?
在 Stripe.Net 中保護 API 金鑰的最佳實踐包括安全地使用環境變數或配置檔案儲存金鑰,並確保它們不被硬編碼在您的源程式碼中。
我如何在 .NET 應用程式中處理付款後生成文件或發票?
using Stripe.Net 處理付款後,您可以使用 IronPDF 將 HTML 內容轉換為 PDF 文件,以專業化地輸出您的計費需求。
using C# PDF 程式庫與 Stripe.Net 的好處是什麼?
使用像 IronPDF 這樣的 C# PDF 程式庫與 Stripe.Net 可以讓您輕鬆地建立、管理及自訂 PDF 文件,如發票,增強 .NET 應用程式的功能。
我如何在 Stripe.Net 中建立一個 PaymentIntent?
要在 Stripe.Net 中建立一個 PaymentIntent,使用 `PaymentIntentService` 類與 `PaymentIntentCreateOptions` 來指定付款詳情並追蹤付款生命週期。
我如何解決整合 Stripe.Net 時的常見問題?
整合 Stripe.Net 時常見的問題可以透過檢查 API 金鑰配置、確保正確使用 Stripe 程式庫方法、並檢查錯誤資訊以獲取具體指導來解決。
Stripe.Net 提供哪些高級功能?
Stripe.Net 提供的高級功能包括處理付款爭議、管理經常性計費以及實現具有強大錯誤處理的安全支付處理。
如何將 HTML 內容轉換成 .NET 應用程式中的 PDF?
您可以使用 IronPDF 的方法,如 RenderHtmlAsPdf 處理 HTML 字串或 RenderHtmlFileAsPdf 處理 HTML 文件,將 HTML 內容轉換成 PDF。




