跳過到頁腳內容
.NET幫助

Stripe .NET(對於開發者的運行原理)

Stripe.Net Integration with IronPDF

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 (它如何為開發人員工作):圖1 - 打開 Visual Studio 並點擊“創建新專案”選項。

  1. 會出現一個新窗口。 選擇控制台應用程式,然後點擊下一步。

Stripe .NET (它如何為開發人員工作):圖2 - 選擇 C# 控制台應用程式作為專案類型。 點擊下一步。

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

Stripe .NET (它如何為開發人員工作):圖3 - 通過指定專案名稱、位置和解決方案名稱來配置您的專案。 然後點擊下一步。

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

Stripe .NET (它如何為開發人員工作):圖4 - 選擇適合您專案的 .NET 框架,然後點擊創建。

這樣,您的新 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"
$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 (它如何為開發人員工作):圖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);
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 (它如何為開發人員工作):圖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 (它如何為開發人員工作):圖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 (它如何為開發人員工作):圖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");
    }
}
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 處理支付後生成 PDF 發票。

安裝 IronPDF .NET 庫

使用 NuGet 套件管理器安裝 IronPDF 的步驟:

  1. 在 Visual Studio 中打開您的 ASP.NET 項目,導航到“工具”菜單。
  2. 選擇“NuGet 包管理器”,然後點擊“為解決方案管理 NuGet 包”。
  3. 在“瀏覽”選項卡中,搜索“IronPDF”並選擇所需的版本。 單擊“安裝”將包添加到您的項目中。 IronPDF 及其依賴項將自動下載並集成,使您能夠在您的 ASP.NET 應用程序中無縫利用其功能。

Stripe .NET (它如何為開發人員工作):圖9 - 使用方案的 NuGet 套件管理器搜索欄搜索“ IronPDF ”來安裝 IronPDF ,然後選擇項目並點擊安裝按鈕。

處理支付和生成發票

以下是創建新支付並使用 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 (它如何為開發人員工作):圖10 - 通過 Stripe 服務使用 IronPDF 生成的 PDF 發票

結論

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

IronPDF 補充 Stripe.Net,使開發人員能夠生成、編輯和管理 PDF 文檔。 這些庫協同工作,為在 .NET 應用程式中處理支付和生成相應文檔提供了強大的解決方案。

通過利用 Stripe.NetIronPDF 的能力,開發人員可以創建從支付處理到文檔生成的無縫高效的工作流程,增強其應用程式的整體功能和用戶體驗。

IronPDF 提供開發人員機會通過提供 IronPDF 的免費試用來測試其廣泛的功能。

IronPDF 提供客戶支持和更新,並附有代碼範例和詳細文件以幫助用戶最大限度地利用它。 要進一步探索這個主題,請參閱我們關於 HTML 到 PDF 轉換與 IronPDF 的詳細教程。

常見問題解答

我如何將支付處理集成到.NET應用程式中?

您可以使用`Stripe.Net`庫將支付處理集成到.NET應用程式中,該庫允許您在應用程式中管理支付、創建客戶和處理賬單。

設置Stripe.Net在一個新的Visual Studio專案中涉及哪些步驟?

要在一個新的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?

您可以使用IronPDF的方法,例如RenderHtmlAsPdf將HTML字符串或RenderHtmlFileAsPdf將HTML文件轉換為PDF。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。