如何使用Ruby通过Paymill付款

时间:2013-02-26 13:10:34

标签: ruby e-commerce payment-gateway activemerchant paymill

我需要向Paymill付款,我希望能够使用Ruby语言实现这一目标。

更新

我在github上公开发布了paymill_on_rails。这是一个基于Rails 4.0.0和paymill-ruby的Paymill订阅系统,运行在ruby-2.0.0-p247上

另请参阅home project

示例应用程序也部署在Heroku上。请随意岔路口最终贡献。

2 个答案:

答案 0 :(得分:5)

我正在使用来自https://github.com/dkd/paymill-ruby

的paymill gem

它非常易于使用,只需按照自述文件进行操作即可了解它的可能性。它还支持订阅。

答案 1 :(得分:1)

我设法使用以下步骤轻松实现了这一目标:

  1. Paymill创建新帐户。

  2. 从Paymill设置页面获取公钥和私钥

  3. 安装activemerchant gem:

    gem install activemerchant
    
  4. 我使用以下脚本进行购买

  5. 请注意,只要您不在Paymill激活帐户,它就会在测试模式下运行。所以没有钱实际上会被转移。他们还列出了永远不会被扣款的test credit cards

    脚本:

    require 'rubygems'
    require 'active_merchant'
    require 'json'
    
    # Use the TrustCommerce test servers
    ActiveMerchant::Billing::Base.mode = :test
    
    gateway = ActiveMerchant::Billing::PaymillGateway.new(
        :public_key => 'MY_PAYMILL_PUBLIC_KEY', 
        :private_key => 'MY_PAYMILL_PRIVATE_KEY')
    
    gateway.default_currency = 'USD'
    
    # ActiveMerchant accepts all amounts as Integer values in cents
    amount = 1000  # $10.00
    
    # The card verification value is also known as CVV2, CVC2, or CID
    credit_card = ActiveMerchant::Billing::CreditCard.new(
        :first_name         => 'Bob',
        :last_name          => 'Bobsen',
        :number             => '5500000000000004',
        :month              => '8',
        :year               => Time.now.year+1,
        :verification_value => '000')
    
    # Validating the card automatically detects the card type
    if credit_card.valid?
    
    # Capture the amount from the credit card
    response = gateway.purchase(amount, credit_card)
    
    if response.success?
    puts "Successfully charged $#{sprintf("%.2f", amount / 100)} to the credit card #{credit_card.display_number}"
    else
    raise StandardError, response.message
    end
    end