代码之家  ›  专栏  ›  技术社区  ›  Besi

如何使用Ruby通过Paymill进行支付

  •  5
  • Besi  · 技术社区  · 13 年前

    我需要付款给 Paymill 我希望能够使用Ruby语言实现这一点。

    更新:

    我确实公开发布了 paymill_on_rails 在github上。这是一个基于Rails 4.0.0和 paymill-ruby ,在ruby-2.0.0-p247上运行

    另请参阅 home project

    上还部署了一个示例应用程序 Heroku 。请随意使用叉子&最终做出贡献。

    2 回复  |  直到 12 年前
        1
  •  5
  •   Kryptman    13 年前

    我用的是来自 https://github.com/dkd/paymill-ruby

    它真的很容易使用,只需遵循自述,你就会对它的可能性有一个想法。它还支持订阅。

        2
  •  1
  •   Besi    13 年前

    我通过以下步骤很容易地做到了这一点:

    1. 在创建新帐户 Paymill .

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

    3. 安装 activemerchant 宝石:

      gem install activemerchant
      
    4. 我使用了下面的脚本进行购买

    请注意,只要您不在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