Skip to content

Commit

Permalink
Dotpay integration support.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemekciacka committed Mar 26, 2012
1 parent a63697a commit 9e7a799
Show file tree
Hide file tree
Showing 9 changed files with 408 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ The [ActiveMerchant Wiki](http://github.com/Shopify/active_merchant/wikis) conta
* [Chronopay](http://www.chronopay.com)
* [Direct-eBanking / sofortueberweisung.de by Payment-Networks AG](https://www.payment-network.com/deb_com_en/merchantarea/home) - DE, AT, CH, BE, UK, NL
* [DirecPay](http://www.timesofmoney.com/direcpay/jsp/home.jsp)
* [Dotpay](http://dotpay.pl)
* [Dwolla](https://www.dwolla.com/default.aspx)
* [HiTRUST](http://www.hitrust.com.hk/)
* [Moneybookers](http://www.moneybookers.com)
Expand Down
22 changes: 22 additions & 0 deletions lib/active_merchant/billing/integrations/dotpay.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module ActiveMerchant #:nodoc:
module Billing #:nodoc:
module Integrations #:nodoc:
module Dotpay
autoload :Return, File.dirname(__FILE__) + '/dotpay/return.rb'
autoload :Helper, File.dirname(__FILE__) + '/dotpay/helper.rb'
autoload :Notification, File.dirname(__FILE__) + '/dotpay/notification.rb'

mattr_accessor :service_url
self.service_url = 'https://ssl.dotpay.pl'

def self.notification(post, options = {})
Notification.new(post, options)
end

def self.return(post, options = {})
Return.new(post, options)
end
end
end
end
end
76 changes: 76 additions & 0 deletions lib/active_merchant/billing/integrations/dotpay/helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module ActiveMerchant #:nodoc:
module Billing #:nodoc:
module Integrations #:nodoc:
module Dotpay
class Helper < ActiveMerchant::Billing::Integrations::Helper
def initialize(order, account, options = {})
options = {:currency => 'PLN'}.merge options

super

add_field('channel', '0')
add_field('ch_lock', '0')
add_field('lang', 'PL')
add_field('onlinetransfer', '0')
add_field('tax', '0')
add_field('type', '2')
end

mapping :account, 'id'

mapping :billing_address, :street => 'street',
:street_n1 => 'street_n1',
:street_n2 => 'street_n2',
:addr2 => 'addr2',
:addr3 => 'addr3',
:city => 'city',
:postcode => 'postcode',
:phone => 'phone',
:country => 'country'

mapping :buttontext, 'buttontext'
mapping :channel, 'channel'
mapping :ch_lock, 'ch_lock'
mapping :code, 'code'
mapping :control, 'control'
mapping :currency, 'currency'

mapping :customer, :firstname => 'firstname',
:lastname => 'lastname',
:email => 'email'

mapping :description, 'description'
mapping :lang, 'lang'
mapping :onlinetransfer, 'onlinetransfer'
mapping :order, 'order'
mapping :p_email, 'p_email'
mapping :p_info, 'p_info'
mapping :tax, 'tax'
mapping :type, 'type'
mapping :url, 'url'
mapping :urlc, 'urlc'

def billing_address(params = {})
country = lookup_country_code(params.delete(:country) { 'POL' }, :alpha3)
add_field(mappings[:billing_address][:country], country)

# Everything else
params.each do |k, v|
field = mappings[:billing_address][k]
add_field(field, v) unless field.nil?
end
end

private

def lookup_country_code(name_or_code, format = country_format)
country = Country.find(name_or_code)
country.code(format).to_s
rescue InvalidCountryCodeError
name_or_code
end
end
end
end
end
end
85 changes: 85 additions & 0 deletions lib/active_merchant/billing/integrations/dotpay/notification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
require 'net/http'

module ActiveMerchant #:nodoc:
module Billing #:nodoc:
module Integrations #:nodoc:
module Dotpay
class Notification < ActiveMerchant::Billing::Integrations::Notification
def initialize(data, options = {})
if options[:pin].nil?
raise ArgumentError, "You need to provide the notification PIN as the option :pin to verify that the notification originated from Dotpay"
end
super
end

def complete?
status == 'OK' && %w(2 4 5).include?(t_status)
end

def currency
orginal_amount.split(' ')[1]
end

# the money amount we received in X.2 decimal.
def gross
params['amount']
end

def status
params['status']
end

PAYMENT_HOOK_FIELDS = [
:id,
:control,
:t_id,
:orginal_amount,
:email,
:service,
:code,
:username,
:password,
:t_status,
:description,
:md5,
:p_info,
:p_email,
:t_date
]

PAYMENT_HOOK_SIGNATURE_FIELDS = [
:id,
:control,
:t_id,
:amount,
:email,
:service,
:code,
:username,
:password,
:t_status
]

# Provide access to raw fields
PAYMENT_HOOK_FIELDS.each do |key|
define_method(key.to_s) do
params[key.to_s]
end
end

def generate_signature_string
"#{@options[:pin]}:" + PAYMENT_HOOK_SIGNATURE_FIELDS.map {|key| params[key.to_s]} * ":"
end

def generate_signature
Digest::MD5.hexdigest(generate_signature_string)
end

def acknowledge
generate_signature.to_s == md5.to_s
end
end
end
end
end
end
11 changes: 11 additions & 0 deletions lib/active_merchant/billing/integrations/dotpay/return.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module ActiveMerchant #:nodoc:
module Billing #:nodoc:
module Integrations #:nodoc:
module Dotpay
class Return < ActiveMerchant::Billing::Integrations::Return
end
end
end
end
end

13 changes: 13 additions & 0 deletions test/unit/integrations/dotpay_module_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'test_helper'

class DotpayModuleTest < Test::Unit::TestCase
include ActiveMerchant::Billing::Integrations

def test_notification_method
assert_instance_of Dotpay::Notification, Dotpay.notification('name=cody', :pin => '1234567890')
end

def test_return
assert_instance_of Dotpay::Return, Dotpay.return("name=me", {})
end
end
107 changes: 107 additions & 0 deletions test/unit/integrations/helpers/dotpay_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
require 'test_helper'

class DotpayHelperTest < Test::Unit::TestCase
include ActiveMerchant::Billing::Integrations

def setup
@helper = Dotpay::Helper.new('order-500', '42655', :amount => 500)
@url = 'http://someuri.com/return.html'
end

def test_basic_helper_fields
assert_field 'id', '42655'
assert_field 'lang', 'PL'
assert_field 'order', 'order-500'
assert_field 'currency', 'PLN'
end

def test_customer_fields
@helper.customer :firstname => 'Przemyslaw', :lastname => 'Ciacka', :email => '[email protected]'
assert_field 'firstname', 'Przemyslaw'
assert_field 'lastname', 'Ciacka'
assert_field 'email', '[email protected]'
end

def test_address_mapping
@helper.billing_address :street => 'Malborska',
:street_n1 => '130',
:city => 'Cracow',
:postcode => '30-624'

assert_field 'street', 'Malborska'
assert_field 'street_n1', '130'
assert_field 'city', 'Cracow'
assert_field 'postcode', '30-624'
assert_field 'country', 'POL'
end

def test_description
@helper.description = 'Order 500/2012'
assert_field 'description', 'Order 500/2012'
end

def test_channel
assert_field 'channel', '0'
@helper.channel = '2'
assert_field 'channel', '2'
end

def test_ch_lock
assert_field 'ch_lock', '0'
@helper.ch_lock = '1'
assert_field 'ch_lock', '1'
end

def test_onlinetransfer
assert_field 'onlinetransfer', '0'
@helper.onlinetransfer = 1
assert_field 'onlinetransfer', '1'
end

def test_url
@helper.url = @url
assert_field 'url', @url
end

def test_urlc
@helper.urlc = @url
assert_field 'urlc', @url
end

def test_type
assert_field 'type', '2'
@helper.type = '3'
assert_field 'type', '3'
end

def test_buttontext
@helper.buttontext = 'Return to the shop'
assert_field 'buttontext', 'Return to the shop'
end

def test_control
@helper.control = 'ThisISSOMEControlP@rameter'
assert_field 'control', 'ThisISSOMEControlP@rameter'
end

def test_code
@helper.code = 'somecode'
assert_field 'code', 'somecode'
end

def test_p_info
@helper.p_info = 'Company Name'
assert_field 'p_info', 'Company Name'
end

def test_p_email
@helper.p_email = '[email protected]'
assert_field 'p_email', '[email protected]'
end

def test_tax
assert_field 'tax', '0'
@helper.tax = '1'
assert_field 'tax', '1'
end
end
Loading

0 comments on commit 9e7a799

Please sign in to comment.