Integra i pagamenti Dodo nelle tue applicazioni Ruby con un elegante SDK nativo Ruby
L’SDK Ruby fornisce un modo semplice e intuitivo per integrare i pagamenti Dodo nelle tue applicazioni Ruby. Segue le convenzioni e le migliori pratiche di Ruby, offrendo una gestione degli errori completa, paginazione e supporto per middleware.
Usa sempre la versione più recente dell’SDK per accedere alle funzionalità più nuove di Dodo Payments. Esegui bundle update dodopayments regolarmente per restare aggiornato.
Poi esegui:
Copia
bundle install
L’SDK supporta Ruby 3.2.0 e versioni successive, con tipi completi, gestione degli errori e meccanismi di retry.
Inizializza il client e crea una sessione di checkout:
Copia
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)
Conserva le tue chiavi API in modo sicuro usando variabili d’ambiente. Non inserirle mai nel controllo versione né esporle nel codice.
page = dodo_payments.payments.list# Fetch single item from pagepayment = page.items[0]puts(payment.brand_id)# Automatically fetches more pages as neededpage.auto_paging_each do |payment| puts(payment.brand_id)end
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
Implementa una logica di retry con backoff esponenziale per gli errori di rate limit, in modo da assicurare che la tua applicazione gestisca con grazia scenari ad alto volume.
Usa Sorbet per parametri di richiesta sicuri per i tipi:
Copia
# Type-safe using Sorbet RBI definitionsdodo_payments.checkout_sessions.create( product_cart: [ Dodopayments::ProductItemReq.new( product_id: "product_id", quantity: 1 ) ])# Hashes work, but are not typesafedodo_payments.checkout_sessions.create( product_cart: [{product_id: "product_id", quantity: 1}])# You can also splat a full Params classparams = Dodopayments::CheckoutSessionCreateParams.new( product_cart: [ Dodopayments::ProductItemReq.new( product_id: "product_id", quantity: 1 ) ])dodo_payments.checkout_sessions.create(**params)