6
2

More than 1 year has passed since last update.

[Stripe Updates] NoCodeでオンライン決済を受け付ける”Payment Links(支払いリンク)"に、管理用APIが登場しました!

Last updated at Posted at 2022-01-25

「コードを一切書かずに、クレジットカードをはじめとするオンライン決済を処理するリンクを作成する仕組み」として、Payment Links(支払いリンク)は2021年に登場しました。

1度商品・料金を登録し[支払いリンク]を作成すれば、後はそのリンクを顧客に踏んでもらうだけで、商品の販売や定期会員の決済を行うことができます。

これまでの[支払いリンク]でできなかったこと

これまでは、このリンクを作成するためにDashboardにアクセスする必要がありました。
そのため、以下のようなユースケースに対応することができませんでした。

  • WordPressやHubSpotといった外部サービス側で、リンク作成・埋め込み・表示を一元化したい
  • アドホックに料金・商品を生成し、ユーザーに応じた支払いリンクを発行したい
  • バッチ処理を利用した、「一定期間または特定時間のみ有効なリンク」を作成したい
  • [Connect] 出店者が、顧客向けに支払いリンクを作成できるようにしたい

Stripe Payment Links APIをローンチ!

こうした要望にお答えするため、[支払いリンク]を作成・更新・管理するためのAPIをリリースしました。

以下のようにAPIを実行することで、支払いリンクを新しく作成することができます。

curl https://api.stripe.com/v1/payment_links \
  -u sk_test_xxxxx: \
  -d "line_items[0][price]"="price_XXXXX" \
  -d "line_items[0][quantity]"=1

APIレスポンス

{
  "id": "plink_xxxxx",
  "object": "payment_link",
  "active": true,
  "after_completion": {
    "hosted_confirmation": {
      "custom_message": null
    },
    "type": "hosted_confirmation"
  },
  "allow_promotion_codes": false,
  "application_fee_amount": null,
  "application_fee_percent": null,
  "automatic_tax": {
    "enabled": false
  },
  "billing_address_collection": "auto",
  "livemode": false,
  "metadata": {
  },
  "on_behalf_of": null,
  "payment_method_types": null,
  "shipping_address_collection": null,
  "subscription_data": null,
  "transfer_data": null,
  "url": "https://buy.stripe.com/test_xxxxxx"
}

組み込みサンプル: ユーザーに応じた料金の支払いリンクを作成する

Node.jsでの実装サンプルとして、「ユーザーごとに異なる料金を提供するサービス」の実装例を紹介します。

このケースでは、1つの商品に対して複数の料金を登録します。しかしその料金はユーザーによって異なるため、前もって料金と支払いリンクを作成することができません。

そこでPayment Links APIを利用し、以下のような関数を組み込むことで、動的な料金・リンク生成が可能になります。

export async function createPaymentLinks( unit_amount, name = 'お客', quantity = 1 ) {
  const price = await stripe.prices.create({
    product: 'prod_xxxxx',
    nickname: `${name}様専用プラン`,
    recurring:  {
      interval: 'month'
    },
    unit_amount,
    currency: 'jpy'
  })
  const link = await stripe.paymentLinks.create({
    line_items: [{
      price: price.id,
      quantity
    }]
  })
  return {
    url: link.url
  }
}

その他のAPI

今回のリリースでは、以下のAPIが追加されました。

  • 支払いリンクの作成
  • 作成済み支払いリンクの取得
  • 作成済み支払いリンクの一覧取得
  • 支払いリンクの設定更新
  • 支払いリンクに含まれる料金データの取得

Node.jsでのコードサンプルをここでは紹介します。
他の言語についても、ほぼ同じ引数・メソッド名で実行できますので、お使いの言語に適宜読み替えていただければと思います。
※ブログ記事にもしていただけると大変助かりますm(_ _)m

支払いリンクの作成

支払いリンクを作成します。料金のIDが必須ですので、事前に取得・作成を行いましょう。

  const link = await stripe.paymentLinks.create({
    line_items: [{
      price: price.id,
      quantity: 3,
      /**
       * 注文数の制限
       */
      adjustable_quantity: {
        enabled: true,
        maximum: 10,
        minimum: 2,
      },
    }],
    /**
     * Taxを利用した付加価値税徴収
     */
    automatic_tax: {
      enabled: true,
    },
    /**
     * 定期課金の追加設定
     */
    subscription_data: {
      trial_period_days: 3,
    }
  })

作成済み支払いリンクの取得

作成したリンクのID(plink_xxxx)を使用して、作成済みのリンクデータを取得できます。

const link = await stripe.paymentLinks.retrieve("plink_xxxx")

支払いリンクの設定更新

