メインコンテンツまでスキップ

サブスクリプション決済

概要

Jammのサブスクリプション決済機能を使用することで、以下のような定期的な決済を実装できます:

  • デジタルコンテンツのサブスクリプション
  • 定期配送サービス
  • 定額制サービス
  • その他定期的な決済が必要なサービス

対象となるサービス

  • デジタルコンテンツのサブスクリプション
  • 定期配送サービス
  • 定額制サービス
  • その他定期的な決済が必要なサービス

本機能によってできること

  • ユーザーが Jamm で決済することを選択できる
  • 選択したあとにユーザーが登録するサービスとの連携を承認できる
  • 加盟店は今後の引き落としに利用するcustomer idを受け取れる
  • customer idを利用して後日引き落としを行える

実装に必要な手順

  • サービス内に Jamm 決済オプションボタンを実装
  • ユーザーを Jamm の指定するリンクに遷移させる
  • ユーザー決済登録完了の webhook を受け取る
  • 後日対象ユーザーから引き落としを行える

サブスクリプション決済の実装手順

Step 1 of 4
1

バックエンドにJamm決済機能を実装

Jammの初期設定

subscription_creation.rb
1# Ruby SDKを使用した実装例
2require 'jamm'
3
4# Jammの初期設定
5Jamm.client_id = '<your client id>'
6Jamm.client_secret = '<your client secret>'
7
8# 初回登録時のサブスクリプション決済作成 (初月支払いなし)
9payment_response = Jamm::Payments.create(
10redirect: {
11 success_url: 'http://www.example.com/success',
12 failure_url: 'http://www.example.com/fail',
13 info_url: 'http://www.example.com/customer_service'
14},
15metadata: {
16 subscription_id: 'sub_123',
17 customer_id: 'cust_456'
18}
19)
20
21# 初回登録時のサブスクリプション決済作成 (初月支払いあり)
22payment_response = Jamm::Payments.create(
23charges: {
24 description: 'Monthly Subscription',
25 amount: 1000,
26 currency: 'jpy'
27},
28redirect: {
29 success_url: 'http://www.example.com/success',
30 failure_url: 'http://www.example.com/fail',
31 info_url: 'http://www.example.com/customer_service'
32},
33metadata: {
34 subscription_id: 'sub_123',
35 customer_id: 'cust_456'
36}
37)
2

Jamm決済用のボタンを実装

ユーザーがJammで決済することを選択できるようにします

JammPaymentButton.jsx
1// Reactコンポーネントの例
2const JammPaymentButton = () => {
3const handlePayment = async () => {
4 try {
5 const response = await fetch('/api/create-subscription', {
6 method: 'POST',
7 headers: { 'Content-Type': 'application/json' },
8 body: JSON.stringify({
9 plan: 'premium',
10 amount: 1000,
11 currency: 'jpy',
12 description: 'Monthly Subscription'
13 })
14 });
15 const data = await response.json();
16
17 // payment_urlが返された場合、ユーザーをJammの決済ページにリダイレクト
18 if (data.payment_url) {
19 window.location.href = data.payment_url;
20 }
21 } catch (error) {
22 console.error('Payment error:', error);
23 }
24};
25
26return (
27 <button onClick={handlePayment}>
28 Jammで決済する
29 </button>
30);
31};
3

登録完了のwebhookを受け取る

ユーザー決済完了のwebhookを受け取り、必要な処理を実行します

webhook_handler.rb
1# Webhookハンドラーの例
2post '/webhooks/jamm' do
3payload = JSON.parse(request.body.read)
4
5case payload['event_type']
6when 'EVENT_TYPE_CONTRACT_ACTIVATED'
7 # トークン有効化時の処理
8 customer_id = payload['content']['customer']
9 activated_at = payload['content']['activated_at']
10 handle_contract_activation(customer_id, activated_at)
11
12when 'EVENT_TYPE_CHARGE_SUCCESS'
13 # 決済成功時の処理
14 customer_id = payload['content']['customer']
15 charge_id = payload['content']['id']
16 amount = payload['content']['final_amount']
17 handle_charge_success(customer_id, charge_id, amount)
18end
19
20status 200
21end
4

定期的な引き落としを実行

設定されたスケジュールに従って、ユーザー口座から代金を引き落とします

subscription_charge.rb
1# 定期的な引き落としの実装例
2def process_subscription_charge(subscription)
3payment_response = Jamm::Payments.create(
4 payment_method: {
5 token_id: subscription.jamm_token_id
6 },
7 charges: {
8 description: "Monthly Subscription - #{subscription.plan_name}",
9 amount: subscription.amount,
10 currency: 'jpy'
11 },
12 metadata: {
13 subscription_id: subscription.id,
14 customer_id: subscription.customer_id
15 }
16)
17
18if payment_response.charge_id
19 # 即時で決済結果を受け取れます
20 handle_charge_success(payment_response.charge_id)
21else
22 # エラーハンドリング
23 handle_charge_error(payment_response.error)
24end
25end