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,然後按一下"建立新專案"。
將出現一個新視窗。 選擇"控制台應用程式",然後按一下"下一步"。
![Stripe .NET(開發人員的工作原理):圖 2 - 選擇 C# 控制台應用程式作為專案類型。 點選"下一步"。
在下一個視窗中,輸入項目名稱並選擇位置,然後按一下"下一步"。
![Stripe .NET(開發人員的工作原理):圖 3 - 透過指定專案名稱、位置和解決方案名稱來配置您的專案。 然後點選"下一步"。
在下一個視窗中,選擇框架並按一下"建立"。
Stripe .NET(開發人員的使用方法):圖 4 - 為您的專案選擇合適的 .NET Framework,然後按一下"建立"。
就這樣,你的新 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";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);輸出 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);進階功能
訂閱
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);處理糾紛
當客戶對銀行或信用卡公司的某項收費提出質疑時,就會發生爭議。 Stripe.Net可讓您列出、檢索和回覆爭議。
var service = new DisputeService();
Dispute dispute = service.Get("dp_123456789");var service = new DisputeService();
Dispute dispute = service.Get("dp_123456789");最佳實踐
1.安全性:務必保護您的 API 金鑰,切勿將其硬編碼到原始檔案中。 2.錯誤處理:實作強大的錯誤處理機制來管理異常和失敗的 API 呼叫。 3.測試:使用 Stripe 的測試模式並提供測試卡號,徹底測試您的整合。 4.文檔:請參閱 Stripe 官方 API 文件和Stripe.Net庫文檔,以取得最新資訊和範例。
推出適用於 C# 的 IronPDF
Stripe .NET(開發者使用指南):圖 8 - IronPDF for .NET:C# PDF 函式庫
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");
}
}主要特點
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 支援 PDF 文件的 128 位元加密、解密和密碼保護。
6. 對 PDF 文件進行數位簽名
開發者可以使用 IronPDF 以程式設計方式為 PDF 添加數位簽章。 它支援多種方式使用.pfx和.p12格式的數位簽章憑證對 PDF 進行簽章。
範例:使用 Stripe.Net 和 IronPDF 產生 PDF 發票
讓我們建立一個實際範例,在透過Stripe.Net處理付款後,使用 IronPDF 產生 PDF 發票。
安裝 IronPDF .NET 函式庫
使用 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();
}
}輸出
Stripe .NET(開發者使用指南):圖 10 - 透過 Stripe 服務使用 IronPDF 產生的 PDF 發票
結論
Stripe.Net是一個功能強大且全面的程式庫,可簡化將 Stripe 的支付處理功能整合到 .NET 應用程式中的流程。 它具備從基本交易處理到管理訂閱和糾紛等一系列功能,涵蓋了廣泛的支付相關需求。
IronPDF與Stripe.Net相輔相成,使開發人員能夠產生、編輯和管理 PDF 文件。 這些程式庫共同為 .NET 應用程式中的支付處理和相應文件產生提供了一個強大的解決方案。
透過利用Stripe.Net和IronPDF的功能,開發人員可以創建無縫且有效率的工作流程,處理從支付處理到文件產生的一切,從而增強應用程式的整體功能和使用者體驗。
IronPDF 為開發者提供機會,讓他們可以透過免費試用 IronPDF來測試其豐富的功能。
IronPDF 提供客戶支援和更新,以及程式碼範例和詳盡的文檔,以幫助用戶充分利用其功能。 若要進一步了解主題,請參閱我們使用 IronPDF 將 HTML 轉換為 PDF 的詳細教學。
常見問題解答
如何將支付處理功能整合到 .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 應用程式中處理付款後產生文件或發票?
使用 Stripe.Net 處理付款後,您可以使用 IronPDF 將 HTML 內容轉換為 PDF 文件,從而產生文件或發票,為您的帳單需求提供專業的輸出。
將 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 提供的高級功能包括處理付款糾紛、管理定期帳單以及實施具有強大錯誤處理功能的安全支付處理。
如何在.NET應用程式中將HTML內容轉換為PDF?
在 .NET 應用程式中,您可以使用 IronPDF 的方法(例如RenderHtmlAsPdf用於 HTML 字串或RenderHtmlFileAsPdf用於 HTML 檔案)將 HTML 內容轉換為 PDF。







