SlideShare a Scribd company logo
1 of 14
Download to read offline
RailsでPayPal

active_paypal_adaptive_payment
自己紹介
• 水谷 裕生(みずたに ひろたか)
• Railsエンジニア
• Twitter: @icb54615
システム概要
• いわゆるマーケットプレイス
• サービスを提供する人と受ける人を結びつけ
  るプラットフォーム
• サービスを予約した際にPayPalで決済
• プラットフォーム事業者に金額の何割かの手
  数料を支払い
• 残りはサービス提供する人に支払い
• 予約期限までは予約のキャンセルが可能で、
  支払らった金額を返金
¥サービス料
                                   ¥手数料を引いた料金

            ¥返金
サービスを受ける人           プラットフォーム事業者            サービスを提供する人
  Sender             Primary Receiver       Secondary Receiver
仕様
• Adaptive Payments APIを使用
• active_paypal_adaptive_payment gem
  を使用
• Pay APIでDelayed Chained Payments
  による支払い
• Refund APIで支払いの返金
• 予約期限が過ぎたらExecutePayment
  APIでサービスを提供する人に支払い
Adaptive Payments API
• 支払いを他の口座に入金が可能
• 自分の口座から他の口座への送金が可能
• 一回の支払いで複数の口座への支払いが可能(Parallel
  Payments)
• 複数の口座への支払いを代理窓口として処理する事が可能
  (Chainned Payments)

注意点:
• ビジネスアカウントが必要
• 本番環境で運用するには、App IDをX.comから取得が必要
• Chainned Payments のような“advanced”な処理をする場合
  はPayPalの事前審査が必要
  ⇒審査に10-15日程度かかる場合がある
active_paypal_adaptive_payment

• Gemfileにgemを追加してbundle install
• 設定ファイルを追加
• PayPalが開発用に用意しているSandbox
  のアカウントを登録して、その値を設定
  ファイルに記述
Gemfile:
gem 'active_paypal_adaptive_payment'



config/initializer/paypal.rb:
unless Rails.env.production?
  ActiveMerchant::Billing::Base.mode = :test
end
PaypalGateway = ActiveMerchant::Billing::PaypalAdaptivePayment.new(
  :login     => "hirotaka_1338865312_biz_api1.example.com",
  :password => "1338863333",
  :signature => "GKo3jGgDxJIjdH5V9tmlOoZlBDOzAMqkRgLDpvlxuu….",
  :appid     => "APP-80W284485P519543T"
)
Pay APIでDelayed Chained
  Paymentsによる支払い
def pay
  primary_receiver   = {
                           :primary => true,
                           :email   => "primary_receiver@example.com",
                           :amount => 5000
                      }
 secondary_receiver = {
                           :primary => false,
                           :email   => "secondary_receiver@example.com",
                           :amount => 4000
                       }
 r = PaypalGateway.setup_purchase(
                                   :action_type => "PAY_PRIMARY",
                                   :receiver_list => [primary_receiver, secondary_receiver],
                                   :currency_code => "JPY",
                                   :error_language => "ja_JP",
                                   :return_url => url_for(:action=> :complete_payment),
                                   :cancel_url => url_for(:action=> :cancel_paypal_payment),
                                 )
 if !r.success? || !r["pay_key"]
   message = "ご迷惑おかけしております。システムエラーが発生しました。"
    redirect_to url_for :action => error, :notice => message and return
  end
  payment = Payment.new
  payment.pay_key = r["pay_key"]
  payment.status = r["payment_exec_status"]
  redirect_to PaypalGateway.redirect_url_for(r["pay_key"])
end
Refund APIで支払いの返金
 • Rufund APIをpayKeyで呼び出す
def cancel
  payment = Payment.find(params[:id])
  r = PaypalGateway.refund(
                              :pay_key => payment.pay_key,
                              :currency_code => "JPY"
                          )
  unless r.success?
    redirect_to :action => :error
  end
end
予約期限が過ぎたらExecutePayment
APIでサービスを提供する人に支払い
• statusが”INCOMPLETE”を対象
• ExecutePayment APIをpayKeyで呼び出
  し
• runnerで定期的に実行
Payment.where(:status => "INCOMPLETE").each do |payment|
  r = PaypalGateway.execute_payment :pay_key => payment.pay_key
  if r.success? && r["payment_exec_status"]
    payment.status = r["payment_exec_status"]
  else
    raise "PayPal connection error or unexpected response"
  end
  payment.save
end
その他感想など
• いろいろな支払いの仕方ができるので、どのやり方が
  いいのか悩む
• active_paypal_adaptive_paymentのドキュメント
  が全部のAPIをカバーしていない
  – Pay APIに対応するメソッドがわからない。
    ActiveMerchant::Billing::PaypalAdaptivePayment::set
    up_purchaseがPay APIを呼んでるのはソースを見て確認。
  – どんなオプションをわたすのがわからない。
  – あとAPIに渡すキーの名前が、例えばpayKeyがpay_key
    だったり。
