首页
/ Active Merchant 技术文档

Active Merchant 技术文档

2024-12-23 02:45:27作者:廉皓灿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 进行支付处理。

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

热门内容推荐

最新内容推荐

项目优选

收起
kernelkernel
deepin linux kernel
C
22
6
docsdocs
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
153
1.98 K
ops-mathops-math
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
505
42
nop-entropynop-entropy
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
8
0
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
194
279
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
992
395
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
938
554
communitycommunity
本项目是CANN开源社区的核心管理仓库,包含社区的治理章程、治理组织、通用操作指引及流程规范等基础信息
332
11
openGauss-serveropenGauss-server
openGauss kernel ~ openGauss is an open source relational database management system
C++
146
191
金融AI编程实战金融AI编程实战
为非计算机科班出身 (例如财经类高校金融学院) 同学量身定制,新手友好,让学生以亲身实践开源开发的方式,学会使用计算机自动化自己的科研/创新工作。案例以量化投资为主线,涉及 Bash、Python、SQL、BI、AI 等全技术栈,培养面向未来的数智化人才 (如数据工程师、数据分析师、数据科学家、数据决策者、量化投资人)。
Python
75
70