.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 專案或打開現有的專案。 在本教程中,我們將使用一個主控台應用程式專案。

  1. 打開 Visual Studio,然後點擊「建立新專案」。

    Stripe .NET(它如何為開發人員工作):圖1 - 打開 Visual Studio,然後點擊「建立新專案」選項。

  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 套件管理員來執行此操作。

使用套件管理器主控台:

Install-Package Stripe.net
Install-Package Stripe.net
SHELL

dotnet add package Stripe.net
dotnet add package Stripe.net
SHELL

使用 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

主要功能

HTML 轉 PDF

IronPDF可以讓開發人員輕鬆創建PDF文件,透過將HTML字串、URL和HTML文件轉換為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 發票

讓我們創建一個實用的範例,使用 IronPDF 在處理 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。

處理付款和生成發票

以下是一個完整的範例,演示如何使用Stripe.Net API創建新的支付並使用IronPDF生成PDF發票。

using Stripe;
using IronPdf;
using System;

public class PaymentService
{
    public void ProcessPaymentAndGenerateInvoice()
    {
        // Configure Stripe API key
        StripeConfiguration.ApiKey = "your_secret_key";

        // Create a PaymentIntent
        var paymentIntentOptions = new PaymentIntentCreateOptions
        {
            Amount = 2000,
            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 HTML to PDF
        var renderer = new ChromePdfRenderer();
        var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);

        // Save PDF to 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;

public class PaymentService
{
    public void ProcessPaymentAndGenerateInvoice()
    {
        // Configure Stripe API key
        StripeConfiguration.ApiKey = "your_secret_key";

        // Create a PaymentIntent
        var paymentIntentOptions = new PaymentIntentCreateOptions
        {
            Amount = 2000,
            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 HTML to PDF
        var renderer = new ChromePdfRenderer();
        var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);

        // Save PDF to 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

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 HTML to PDF
		Dim renderer = New ChromePdfRenderer()
		Dim pdfDocument = renderer.RenderHtmlAsPdf(htmlContent)

		' Save PDF to 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 - 使用 IronPDF 通過 Stripe 服務生成的 PDF 發票

結論

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

IronPDF 通過讓開發人員生成、編輯和管理 PDF 文件來補充 Stripe.Net。 這些函式庫共同為 .NET 應用程式中的付款處理和生成相應文檔提供了一個強大的解決方案。

通過利用Stripe.NetIronPDF兩者的功能,開發人員可以創建無縫且高效的工作流程,從支付處理到文件生成,提升其應用程序的整體功能和用戶體驗。

IronPDF 透過提供IronPDF 的免費試用,讓開發者有機會測試其豐富的功能。

IronPDF 提供客戶支持和更新,以及代碼示例和詳細的文檔,幫助用戶充分利用其功能。 要進一步探索這個主題,請參考我們關於使用IronPDF進行HTML轉換為PDF的詳細教程。

Chipego
奇佩戈·卡林达
軟體工程師
Chipego 擁有天生的傾聽技能,這幫助他理解客戶問題,並提供智能解決方案。他在獲得信息技術理學學士學位後,于 2023 年加入 Iron Software 團隊。IronPDF 和 IronOCR 是 Chipego 專注的兩個產品,但隨著他每天找到新的方法來支持客戶,他對所有產品的了解也在不斷增長。他喜歡在 Iron Software 的協作生活,公司內的團隊成員從各自不同的經歷中共同努力,創造出有效的創新解決方案。當 Chipego 離開辦公桌時,他常常享受讀好書或踢足球的樂趣。
< 上一頁
Papercut SMTP C#(開發人員如何使用)
下一個 >
TensorFlow .NET(它對開發者的運作方式)