• PayPal Adaptive Payments Developer Guideは読み
  込まないといけないけど英語のみ
• App IDの申請も英語のみ

More Related Content

Recently uploaded

研究紹介スライド: オフライン強化学習に基づくロボティックスワームの制御器の設計
研究紹介スライド: オフライン強化学習に基づくロボティックスワームの制御器の設計研究紹介スライド: オフライン強化学習に基づくロボティックスワームの制御器の設計
研究紹介スライド: オフライン強化学習に基づくロボティックスワームの制御器の設計
atsushi061452
 
2024年5月25日Serverless Meetup大阪 アプリケーションをどこで動かすべきなのか.pptx
2024年5月25日Serverless Meetup大阪 アプリケーションをどこで動かすべきなのか.pptx2024年5月25日Serverless Meetup大阪 アプリケーションをどこで動かすべきなのか.pptx
2024年5月25日Serverless Meetup大阪 アプリケーションをどこで動かすべきなのか.pptx
ssuserbefd24
 

Recently uploaded (11)

ロボットマニピュレーションの作業・動作計画 / rosjp_planning_for_robotic_manipulation_20240521
ロボットマニピュレーションの作業・動作計画 / rosjp_planning_for_robotic_manipulation_20240521ロボットマニピュレーションの作業・動作計画 / rosjp_planning_for_robotic_manipulation_20240521
ロボットマニピュレーションの作業・動作計画 / rosjp_planning_for_robotic_manipulation_20240521
 
5/22 第23回 Customer系エンジニア座談会のスライド 公開用 西口瑛一
5/22 第23回 Customer系エンジニア座談会のスライド 公開用 西口瑛一5/22 第23回 Customer系エンジニア座談会のスライド 公開用 西口瑛一
5/22 第23回 Customer系エンジニア座談会のスライド 公開用 西口瑛一
 
論文紹介:Deep Occlusion-Aware Instance Segmentation With Overlapping BiLayers
論文紹介:Deep Occlusion-Aware Instance Segmentation With Overlapping BiLayers論文紹介:Deep Occlusion-Aware Instance Segmentation With Overlapping BiLayers
論文紹介:Deep Occlusion-Aware Instance Segmentation With Overlapping BiLayers
 
クラウド時代におけるSREとUPWARDの取組ーUPWARD株式会社 CTO門畑
クラウド時代におけるSREとUPWARDの取組ーUPWARD株式会社 CTO門畑クラウド時代におけるSREとUPWARDの取組ーUPWARD株式会社 CTO門畑
クラウド時代におけるSREとUPWARDの取組ーUPWARD株式会社 CTO門畑
 
部内勉強会(IT用語ざっくり学習) 実施日:2024年5月17日(金) 対象者:営業部社員
部内勉強会(IT用語ざっくり学習) 実施日:2024年5月17日(金) 対象者:営業部社員部内勉強会(IT用語ざっくり学習) 実施日:2024年5月17日(金) 対象者:営業部社員
部内勉強会(IT用語ざっくり学習) 実施日:2024年5月17日(金) 対象者:営業部社員
 
20240523_IoTLT_vol111_kitazaki_v1___.pdf
20240523_IoTLT_vol111_kitazaki_v1___.pdf20240523_IoTLT_vol111_kitazaki_v1___.pdf
20240523_IoTLT_vol111_kitazaki_v1___.pdf
 
研究紹介スライド: オフライン強化学習に基づくロボティックスワームの制御器の設計
研究紹介スライド: オフライン強化学習に基づくロボティックスワームの制御器の設計研究紹介スライド: オフライン強化学習に基づくロボティックスワームの制御器の設計
研究紹介スライド: オフライン強化学習に基づくロボティックスワームの制御器の設計
 
論文紹介:ViTPose: Simple Vision Transformer Baselines for Human Pose Estimation
論文紹介:ViTPose: Simple Vision Transformer Baselines for Human Pose Estimation論文紹介:ViTPose: Simple Vision Transformer Baselines for Human Pose Estimation
論文紹介:ViTPose: Simple Vision Transformer Baselines for Human Pose Estimation
 
2024年5月25日Serverless Meetup大阪 アプリケーションをどこで動かすべきなのか.pptx
2024年5月25日Serverless Meetup大阪 アプリケーションをどこで動かすべきなのか.pptx2024年5月25日Serverless Meetup大阪 アプリケーションをどこで動かすべきなのか.pptx
2024年5月25日Serverless Meetup大阪 アプリケーションをどこで動かすべきなのか.pptx
 
Intranet Development v1.0 (TSG LIVE! 12 LT )
Intranet Development v1.0 (TSG LIVE! 12 LT )Intranet Development v1.0 (TSG LIVE! 12 LT )
Intranet Development v1.0 (TSG LIVE! 12 LT )
 
