💳

Ruby ActiveMerchant sample code for WorldPay

2023/02/20に公開

require 'active_merchant'
require 'pp'
 
currency = 'USD'
 
gateway = ActiveMerchant::Billing::WorldpayGateway.new(
  :login => 'YOUR_MERCHANT_ID',
  :password => 'YOUR_XML_PASSWORD',
  :test => true,
)
 
money = 100
 
 
credit_card = ActiveMerchant::Billing::CreditCard.new(
  :number             => "4444333322221111",
  :month              => "12",
  :year               => Time.now.year + 1,
  :brand              => :visa,
  :first_name         => 'first',
  :last_name          => 'last',
)
 
order_id = 'label-' + Time.now.to_i.to_s
 
 
 
# 1. authorization
response1 = gateway.authorize(money, credit_card, {
  :order_id => order_id,
  :currency => currency,
})
 
pp response1
 
 
# 2. capture
#response2 = gateway.capture(money, response1.authorization, {
#  :authorization_validated => true, # avoid order status query
#})
#pp response2
 
# 3. void
#response3 = gateway.void(response1.authorization)
#pp response3
 
 
# 4. Reccuring payment (payAsOrder)
# Specify initial order_id instead of credit card comparing to the 1st payment.
subsequent_order_id = 'label-subsequent-' + Time.now.to_i.to_s
response4 = gateway.authorize(money, order_id, {
  :order_id => subsequent_order_id,
  :currency => currency,
})
 
pp response4


quoted from
https://matsu.teraren.com/blog/2014/02/25/worldpay-activemerchant/

Discussion