跳過到頁腳內容
.NET幫助

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

與IronPDF的Stripe.Net整合

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 (How It Works For Developers): Figure 1 - Open Visual Studio and click on Create a new project option.

  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

或者

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

最佳實踐

  1. 安全性:始終保護您的API密鑰,不要將其硬編碼在源檔中。
  2. 錯誤處理:實施強大的錯誤處理解決方案來管理例外情況和失敗的API呼叫。
  3. 測試:使用Stripe的測試模式並提供測試卡號碼來全面測試您的整合。
  4. 文件:參考官方的Stripe API文件和Stripe.Net程式庫文件,獲取最新的資訊和範例。

Introducing 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");
    }
}
$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支持128位加密、解密和PDF文件的密碼保護。

6. 數位簽署PDF文件

開發者可以使用IronPDF程式化地將數位簽章添加到PDF中。 它支持使用.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 (How It Works For Developers): Figure 9 - Install IronPDF using the Manage NuGet Package for Solution by searching IronPDF in the search bar of NuGet Package Manager, then select the project and click on the Install button.

處理支付並生成發票

這是一個完整的示例,演示如何使用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();
    }
}
$vbLabelText   $csharpLabel

輸出

Stripe .NET(開發者如何運作):圖10 - 使用IronPDF通過Stripe服務生成的PDF發票

結論

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

Stripe.Net,讓開發者能夠生成、編輯和管理PDF文件。 這些程式庫共同提供了一個強大的解決方案,用於處理支付和生成.NET應用程式中的相應文件。

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

IronPDF提供開發者一個<團支>免費試用IronPDF的機會,以測試其廣泛的功能。

IronPDF提供客戶支援和更新,並配有代碼示例和詳細的文件幫助用戶充分利用它。 要進一步探索該主題,請參考我們關於使用IronPDF進行HTML到PDF轉換的廣泛教程

常見問題解答

我如何將支付處理集成到.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。

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

Jacob Mellor是Iron Software的首席技術官,也是開創C# PDF技術的前瞻性工程師。作為Iron Software核心代碼庫的原始開發者,他自公司成立以來就塑造了公司的產品架構,並與CEO Cameron Rimington將公司轉型為服務NASA、Tesla以及全球政府機構的50多人公司。

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

他的旗艦作品IronPDF和Iron Suite .NET程式庫全球已獲得超過3000萬次NuGet安裝,他的基礎代碼不斷在全球各地驅動開發者工具。擁有25年以上的商業經驗和41年的編碼專業知識,Jacob仍然專注於推動企業級C#、Java和Python PDF技術的創新,同時指導下一代技術領導者。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我