.NET 帮助

Stripe .NET(开发人员如何使用)

发布 2024年七月1日
分享:

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,点击 "创建新项目 "选项。](/static-assets/pdf/blog/stripe-net/stripe-net-1.webp)

2.这时会出现一个新窗口。选择 "控制台应用程序",然后点击 "下一步"。

![Stripe .NET(如何为开发人员工作):图 2 - 选择 C# 控制台应用程序作为项目类型。单击 "下一步"。](/static-assets/pdf/blog/stripe-net/stripe-net-2.webp)

3.在下一个窗口中,输入项目名称并选择位置,然后点击下一步。

![Stripe .NET(如何为开发人员工作):图 3 - 通过指定项目名称、位置和解决方案名称来配置项目。然后点击下一步。](/static-assets/pdf/blog/stripe-net/stripe-net-3.webp)

4.在下一个窗口中,选择框架并点击创建。

![Stripe .NET(如何为开发人员工作):图 4 - 为您的项目选择合适的 .NET Framework,然后单击 "创建"。](/static-assets/pdf/blog/stripe-net/stripe-net-4.webp)

就这样,新的 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"
VB   C#

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)
VB   C#

输出条纹仪表板

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)
VB   C#

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)
VB   C#

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")
VB   C#

最佳做法

  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 的理想工具,无论是用于报告、发票还是其他文档需求。

主要功能

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 .NET 库

使用 NuGet 包管理器安装 IronPDF 的步骤:

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

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

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

![Stripe .NET(如何为开发人员工作):图 9 - 在 NuGet Package Manager 的搜索栏中搜索 "IronPDF",使用 Manage NuGet Package for Solution 安装 IronPDF,然后选择项目并点击安装按钮。](/static-assets/pdf/blog/stripe-net/stripe-net-9.webp)

处理付款并生成发票

下面是一个完整的示例,演示了使用 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
VB   C#

输出

Stripe .NET(如何为开发人员工作):图 10 - 通过 Stripe 服务使用 IronPDF 生成 PDF 发票

结论

Stripe.Net "是一个全面而强大的库,可简化 Stripe 支付处理与 .NET 应用程序的集成。它具有从基本交易处理到管理订阅和争议的各种功能,可满足与支付相关的各种需求。

IronPDF "是对 "Stripe.Net "的补充,使开发人员能够生成、编辑和管理PDF文档。这些库共同为在.NET应用程序中处理付款和生成相应文档提供了强大的解决方案。

通过利用 Stripe.NetIronPDF 的功能,开发人员可以创建无缝、高效的工作流,处理从支付处理到文档生成的所有事务,从而增强应用程序的整体功能和用户体验。

IronPDF 通过提供免费试用页面,让开发人员有机会测试其广泛的功能。

IronPDF 还提供客户支持和更新,以及代码示例和详尽的文档,帮助用户充分利用它。如需进一步了解该主题,请参阅我们有关 HTML 到 PDF 转换的详尽教程。

< 前一页
Papercut SMTP C# (开发者如何使用)
下一步 >
TensorFlow .NET(它是如何为开发人员工作的)

准备开始了吗? 版本: 2024.9 刚刚发布

免费NuGet下载 总下载量: 10,731,156 查看许可证 >