IRONSOFTWAREHOME
開發者更新

Stripe.Net 與 IronPDF 的整合

Jacob Mellor, Chief Technology Officer @ Team Iron
Jacob Mellor
Updated: 2026年4月21日

Stripe.Net 是一個功能強大的 .NET 程式庫,允許開發者將 Stripe 的支付處理能力整合到 .NET 應用程式中。 Stripe 是一個流行的支付網關,使企業能夠接受線上付款。使用 Stripe.Net,開發者可以使用 Stripe API 提供的強大功能來管理交易、客戶、訂閱等。 在本文中,我們將討論如何使用 Stripe 與 IronPDF 來建立 PDF。

開始使用 Stripe.Net

建立一個新的Visual Studio項目

要開始使用 Stripe.Net,我們需要建立一個新的 Visual Studio 專案或打開現有的專案。 在本教程中,我們將使用一個控制台應用程式專案。

  1. 打開 Visual Studio 並點擊 "建立新專案"。

    Stripe .NET (How It Works For Developers): Figure 1 - Open Visual Studio and click on "Create a new project" option.

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

    Stripe .NET (用於開發者的工作方式):圖 2 - 選擇 C# 控制台應用程式作為專案型別。 點擊下一步。

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

    Stripe .NET (用於開發者的工作方式):圖 3 - 配置您的專案,指定專案名稱、位置和解決方案名稱。 然後點擊下一步。

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

    Stripe .NET (用於開發者的工作方式):圖 4 - 選擇適合您的專案的 .NET Framework,然後點擊建立。

就這樣,您的新 Visual Studio 控制台應用程式專案已建立。

安裝

要在您的專案中開始使用 Stripe.Net,您需要通過 NuGet 安裝 Stripe.Net 套件。 您可以使用套件管理器控制台或 Visual Studio 中的 NuGet 套件管理器執行此操作。

使用套件管理器控制台:

PM > 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";

使用 Stripe.Net 的基本操作

建立客戶

在使用 Stripe.Net 時,建立客戶是一個基本操作。 客戶可以與支付方法和訂閱相關聯。

var options = new CustomerCreateOptions
{
    Email = "customer@example.com",
    Name = "John Doe",
};
var service = new CustomerService();
Customer customer = service.Create(options);

輸出 Stripe 儀表板

Stripe .NET (用於開發者的工作方式):圖 5 - 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);

Stripe .NET (用於開發者的工作方式):圖 6 - Stripe 支付意向

高級功能

訂閱

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);

Stripe .NET (用於開發者的工作方式):圖 7 - Stripe 訂閱服務

處理爭議

爭議發生在客戶向其銀行或信用卡公司質疑費用時。 Stripe.Net 允許您列出、檢索和回應爭議。

var service = new DisputeService();
Dispute dispute = service.Get("dp_123456789");

最佳實踐

  1. 安全性: 總是確保您的 API 金鑰安全,並且永遠不要在源文件中硬編碼它們。
  2. 錯誤處理: 實施強大的錯誤處理措施以管理異常和失敗的 API 呼叫。
  3. 測試: 使用 Stripe 的測試模式並提供測試卡號來徹底測試您的整合。
  4. 文件: 請參閱官方 Stripe API 文件和 Stripe.Net 程式庫文件以獲取最新的資訊和範例。

Introducing IronPDF for C#

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");
    }
}

主要功能

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 的步驟:

  1. 在 Visual Studio 中打開您的 ASP.NET 專案並前往 "工具" 選單。

  2. 選擇 "NuGet 套件管理器",然後點擊 "管理解決方案的 NuGet 套件"。

  3. 在 "瀏覽" 標籤中,搜尋 "IronPDF" 並選擇所需版本。 點擊 "安裝" 將套件新增到您的專案中。 IronPDF 和其依賴項將自動下載和整合,允許您在 ASP.NET 應用程式中無縫利用其功能。

    Stripe .NET (How It Works For Developers): Figure 9 - Install IronPDF using the Manage NuGet Package for Solution by searching "IronPDF" in the search bar of NuGet Package Manager, then select the project and click on the Install button.

處理付款並生成發票

這是一個完整的範例,展示使用 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();
    }
}

輸出

Stripe .NET (用於開發者的工作方式):圖 10 - 使用 IronPDF 通過 Stripe 服務生成的 PDF 發票

結論

Stripe.Net 是一個全面且強大的程式庫,簡化了將 Stripe 的支付處理整合到 .NET 應用程式的過程。 其功能從基本交易處理到管理訂閱和爭議,涵蓋了廣泛的支付相關需求。

IronPDF 補充了 Stripe.Net,使開發者能夠生成、編輯和管理 PDF 文件。 這些程式庫一起提供了一個強大的解決方案,用於處理支付和生成對應的文件在 .NET 應用程式中。

通過利用 Stripe.NetIronPDF 的功能,開發者可以建立流暢且高效的工作流程,涵蓋從支付處理到文件生成的各個方面,提升他們應用程式的整體功能和使用者體驗。

IronPDF 提供了開發者一個機會來測試其豐富的功能,通過提供免費試用 IronPDF

IronPDF 提供客戶支持和更新,以及程式碼範例和完整的文件,以幫助使用者充分利用它。 要進一步探索此主題,請參閱我們的HTML 到 PDF 轉換與 IronPDF的詳盡教程。

Jacob Mellor, Chief Technology Officer @ Team Iron
Chief Technology Officer

Jacob Mellor is Chief Technology Officer at Iron Software and a visionary engineer pioneering C# PDF technology. As the original developer behind Iron Software's core codebase, he has shaped the company's product architecture since its inception, transforming it alongside CEO Cameron Rimington into a 50+ person company serving NASA, Tesla, and global government agencies.

...
Read More

Related Articles

Key in blue circle

立即免費取得 30 天試用金鑰

bullet_checked無需信用卡或建立帳號
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預訂您的免費現場演示
Booking Badge related to IronPDF Product Demo

受到全球數百萬工程師的信任

Iron Software的客戶標誌
獲取您的無義務諮詢
填寫以下表格或電子郵件sales@ironsoftware.com
您的詳細資訊將始終保密
受到全球數百萬工程師的信任
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立