Ruby SDKは、Dodo PaymentsをRubyアプリケーションに統合するためのシンプルで直感的な方法を提供します。Rubyの慣習とベストプラクティスに従い、包括的なエラーハンドリング、ページネーション、およびミドルウェアサポートを提供します。
インストール
Gemfileにgemを追加します:
gem "dodopayments" , "~> 2.0.0"
常に最新のSDKバージョンを使用して、最新のDodo Payments機能にアクセスしてください。最新情報を維持するためにbundle update dodopaymentsを定期的に実行してください。
次に、実行します:
SDK は Ruby 3.2.0 以降をサポートしており、包括的な型、エラーハンドリング、およびリトライ機構を備えています。
クイックスタート
クライアントを初期化し、チェックアウトセッションを作成します:
require "bundler/setup"
require "dodopayments"
dodo_payments = Dodopayments :: Client . new (
bearer_token: ENV [ "DODO_PAYMENTS_API_KEY" ], # This is the default and can be omitted
environment: "test_mode" # defaults to "live_mode"
)
checkout_session_response = dodo_payments. checkout_sessions . create (
product_cart: [{ product_id: "product_id" , quantity: 1 }]
)
puts (checkout_session_response. session_id )
APIキーは環境変数を使用して安全に保管してください。バージョン管理にコミットしたりコード内で公開したりしないでください。
コア機能
Ruby Conventions Rubyの命名規則や慣用的なパターンに従います
Elegant Syntax Ruby開発者にとって自然に感じられる、クリーンで読みやすいAPI
Auto-Pagination リスト応答のための組み込みのオートページングイテレータ
Type Safety 型安全性を強化する任意のSorbet型
タイムアウト設定
リクエストのタイムアウトを設定します:
# Configure default for all requests (default is 60 seconds)
dodo_payments = Dodopayments :: Client . new (
timeout: nil # disable timeout
)
# Or, configure per-request
dodo_payments. checkout_sessions . create (
product_cart: [{ product_id: "product_id" , quantity: 1 }],
request_options: { timeout: 5 }
)
リトライ設定
自動リトライの動作を設定します:
# Configure default for all requests (default is 2)
dodo_payments = Dodopayments :: Client . new (
max_retries: 0 # disable retries
)
# Or, configure per-request
dodo_payments. checkout_sessions . create (
product_cart: [{ product_id: "product_id" , quantity: 1 }],
request_options: { max_retries: 5 }
)
一般的な操作
チェックアウトセッションの作成
チェックアウトセッションを生成します:
session = dodo_payments. checkout_sessions . create (
product_cart: [
{
product_id: "prod_123" ,
quantity: 1
}
],
return_url: "https://yourdomain.com/return"
)
# Redirect to checkout
redirect_to session. url
顧客の管理
顧客情報を作成および取得します:
# Create a customer
customer = dodo_payments. customers . create (
email: "customer@example.com" ,
name: "John Doe" ,
metadata: {
user_id: "12345"
}
)
# Retrieve customer
customer = dodo_payments. customers . retrieve ( "cus_123" )
puts "Customer: #{ customer. name } ( #{ customer. email } )"
サブスクリプションの管理
定期的なサブスクリプションを作成および管理します:
# Create a subscription
subscription = dodo_payments. subscriptions . create (
customer_id: "cus_123" ,
product_id: "prod_456" ,
price_id: "price_789"
)
# Update subscription
updated = dodo_payments. subscriptions . update (
subscription. id ,
metadata: { plan_type: "premium" }
)
# Cancel subscription
dodo_payments. subscriptions . cancel (subscription. id )
ページネーション
自動ページネーション
すべてのページを自動的にイテレートします:
page = dodo_payments. payments . list
# Fetch single item from page
payment = page. items [ 0 ]
puts (payment. brand_id )
# Automatically fetches more pages as needed
page. auto_paging_each do | payment |
puts (payment. brand_id )
end
手動ページネーション
ページネーションをより制御するために:
page = dodo_payments. payments . list
if page. next_page?
new_page = page. next_page
puts (new_page. items [ 0 ]. brand_id )
end
エラーハンドリング
さまざまなDodo Payments APIエラーを処理します:
begin
checkout_session = dodo_payments. checkout_sessions . create (
product_cart: [{ product_id: "product_id" , quantity: 1 }]
)
rescue Dodopayments :: Errors :: APIConnectionError => e
puts ( "The server could not be reached" )
puts (e. cause ) # an underlying Exception, likely raised within `net/http`
rescue Dodopayments :: Errors :: RateLimitError => e
puts ( "A 429 status code was received; we should back off a bit." )
rescue Dodopayments :: Errors :: APIStatusError => e
puts ( "Another non-200-range status code was received" )
puts (e. status )
end
レート制限エラーに対して指数バックオフを用いたリトライロジックを実装し、高負荷時にもアプリケーションが優雅に動作するようにしましょう。
Sorbetによる型安全性
型安全なリクエストパラメータにSorbetを使用します:
# Type-safe using Sorbet RBI definitions
dodo_payments. checkout_sessions . create (
product_cart: [
Dodopayments :: ProductItemReq . new (
product_id: "product_id" ,
quantity: 1
)
]
)
# Hashes work, but are not typesafe
dodo_payments. checkout_sessions . create (
product_cart: [{ product_id: "product_id" , quantity: 1 }]
)
# You can also splat a full Params class
params = Dodopayments :: CheckoutSessionCreateParams . new (
product_cart: [
Dodopayments :: ProductItemReq . new (
product_id: "product_id" ,
quantity: 1
)
]
)
dodo_payments. checkout_sessions . create ( ** params)
高度な使用法
ドキュメント未記載のエンドポイント
ドキュメント未記載のエンドポイントにリクエストを送信します:
response = dodo_payments. request (
method: :post ,
path: '/undocumented/endpoint' ,
query: { "dog" : "woof" },
headers: { "useful-header" : "interesting-value" },
body: { "hello" : "world" }
)
ドキュメント未記載のパラメータ
ドキュメント未記載のパラメータを送信します:
checkout_session_response = dodo_payments. checkout_sessions . create (
product_cart: [{ product_id: "product_id" , quantity: 1 }],
request_options: {
extra_query: { my_query_parameter: value},
extra_body: { my_body_parameter: value},
extra_headers: { "my-header" : value}
}
)
# Access undocumented response properties
puts (checkout_session_response[ :my_undocumented_property ])
Rails統合
イニシャライザの作成
config/initializers/dodo_payments.rb を作成してください:
require "dodopayments"
DODO_CLIENT = Dodopayments :: Client . new (
bearer_token: Rails . application . credentials . dodo_api_key ,
environment: Rails . env . production? ? "live_mode" : "test_mode"
)
サービスオブジェクトパターン
支払いサービスを作成します:
# app/services/payment_service.rb
class PaymentService
def initialize
@client = DODO_CLIENT
end
def create_checkout ( items )
@client . checkout_sessions . create (
product_cart: items,
return_url: Rails . application . routes . url_helpers . checkout_return_url
)
end
def process_payment ( amount: , currency: , customer_id: )
@client . payments . create (
amount: amount,
currency: currency,
customer_id: customer_id
)
end
end
コントローラー統合
Railsコントローラーで使用します:
# app/controllers/checkouts_controller.rb
class CheckoutsController < ApplicationController
def create
service = PaymentService . new
session = service. create_checkout (checkout_params[ :items ])
redirect_to session. url , allow_other_host: true
rescue Dodopayments :: Errors :: APIError => e
flash[ :error ] = "Payment error: #{ e. message } "
redirect_to cart_path
end
private
def checkout_params
params. require ( :checkout ). permit ( items: [ :product_id , :quantity ])
end
end
Sinatra統合
Sinatraアプリケーションで使用します:
require "sinatra"
require "dodopayments"
configure do
set :dodo_client , Dodopayments :: Client . new (
bearer_token: ENV [ "DODO_API_KEY" ]
)
end
post "/create-checkout" do
content_type :json
begin
session = settings. dodo_client . checkout_sessions . create (
product_cart: JSON . parse (request. body . read )[ "items" ],
return_url: " #{ request. base_url } /return"
)
{ checkout_url: session. url }. to_json
rescue Dodopayments :: Errors :: APIError => e
status 400
{ error: e. message }. to_json
end
end
リソース
GitHub Repository ソースコードを確認し、貢献する
API Reference 完全なAPIドキュメント
Discord Community ヘルプを得て開発者とつながる
Report Issues バグを報告するか機能をリクエストする
サポート
Ruby SDKに関して助けが必要ですか?
貢献を歓迎します!始めるための貢献ガイドライン を確認してください。