首页
/ Active Merchant 技术文档

Active Merchant 技术文档

2024-12-23 20:06:44作者:廉皓灿Ida

1. 安装指南

从 Git 安装

你可以通过 Git 克隆最新的源代码:

git clone git://github.com/activemerchant/active_merchant.git

从 RubyGems 安装

通过 RubyGems 安装:

gem install activemerchant

如果你使用 Bundler,只需在 Gemfile 中添加以下内容:

gem 'activemerchant'

2. 项目使用说明

Active Merchant 是一个从 Shopify 电子商务系统中提取出来的库,旨在提供一个简单且统一的 API,用于访问数十种不同的支付网关。它最初是为 Ruby on Rails 应用程序开发的,但也可以作为独立的 Ruby 库使用。

以下是一个简单的示例,演示如何使用 Active Merchant 进行信用卡支付:

require 'active_merchant'

# 使用 TrustCommerce 测试服务器
ActiveMerchant::Billing::Base.mode = :test

gateway = ActiveMerchant::Billing::TrustCommerceGateway.new(
            :login => 'TestMerchant',
            :password => 'password')

# ActiveMerchant 接受所有金额作为整数值(以分为单位)
amount = 1000  # $10.00

# 信用卡验证值也称为 CVV2、CVC2 或 CID
credit_card = ActiveMerchant::Billing::CreditCard.new(
                :first_name         => 'Bob',
                :last_name          => 'Bobsen',
                :number             => '4242424242424242',
                :month              => '8',
                :year               => Time.now.year+1,
                :verification_value => '000')

# 验证信用卡会自动检测卡类型
if credit_card.validate.empty?
  # 从信用卡中扣除 $10
  response = gateway.purchase(amount, credit_card)

  if response.success?
    puts "成功从信用卡 #{credit_card.display_number} 扣除 $#{sprintf("%.2f", amount / 100)}"
  else
    raise StandardError, response.message
  end
end

3. 项目 API 使用文档

Active Merchant 提供了丰富的 API 接口,用于处理各种支付网关的交互。以下是一些常用的 API 方法:

  • gateway.purchase(amount, credit_card): 从信用卡中扣除指定金额。
  • gateway.authorize(amount, credit_card): 授权但不扣款。
  • gateway.capture(amount, authorization): 捕获之前授权的金额。
  • gateway.void(transaction_id): 取消交易。
  • gateway.credit(amount, credit_card): 退款。

更多详细信息,请参考 API 文档

4. 项目安装方式

Active Merchant 可以通过以下两种方式安装:

从 Git 安装

git clone git://github.com/activemerchant/active_merchant.git

从 RubyGems 安装

gem install activemerchant

如果你使用 Bundler,只需在 Gemfile 中添加以下内容:

gem 'activemerchant'

通过以上步骤,你可以轻松安装并使用 Active Merchant 进行支付处理。

登录后查看全文
热门项目推荐
相关项目推荐