決済 gem WebPay ; Ruby ; PHP Java Python もサポート

決済処理に当たって、ActiveMerchant もあったが決済会社選んだり GateWay がどうとかって メンドイ。

WebPay-san お願いします。 (ここにあるカード番号、全てテスト用なので、あしからず)

もともとは WebAPI なので、PHP Java Python でも使えるらしい{1}

gem

gem install webpay

課金

require 'webpay'

# YAMADA TARO の 
# JCB カード(3566-0020-2036-0505, 有効期限 11/2014, セキュリティコード 123)
# に 日本円 1,000- 課金する

WebPay.api_key = 'test_secret_eHn4TTgsGguBcW764a2KA8Yd'
WebPay::Charge.create(
  amount: 1000,
  currency: 'jpy',
  card: {
    number: '3566-0020-2036-0505',
    exp_month: '11',
    exp_year: '2014',
    cvc: '123',
    name: 'TARO YAMADA'
  }
)

顧客情報保存

WebPay::Customer.create した時、id にその顧客に対する

一意文字列が割り当てられる。これをDBか何かに保存する。

(もう一度この顧客情報を使いたければ、WebPay 側にこの id を使って問い合せればよい(後述))

require 'webpay'

WebPay.api_key = 'test_secret_eHn4TTgsGguBcW764a2KA8Yd'
WebPay::Customer.create(
  card: {
    number: '3566-0020-2036-0505',
    exp_month: '11',
    exp_year: '2014',
    cvc: '123',
    name: 'HANAKO YAMADA'
  }
)
#=>
#<WebPay::Customer:0x95fd1c0 @updated_attributes={},
@attributes=
{"id"=>"cus_fNU8s20HRcLQc9H",
"object"=>"customer",
"livemode"=>false,
"created"=>1391323774,
"email"=>nil,
"description"=>nil,
"active_card"=>#"card",
             "exp_year"=>2014,
             "exp_month"=>11,
             "fingerprint"=>"1cff7dadf64a34a93ae6d33df5badb7e1c23ebe9",
             "name"=>"KEI KUBO",
             "country"=>"JP", 
             "type"=>"JCB",
             "cvc_check"=>"pass",
             "last4"=>"0505"}>
}>

既存顧客への課金

上述 YAMADA HANAKO(cus_fNU8s20HRcLQc9H) へ 1,000- 課金

require 'webpay'

WebPay.api_key = 'test_secret_eHn4TTgsGguBcW764a2KA8Yd'
WebPay::Charge.create(
  amount: 1000,
  currency: 'jpy',
  customer: 'cus_fNU8s20HRcLQc9H'
)
#=>
#"ch_fNUebb5Ab5Yr83V", 
"object"=>"charge",
"livemode"=>false,
"currency"=>"jpy",
"description"=>nil,
"amount"=>1000,
"amount_refunded"=>0,
"customer"=>"cus_fNU8s20HRcLQc9H",
"created"=>1391324628,
"paid"=>true,
"refunded"=>false,
"failure_message"=>nil,
"card"=>#"card",
             "exp_year"=>2014,
             "exp_month"=>11,
             "fingerprint"=>"1cff7dadf64a34a93ae6d33df5badb7e1c23ebe9",
             "name"=>"KEI KUBO",
             "country"=>"JP",
             "type"=>"JCB",
             "cvc_check"=>"pass",
             "last4"=>"0505"}>,
"captured"=>true,
"expire_time"=>nil
}>

引用元/情報元 # ありがとうございます

{1}イントロダクション | WebPay: 開発者向けクレジットカード決済サービス

今からでも遅くないWebpay入門 - Qiita [キータ]