Amazon Cognitoで実装するパスキー (Security-JAWS【第33回】 勉強会)
Amazon Cognitoで実装するパスキー (Security-JAWS【第33回】 勉強会)Amazon Cognitoで実装するパスキー (Security-JAWS【第33回】 勉強会)
Amazon Cognitoで実装するパスキー (Security-JAWS【第33回】 勉強会)
 

Featured

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 

RailsでPaypal

  • 2. 自己紹介 • 水谷 裕生(みずたに ひろたか) • Railsエンジニア • Twitter: @icb54615
  • 3. システム概要 • いわゆるマーケットプレイス • サービスを提供する人と受ける人を結びつけ るプラットフォーム • サービスを予約した際にPayPalで決済 • プラットフォーム事業者に金額の何割かの手 数料を支払い • 残りはサービス提供する人に支払い • 予約期限までは予約のキャンセルが可能で、 支払らった金額を返金
  • 4. ¥サービス料 ¥手数料を引いた料金 ¥返金 サービスを受ける人 プラットフォーム事業者 サービスを提供する人 Sender Primary Receiver Secondary Receiver
  • 5. 仕様 • Adaptive Payments APIを使用 • active_paypal_adaptive_payment gem を使用 • Pay APIでDelayed Chained Payments による支払い • Refund APIで支払いの返金 • 予約期限が過ぎたらExecutePayment APIでサービスを提供する人に支払い
  • 6. Adaptive Payments API • 支払いを他の口座に入金が可能 • 自分の口座から他の口座への送金が可能 • 一回の支払いで複数の口座への支払いが可能(Parallel Payments) • 複数の口座への支払いを代理窓口として処理する事が可能 (Chainned Payments) 注意点: • ビジネスアカウントが必要 • 本番環境で運用するには、App IDをX.comから取得が必要 • Chainned Payments のような“advanced”な処理をする場合 はPayPalの事前審査が必要 ⇒審査に10-15日程度かかる場合がある
  • 7. active_paypal_adaptive_payment • Gemfileにgemを追加してbundle install • 設定ファイルを追加 • PayPalが開発用に用意しているSandbox のアカウントを登録して、その値を設定 ファイルに記述
  • 8. Gemfile: gem 'active_paypal_adaptive_payment' config/initializer/paypal.rb: unless Rails.env.production? ActiveMerchant::Billing::Base.mode = :test end PaypalGateway = ActiveMerchant::Billing::PaypalAdaptivePayment.new( :login => "hirotaka_1338865312_biz_api1.example.com", :password => "1338863333", :signature => "GKo3jGgDxJIjdH5V9tmlOoZlBDOzAMqkRgLDpvlxuu….", :appid => "APP-80W284485P519543T" )
  • 9. Pay APIでDelayed Chained Paymentsによる支払い
  • 10. def pay primary_receiver = { :primary => true, :email => "primary_receiver@example.com", :amount => 5000 } secondary_receiver = { :primary => false, :email => "secondary_receiver@example.com", :amount => 4000 } r = PaypalGateway.setup_purchase( :action_type => "PAY_PRIMARY", :receiver_list => [primary_receiver, secondary_receiver], :currency_code => "JPY", :error_language => "ja_JP", :return_url => url_for(:action=> :complete_payment), :cancel_url => url_for(:action=> :cancel_paypal_payment), ) if !r.success? || !r["pay_key"] message = "ご迷惑おかけしております。システムエラーが発生しました。" redirect_to url_for :action => error, :notice => message and return end payment = Payment.new payment.pay_key = r["pay_key"] payment.status = r["payment_exec_status"] redirect_to PaypalGateway.redirect_url_for(r["pay_key"]) end
  • 11. Refund APIで支払いの返金 • Rufund APIをpayKeyで呼び出す def cancel payment = Payment.find(params[:id]) r = PaypalGateway.refund( :pay_key => payment.pay_key, :currency_code => "JPY" ) unless r.success? redirect_to :action => :error end end
  • 13. Payment.where(:status => "INCOMPLETE").each do |payment| r = PaypalGateway.execute_payment :pay_key => payment.pay_key if r.success? && r["payment_exec_status"] payment.status = r["payment_exec_status"] else raise "PayPal connection error or unexpected response" end payment.save end
  • 14. その他感想など • いろいろな支払いの仕方ができるので、どのやり方が いいのか悩む • active_paypal_adaptive_paymentのドキュメント が全部のAPIをカバーしていない – Pay APIに対応するメソッドがわからない。 ActiveMerchant::Billing::PaypalAdaptivePayment::set up_purchaseがPay APIを呼んでるのはソースを見て確認。 – どんなオプションをわたすのがわからない。 – あとAPIに渡すキーの名前が、例えばpayKeyがpay_key だったり。 • PayPal Adaptive Payments Developer Guideは読み 込まないといけないけど英語のみ • App IDの申請も英語のみ