Created
June 7, 2011 23:07
-
-
Save amasses/1013403 to your computer and use it in GitHub Desktop.
Spree Payments Issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # lib/gateway/money_monkey.rb | |
| class Gateway::MoneyMonkey < Gateway | |
| preference :login, :string | |
| preference :token, :string | |
| def provider_class | |
| ActiveMerchant::Billing::MoneyMonkeyGateway | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # lib/money_monkey.rb | |
| require 'spree_core' | |
| module MoneyMonkey | |
| class Engine < Rails::Engine | |
| config.autoload_paths += %W(#{config.root}/lib) | |
| def self.activate | |
| require 'gateway/money_monkey' | |
| require 'active_merchant/billing/money_monkey_gateway' | |
| Gateway::MoneyMonkey.register | |
| end | |
| config.to_prepare &method(:activate).to_proc | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # In Gem for extending active merchant | |
| # lib/active_merchant/billing/money_monkey_gateway.rb | |
| require 'json' | |
| module ActiveMerchant #:nodoc: | |
| module Billing #:nodoc: | |
| class MoneyMonkeyGateway < Gateway | |
| TEST_URL = 'http://moneymonkey.dev:88' | |
| LIVE_URL = 'https://gateway.moneymonkey.com' | |
| # The countries the gateway supports merchants from as 2 digit ISO country codes | |
| self.supported_countries = ['AU'] | |
| # The card types supported by the payment gateway | |
| self.supported_cardtypes = [:visa, :master] | |
| # The homepage URL of the gateway | |
| self.homepage_url = 'http://www.moneymonkey.com/' | |
| # The name of the gateway | |
| self.display_name = 'MoneyMonkey' | |
| def initialize(options = {}) | |
| requires!(options, :login, :token) | |
| @options = options | |
| super | |
| end | |
| def authorize(money, creditcard, options = {}) | |
| post = {} | |
| add_invoice(post, options) | |
| add_creditcard(post, creditcard) | |
| commit('authonly', money, post) | |
| end | |
| def purchase(money, creditcard, options = {}) | |
| post = {} | |
| add_invoice(post, options) | |
| add_creditcard(post, creditcard) | |
| commit('purchase', money, post) | |
| end | |
| def capture(money, authorization, options = {}) | |
| post = { | |
| :authorization_id => authorization, | |
| :amount => money, | |
| :reference => options[:reference], | |
| :customer_ip => options[:ip] | |
| } | |
| commit('capture', money, post) | |
| end | |
| def refund(money, authorization, options = {}) | |
| post = { | |
| :transaction_id => authorization, | |
| :amount => money, | |
| :reference => options[:reference], | |
| :customer_ip => options[:ip] | |
| } | |
| commit('refund', money, post) | |
| end | |
| def credit(money, identification, options = {}) | |
| puts "Crediting!" | |
| end | |
| def void(authorization, options = {}) | |
| puts "VOIDING" | |
| end | |
| private | |
| def add_invoice(post, options) | |
| post[:reference] = options[:reference] | |
| post[:customer_ip] = options[:ip] | |
| end | |
| def add_creditcard(post, creditcard) | |
| post[:card_number] = creditcard.number | |
| post[:card_holder] = [creditcard.first_name, creditcard.last_name].join(" ") | |
| post[:cvv] = creditcard.verification_value | |
| post[:card_expiry] = [creditcard.month, creditcard.year].join("/") | |
| end | |
| def parse(body) | |
| # Parse the API response | |
| data = JSON.parse(body) | |
| success = data["response"]["authorized"] || | |
| data["response"]["captured"] || | |
| data["response"]["successful"] || | |
| data["response"]["refunded"] | |
| Response.new( | |
| (data["successful"] && success), | |
| message_from(data), | |
| { | |
| :errors => data["errors"], | |
| :amount => data["response"]["amount"], | |
| :reference => data["response"]["reference"], | |
| :transaction_id => data["response"]["transaction_id"] | |
| }, | |
| :authorization => data["response"]["authorization"], | |
| :test => test?) | |
| end | |
| def commit(action, money, parameters) | |
| parameters[:amount] = money | |
| response = ssl_post(gateway_url(action, parameters), parameters.to_json, auth_headers) | |
| parse(response) | |
| end | |
| def message_from(response) | |
| response["response"]["message"] || response["errors"].join(", ") | |
| end | |
| def auth_headers | |
| {"Authorization" => "Basic " + ActiveSupport::Base64.encode64s("#{options[:login]}:#{options[:token]}").strip} | |
| end | |
| def gateway_url(method, options = {}) | |
| base_url = self.test? ? TEST_URL : LIVE_URL | |
| case method | |
| when 'authonly' | |
| base_url + "/authorizations" | |
| when 'capture' | |
| base_url + "/authorizations/#{options[:authorization_id]}/capture" | |
| when 'purchase' | |
| base_url + "/purchases" | |
| when 'refund' | |
| base_url + "/refunds" | |
| end | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment