【问题标题】:Correct way to require Payment API in Rails在 Rails 中要求支付 API 的正确方法
【发布时间】:2025-12-17 07:45:02
【问题描述】:

我正在尝试使用 Mollie API 集成 Mollie Payments 我将 gem 添加到我的 gemfile 并运行 bundle install。之后我对我现有的控制器做了这个:

class PagesController < ApplicationController

  require 'Mollie/API/Client'

  def form_page
    mollie = Mollie::API::Client.new
    mollie.api_key = 'test_SKQzVUv7Rs7AUqmW7FdTvF9SbEujmQ'

    payment = mollie.payments.create(
        amount: 10.00,
        description: 'My first API payment',
        redirectUrl: '/index'
    )

    payment = mollie.payments.get(payment.id)

    if payment.paid?
      puts 'Payment received.'
    end
  end

  def success
  end
end

form_page 是“新”方法,“success”是付款成功后应该重定向到的页面。

但是当进入 form_page 视图时,我得到了这个错误:

NoMethodError (undefined method `api_key=' for #<Mollie::API::Client:0x007fa6fb8>)

所以我的猜测是 API 不需要以正确的方式或类似的方式。有人知道我做错了什么吗?任何帮助将不胜感激!

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 rubygems


    【解决方案1】:

    为避免此异常,请尝试使用方法setApiKey 而不是api_key =

      mollie = Mollie::API::Client.new
      mollie.setApiKey "test_dHar4XY7LxsDOtmnkVtjNVWXLSlXsM"
    

    更多示例如何处理 webapp 请求在示例目录中描述:

    https://github.com/mollie/mollie-api-ruby/tree/master/examples

    【讨论】: