Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ before_install:
- gem install bundler
- gem update bundler
rvm:
- 1.9.3
- 2.0.0
- 2.4.1
- ruby-head
env:
- "rack=1.6.4"
20 changes: 11 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
payex (0.3)
payex (0.4)
httpclient (~> 2.2)
nori (~> 2.6)
savon (~> 2.11)
Expand All @@ -12,7 +12,7 @@ GEM
akami (1.3.1)
gyoku (>= 0.4.0)
nokogiri
builder (3.2.2)
builder (3.2.3)
diff-lcs (1.1.3)
guard (1.3.3)
listen (>= 0.4.2)
Expand All @@ -24,15 +24,16 @@ GEM
guard (>= 1.1)
gyoku (1.3.1)
builder (>= 2.1.2)
httpclient (2.8.0)
httpi (2.4.1)
httpclient (2.8.3)
httpi (2.4.2)
rack
socksify
listen (0.5.2)
mini_portile2 (2.0.0)
nokogiri (1.6.7.2)
mini_portile2 (~> 2.0.0.rc2)
mini_portile2 (2.2.0)
nokogiri (1.8.0)
mini_portile2 (~> 2.2.0)
nori (2.6.0)
rack (1.6.4)
rack (2.0.3)
rake (0.9.2.2)
rb-fsevent (0.9.2)
rspec (2.11.0)
Expand All @@ -51,6 +52,7 @@ GEM
nokogiri (>= 1.4.0)
nori (~> 2.4)
wasabi (~> 3.4)
socksify (1.7.1)
terminal-notifier-guard (1.5.3)
thor (0.16.0)
wasabi (3.5.0)
Expand All @@ -72,4 +74,4 @@ DEPENDENCIES
terminal-notifier-guard (~> 1.5)

BUNDLED WITH
1.11.2
1.14.6
28 changes: 18 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,49 @@ gem install payex

## Usage

This library only implements the "credit card redirect" method.

Here's how a basic credit card transaction works:

```ruby
require 'payex'

PayEx.account_number = 123456789
PayEx.encryption_key = 'e4939be3910ebu194'
PayEx.default_currency = 'SEK'
#PayEx.base_url = PayEx::TEST_URL # (use this for testing)
# PayEx.base_url = PayEx::TEST_URL # (use this for testing)

# (an arbitrary string you can use to identify this transaction)
my_order_id = 'c704acc45a4bec4c8cd50b73fb01a7c7'

payment_url = PayEx::CreditCardRedirect.initialize_transaction!
# Credit card example:
payment_url = PayEx::Redirect.initialize!
price: 14900, # (in cents)
order_id: my_order_id,
product_number: '123456',
product_description: 'Brief product description',
description: 'Brief product description',
client_ip_address: '12.34.56.78',
return_url: 'http://example.com/payex-return',
cancel_url: 'http://example.com/payex-cancel'

# Swish example:
payment_url = PayEx::Redirect.initialize!
price: 14900, # (in cents)
customer_ip: '12.34.56.78',
order_id: my_order_id,
product_number: '123456',
description: 'Brief product description',
client_ip_address: '12.34.56.78',
return_url: 'http://example.com/payex-return',
view: 'SWISH'
cancel_url: 'http://example.com/payex-cancel'
```

After redirecting the customer to `payment_url`, they'll enter their
payment details and then PayEx will redirect them back to `return_url`
with a parameter called `orderRef` appended to the query string.

The `PayEx::CreditCardRedirect.complete_transaction!` method takes
The `PayEx::Redirect.complete!` method takes
this `orderRef` string as input and returns your order ID as output.

```ruby
order_id, error, raw_response =
PayEx.complete_transaction! '9b4031c19960da92d'
PayEx::Redirect.complete! '9b4031c19960da92d'
case error
when nil
# The transaction succeeded (use `order_id` to proceed).
Expand Down
2 changes: 1 addition & 1 deletion lib/payex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ class PayEx::Error::CardDeclined < PayEx::Error; end

require 'payex/api'
require 'payex/api/pxorder'
require 'payex/credit_card_redirect'
require 'payex/redirect'
5 changes: 3 additions & 2 deletions lib/payex/api/pxorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def Initialize8(params)
default: proc { PayEx.account_number! }
},
'purchaseOperation' => {
signed: true
signed: true,
default: 'SALE'
},
'price' => {
signed: true,
Expand Down Expand Up @@ -52,7 +53,7 @@ def Initialize8(params)
},
'additionalValues' => {
signed: true,
default: ''
default: 'RESPONSIVE=1'
},
'externalID' => {
signed: true,
Expand Down
32 changes: 21 additions & 11 deletions lib/payex/credit_card_redirect.rb → lib/payex/redirect.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
module PayEx::CreditCardRedirect
module PayEx::Redirect
extend self

def initialize_transaction! params
response = PayEx::PxOrder.Initialize8 \
def initialize!(params)
response = PayEx::PxOrder.Initialize8(
accountNumber: params[:account_number],
purchaseOperation: params[:purchase_operation],
price: params[:price],
priceArgList: params[:price_arg_list],
currency: params[:currency],
vat: params[:vat],
orderID: params[:order_id],
purchaseOperation: 'AUTHORIZATION',
productNumber: params[:product_number],
description: params[:product_description],
price: params[:price],
clientIPAddress: params[:customer_ip],
description: params[:description],
clientIPAddress: params[:client_ip_address],
clientIdentifier: params[:client_identifier],
additionalValues: params[:additional_values],
externalID: params[:external_id],
returnUrl: params[:return_url],
cancelUrl: params[:cancel_url]

view: params[:view],
agreementRef: params[:agreement_ref],
cancelUrl: params[:cancel_url],
clientLanguage: params[:client_language]
)
response[:redirect_url]
end

def complete_transaction! id
def complete!(id)
response = PayEx::PxOrder.Complete(orderRef: id)

status = response[:transaction_status]
status = PayEx::API.parse_transaction_status(status)

case status
when :authorize
when :sale, :authorize
error = nil
when :initialize
error = PayEx::Error.new('Transaction not completed')
Expand Down
28 changes: 15 additions & 13 deletions spec/credit_card_redirect_spec.rb → spec/redirect_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# -*- coding: utf-8 -*-
require 'payex'
require 'spec_helper'
require "savon/mock/spec_helper"
require 'savon/mock/spec_helper'

describe PayEx::CreditCardRedirect do
describe PayEx::Redirect do
include Savon::SpecHelper

before(:all) { savon.mock! }
after(:all) { savon.unmock! }

SAMPLE_ACCOUNT_NUMBER = 'SAMPLEACCOUNTNUMBER0001'
SAMPLE_ENCRYPTION_KEY = 'SAMPLEENCRYPTIONKEY0001'
SAMPLE_DEFAULT_CURRENCY = 'SEK'
Expand All @@ -26,7 +27,7 @@
SAMPLE_RETURN_URL = 'http://example.com/payex-return'
SAMPLE_CANCEL_URL = 'http://example.com/payex-cancel'

describe :initialize_transaction! do
describe :initialize! do
example 'successful initialization' do
expected = {
'accountNumber' => SAMPLE_ACCOUNT_NUMBER,
Expand All @@ -40,35 +41,36 @@
'description' => SAMPLE_PRODUCT_DESCRIPTION,
'clientIPAddress' => SAMPLE_IP_ADDRESS,
'clientIdentifier' => '',
'additionalValues' => '',
'additionalValues' => 'RESPONSIVE=1',
'externalID' => '',
'returnUrl' => SAMPLE_RETURN_URL,
'view' => 'CREDITCARD',
'agreementRef' => '',
'cancelUrl' => SAMPLE_CANCEL_URL,
'clientLanguage' => ''
}

expected['hash'] = PayEx::API.signed_hash(expected.values.join)

initialize_ok_fixture = File.read('spec/fixtures/initialize8/initialize_ok.xml')
savon.expects(:initialize8).with(message: expected).returns(initialize_ok_fixture)

href = PayEx::CreditCardRedirect.initialize_transaction! \
href = PayEx::Redirect.initialize!(
purchase_operation: 'AUTHORIZATION',
price: SAMPLE_PRICE_CENTS,
order_id: SAMPLE_ORDER_ID,
product_number: SAMPLE_PRODUCT_NUMBER,
product_description: SAMPLE_PRODUCT_DESCRIPTION,
price: SAMPLE_PRICE_CENTS,
customer_ip: SAMPLE_IP_ADDRESS,
description: SAMPLE_PRODUCT_DESCRIPTION,
client_ip_address: SAMPLE_IP_ADDRESS,
return_url: SAMPLE_RETURN_URL,
cancel_url: SAMPLE_CANCEL_URL

)
href.should include 'http'
end
end

SAMPLE_ORDER_REF = 'SAMPLEORDERREF0001'

describe :complete_transaction! do
describe :complete! do
def invoke_complete! response_fixture_file
expected = {
'accountNumber' => SAMPLE_ACCOUNT_NUMBER,
Expand All @@ -78,7 +80,7 @@ def invoke_complete! response_fixture_file
expected['hash'] = PayEx::API.signed_hash(expected.values.join)
response_fixture = File.read("spec/fixtures/complete/#{response_fixture_file}.xml")
savon.expects(:complete).with(message: expected).returns(response_fixture)
PayEx::CreditCardRedirect.complete_transaction! SAMPLE_ORDER_REF
PayEx::Redirect.complete! SAMPLE_ORDER_REF
end

example 'successful completion' do
Expand Down