リンクの設定内容を更新したい場合のAPIはこちらです。
リンクを無効化したい場合や、設定する商品や個数を変更したい場合に実行します。

支払いリンクの無効化操作

const link = await stripe.paymentLinks.update("plink_xxxx", { active: false })

支払いリンクの有効化操作

const link = await stripe.paymentLinks.update("plink_xxxx", { active: true })

商品や設定を変更する操作

基本的には、支払リンク作成時とほぼ同じパラメータが利用できます。

const link = await stripe.paymentLinks.update("plink_xxxx", {
    line_items: [{
      price: 'price_1KLMg3KlXtpaTqYxaZiBUihW',
      quantity: 3,
    }],
    /**
     * お届け先住所
     */
    billing_address_collection: 'required',
    /**
     * クーポンコード
     */
    allow_promotion_codes: true,
    /**
     * 注文完了後のメッセージ
     */
    after_completion: {
      type: 'hosted_confirmation',
      hosted_confirmation: {
        custom_message: "ご注文ありがとうございました!"
      }
    }
  })

支払いリンクに含まれる料金データの取得

作成した支払いリンクで決済する中身だけを取得することもできます。

const link = await stripe.paymentLinks.listLineItems('plink_xxx', { limit: 3 })

レスポンス

{
  "object": "list",
  "data": [
    {
      "id": "li_xxx",
      "object": "item",
      "amount_subtotal": 1000,
      "amount_total": 1000,
      "currency": "jpy",
      "description": "動的な料金サンプル",
      "price": {
        "id": "price_xxxxx",
        "object": "price",
        "active": true,
        "billing_scheme": "per_unit",
        "created": 1643008163,
        "currency": "jpy",
        "livemode": false,
        "lookup_key": null,
        "metadata": {},
        "nickname": "専用プラン",
        "product": "prod_xxxx",
        "recurring": {
          "aggregate_usage": null,
          "interval": "month",
          "interval_count": 1,
          "trial_period_days": null,
          "usage_type": "licensed"
        },
        "tax_behavior": "unspecified",
        "tiers_mode": null,
        "transform_quantity": null,
        "type": "recurring",
        "unit_amount": 1000,
        "unit_amount_decimal": "1000"
      },
      "quantity": 1
    }
  ],
  "has_more": false,
  "url": "/v1/payment_links/plink_xxxxx/line_items"
}

作成済み支払いリンクの一覧取得

作成した支払いリンクを取得できます。
現時点では、無効化中のリンクを除外するパラメータがありません。
エンドユーザー向けの表示に利用される場合は、ご自身でフィルター処理を追加する必要があることにご注意ください。

const link = await stripe.paymentLinks.list({ limit: 3 })

レスポンス

{
  "object": "list",
  "data": [
    {
      "id": "plink_xxxxx",
      "object": "payment_link",
      "active": true,
      "after_completion": {
        "hosted_confirmation": {
          "custom_message": "ご注文ありがとうございました!"
        },
        "type": "hosted_confirmation"
      },
      "allow_promotion_codes": true,
      "application_fee_amount": null,
      "application_fee_percent": null,
      "automatic_tax": {
        "enabled": false
      },
      "billing_address_collection": "required",
      "livemode": false,
      "metadata": {},
      "on_behalf_of": null,
      "payment_method_types": null,
      "shipping_address_collection": null,
      "subscription_data": {
        "trial_period_days": null
      },
      "transfer_data": null,
      "url": "https://buy.stripe.com/test_xxxx"
    },
  ],
  "has_more": true,
  "url": "/v1/payment_links"
}

おわりに

[支払いリンク]のAPIが登場したことで、運営されているサービス・プラットフォーム・業務フローへの組み込みがより容易になります。

また、ZapierやIFTTT、AWS Step Functions・Bubbleなど、ノーコード・ローコード系のシステムとの相性も良いかと思いますので、ぜひみなさんお試しください。

関連ドキュメント

YouTubeで紹介・デモ動画も公開中

英語ですが、デモやユースケースなどの紹介をしています。
こちらも合わせてどうぞ。

[PR] Stripe開発者向け情報をQiitaにて配信中!

2021年12月よりQiitaにて、Stripe開発者のためのブログ記事更新を開始しました。

  • [Stripe Updates]:開発者向けStripeアップデート紹介・解説
  • ユースケース別のStripe製品や実装サンプルの紹介
  • Stripeと外部サービス・OSSとの連携方法やTipsの紹介
  • 初心者向けのチュートリアル(予定)

など、Stripeを利用してオンラインビジネスを始める方法について随時更新してまいります。

-> Stripe Organizationsをフォローして最新情報をQiitaで受け取る

6
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
2