跳過到頁腳內容
.NET幫助

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 專案或開啟現有的專案。 在本教程中,我們將使用 Console Application 專案。

1.開啟 Visual Studio 並按一下"建立新專案"。

!<a href="/static-assets/pdf/blog/stripe-net/stripe-net-1.webp">Stripe .NET (How It Works For Developers):圖 1 - 開啟 Visual Studio 並點選"建立新專案"選項。

2.將會出現新視窗。 選擇控制台應用程式,然後按一下下一步。

![Stripe .NET (How It Works For Developers):圖 2 - 選擇 C# Console App 作為專案類型。 點選下一步。](/static-assets/pdf/blog/stripe-net/stripe-net-2.webp)

3.在下一個視窗中,輸入專案名稱並選擇位置,然後按一下下一步。

![Stripe .NET (How It Works For Developers):圖 3 - 透過指定專案名稱、位置和解決方案名稱來設定您的專案。 然後按下一步。](/static-assets/pdf/blog/stripe-net/stripe-net-3.webp)

4.在下一個視窗中,選擇框架,然後按一下建立。

![Stripe .NET (How It Works For Developers):圖 4 - 為您的專案選擇適當的 .NET Framework,然後按一下"建立"](/static-assets/pdf/blog/stripe-net/stripe-net-4.webp)。

就這樣,您的新 Visual Studio Console Application 專案就建立了。

安裝

要開始在您的專案中使用 Stripe.Net ,您需要透過 NuGet 安裝 Stripe.Net 套件。 您可以使用套件管理員控制台或 Visual Studio 中的 NuGet 套件管理員來完成此工作。

使用套件管理員控制台:

Install-Package Stripe.net

dotnet add package Stripe.net

使用 NuGet Package Manager,搜尋"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"
$vbLabelText   $csharpLabel

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)
$vbLabelText   $csharpLabel

輸出 Stripe 控制面板

Stripe .NET (How It Works For Developers):圖 5 - Stripe Dashboard for Customers

建立付款意向

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)
$vbLabelText   $csharpLabel

Stripe .NET (How It Works For Developers):圖 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);
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)
$vbLabelText   $csharpLabel

Stripe .NET (How It Works For Developers):圖 7 - Stripe 訂閱服務

處理爭議

當客戶向銀行或信用卡公司質疑某項收費時,就會發生爭議。 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")
$vbLabelText   $csharpLabel

最佳實務

1.安全性:請務必保護您的 API 金鑰,切勿在原始碼檔案中硬性編碼。 2.錯誤處理:實施強大的錯誤處理,以管理異常和失敗的 API 呼叫。 3.測試:使用 Stripe 的測試模式並提供測試卡號,徹底測試您的整合。 4.文件:請參考 Stripe API 官方文件和 Stripe.Net 函式庫文件,以取得最新資訊和範例。

介紹 IronPDF for C#。

Stripe .NET (How It Works For Developers):圖 8 - IronPDF for .NET:C# PDF 函式庫

IronPDF 是一個 C# 函式庫,可讓開發人員建立、編輯 PDF 文件,並從 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
$vbLabelText   $csharpLabel

主要功能

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 for .NET 函式庫。

使用 NuGet Package Manager 安裝 IronPDF 的步驟:

1.在 Visual Studio 中開啟您的 ASP.NET 專案,並導航至"工具"功能表。 2.選擇 "NuGet Package Manager",然後按一下 "Manage NuGet Packages for Solution"。 3.在"瀏覽"標籤中,搜尋"IronPDF"並選擇所需版本。 按一下"安裝"即可將套件新增至您的專案。 IronPDF for .NET 及其相依性將會自動下載與整合,讓您可以在 ASP.NET 應用程式中無縫地開始運用其功能。

![Stripe .NET (How It Works For Developers):圖 9 - 使用"Manage NuGet Package for Solution"安裝 IronPDF,方法是在 NuGet Package Manager 的搜尋列中搜尋"IronPDF",然後選擇專案並點選安裝按鈕](/static-assets/pdf/blog/stripe-net/stripe-net-9.webp)。

處理付款並產生發票

以下是一個完整的範例,示範使用 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
$vbLabelText   $csharpLabel

輸出

Stripe .NET (How It Works For Developers):圖 10 - 透過 Stripe 服務使用 IronPDF 產生 PDF 發票

結論

Stripe.Net是一個全面且功能強大的函式庫,可簡化將 Stripe 的付款處理整合至 .NET 應用程式的作業。 其功能範圍從基本的交易處理到管理訂閱和糾紛,涵蓋了廣泛的付款相關需求。

IronPDF for .NETStripe.Net 的補充,使開發人員能夠生成、編輯和管理 PDF 文件。 這些函式庫一起提供了強大的解決方案,可在 .NET 應用程式中處理付款並產生相對應的文件。

透過利用 Stripe.NetIronPDF 兩者的功能,開發人員可以建立無縫且有效率的工作流程,處理從付款處理到文件產生的所有工作,強化應用程式的整體功能和使用者體驗。

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 檔案,產生文件或發票,提供專業的輸出,滿足您的帳單需求。

在 Stripe.Net 中使用 C# PDF 函式庫有什麼好處?

使用 IronPDF for .NET 之類的 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 的方法將 HTML 內容轉換為 PDF,例如使用 RenderHtmlAsPdf 來轉換 HTML 字串,或使用 RenderHtmlFileAsPdf 來轉換 HTML 檔案。

Jacob Mellor, Team Iron 首席技术官
首席技术官

Jacob Mellor 是 Iron Software 的首席技術官,作為 C# PDF 技術的先鋒工程師。作為 Iron Software 核心代碼的原作者,他自開始以來塑造了公司產品架構,與 CEO Cameron Rimington 一起將其轉變為一家擁有超過 50 名員工的公司,為 NASA、特斯拉 和 全世界政府機構服務。

Jacob 持有曼徹斯特大學土木工程一級榮譽学士工程學位(BEng) (1998-2001)。他於 1999 年在倫敦開設了他的第一家軟件公司,並於 2005 年製作了他的首個 .NET 組件,專注於解決 Microsoft 生態系統內的複雜問題。

他的旗艦產品 IronPDF & Iron Suite .NET 庫在全球 NuGet 被安裝超過 3000 萬次,其基礎代碼繼續為世界各地的開發工具提供動力。擁有 25 年的商業經驗和 41 年的編碼專業知識,Jacob 仍專注於推動企業級 C#、Java 及 Python PDF 技術的創新,同時指導新一代技術領袖。