.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

主要功能

1.HTML 至 PDF

IronPdf 允许开发人员将 HTML 字符串、URL 和 HTML 文件转换为 PDF,从而轻松创建 PDF 文档。

2. PDF编辑

轻松编辑现有 PDF 文档。 IronPDF 允许用户在特定索引处添加页面、复制或删除页面、分割 PDF 以及提取页面以创建新 PDF 等,从而对现有 PDF 进行操作。

3.PDF 合并

IronPDF 的合并功能允许开发人员将两个或多个 PDF 文档合并为一个文档。

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 包管理器安装 IronPDF 的步骤:

  1. 在 Visual Studio 中打开 ASP.NET 项目,并导航到 "工具 "菜单。

  2. 选择 "NuGet 包管理器",然后点击 "管理解决方案的 NuGet 包"。

  3. 在 "浏览 "选项卡中搜索 "IronPDF "并选择所需的版本。 点击 "安装 "将软件包添加到您的项目中。 IronPDF 及其附属程序将自动下载并集成,使您可以开始在 ASP.NET 应用程序中无缝利用其功能。

    Stripe .NET(开发人员如何使用):图9 - 通过在NuGet包管理器的搜索栏中搜索IronPDF,使用“为解决方案管理NuGet包”安装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(它是如何为开发人员工作的)