.NET ヘルプ

Stripe .NET(開発者向けの仕組み)

更新済み 7月 1, 2024
共有:

IronPDFとのStripe.Net統合

Stripe.Netは、開発者がStripeの決済処理機能を.NETアプリケーションに統合できる強力な.NETライブラリです。 Stripeは、オンラインでの支払いを受け付けることを可能にする人気のある決済ゲートウェイです。 Stripe.Netを使用することで、開発者はStripe APIが提供する強力な機能を利用して、取引、顧客、サブスクリプションなどを管理することができます。 この記事では、StripeをIronPDFと一緒に使用してPDFを作成する方法について説明します。

Stripe.Netの使い方を始める

新しいVisual Studioプロジェクトを作成する

Stripe.Netを使い始めるには、新しいVisual Studioプロジェクトを作成するか、既存のプロジェクトを開く必要があります。 このチュートリアルでは、コンソールアプリケーションプロジェクトを使用します。

  1. Visual Studio を開き、「Create a New Project」をクリックしてください。

    Stripe .NET(開発者向け動作方法):図1 - Visual Studio を開き、「新しいプロジェクトを作成」オプションをクリックします。

  2. 新しいウィンドウが表示されます。 「コンソールアプリケーション」を選択し、「次へ」をクリックしてください。

    Stripe .NET (ストライプ .NET) (開発者向けの仕組み)図2 - プロジェクトの種類としてC#コンソールアプリを選択する。 [次へ]をクリック。

  3. 次のウィンドウでは、プロジェクト名を入力し、場所を選択してから「次へ」をクリックしてください。

    Stripe .NET (ストライプ .NET) (開発者向けの仕組み)図3 - プロジェクト名、場所、ソリューション名を指定してプロジェクトを構成します。 次へをクリックしてください。

  4. 次のウィンドウで、フレームワークを選択し、「作成」をクリックしてください。

    Stripe .NET (ストライプ .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アカウントにあるStripe APIキーを設定する必要があります。 このキーは、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 ダッシュボードの出力

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 (ストライプ .NET) (開発者向けの仕組み): 図6 - Stripe Payment Intent

高度な機能

サブスクリプション

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 ライブラリドキュメントを参照してください。

C# のための IronPDF の紹介

Stripe .NET (ストライプ .NET) (開発者向けの動作方法): 図8 - IronPDF for .NET: The C# PDF Library

IronPDFは、開発者がPDFドキュメントを作成、編集、および内容を抽出することを可能にするC#ライブラリです。 これは、.NETアプリケーションでPDFを生成するための理想的なツールであり、レポート、請求書、その他のドキュメントニーズに適しています。

主な機能

HTML から PDF へ

IronPDFを使用すると、開発者はHTML文字列、URL、およびHTMLファイルをPDFに変換して簡単にPDF文書を作成できます。

PDF編集

既存のPDFドキュメントを簡単に編集できます。 IronPDFは、ユーザーが特定のインデックスにページを追加したり、ページをコピーまたは削除したり、PDFを分割したり、ページを抽出して新しいPDFを作成したりすることを可能にします。

3. PDF結合

IronPDFのマージ機能により、開発者は2つ以上のPDFドキュメントを1つに結合することができます。

4. PDFセキュリティ

IronPDFは、ユーザーがPDFセキュリティを強化するために、PDFにパスワードと権限を追加することを可能にします。

5. PDFの暗号化と復号化

IronPDFは、PDFドキュメントの128ビット暗号化、復号化、およびパスワード保護をサポートしています。

PDFドキュメントに電子署名する

開発者は、IronPDFを使用してプログラムでPDFにデジタル署名を追加できます。 複数の方法でPDFにデジタル署名証明書(.pfxおよび.p12形式)を使用して署名することをサポートしています。

例: 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 パッケージ マネージャーの検索バーに「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
VB   C#

出力

Stripe .NET(開発者向けの動作方法):図10 - Stripeサービスを介してIronPDFで生成されたPDF請求書

結論

「Stripe.Net」は、Stripeの決済処理を.NETアプリケーションに統合するのを簡素化する包括的かつ強力なライブラリです。 基本的なトランザクション処理から、サブスクリプションや紛争の管理に至るまでの機能を備えており、幅広い支払い関連のニーズに対応します。

IronPDF は、開発者がPDF文書の生成、編集、および管理を可能にすることで Stripe.Net を補完します。 これらのライブラリを組み合わせることで、.NETアプリケーションにおける支払いの処理およびそれに対応するドキュメントの生成に対する強力なソリューションが提供されます。

Stripe.NetIronPDFの両方の機能を活用することで、開発者は、支払い処理からドキュメント生成までを扱うシームレスで効率的なワークフローを作成でき、アプリケーションの全体的な機能とユーザー体験を向上させることができます。

IronPDFは、開発者がその幅広い機能を試す機会を提供するために、無料試用ページを提供しています。

IronPDFは、カスタマーサポートやアップデートとともに、コード例や詳細なドキュメントを提供し、ユーザーが最大限に活用できるよう支援します。 詳しくは、HTMLからPDFへの変換に関する包括的なチュートリアルをご覧下さい。

< 以前
Papercut SMTP C#(開発者向けの動作方法)
次へ >
TensorFlow .NET(開発者向けの動作方法)

準備はできましたか? バージョン: 2024.9 新発売

無料のNuGetダウンロード 総ダウンロード数: 10,659,073 View Licenses >