-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from visini/add-merchant-authorize
Add `JSON::Transaction::MerchantAuthorize`
- Loading branch information
Showing
5 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'httparty' | ||
require 'datatrans/json/transaction/response' | ||
|
||
class Datatrans::JSON::Transaction | ||
class MerchantAuthorize | ||
# class to authorize a transaction without user interaction https://api-reference.datatrans.ch/#tag/v1transactions/operation/authorize | ||
attr_accessor :params, :datatrans | ||
|
||
def initialize(datatrans, params) | ||
@datatrans = datatrans | ||
@params = params | ||
end | ||
|
||
def post(url, options = {}) | ||
options = options | ||
.merge(self.datatrans.proxy) | ||
.merge(:basic_auth => { :username => self.datatrans.merchant_id, :password => self.datatrans.password }) | ||
HTTParty.post(url, **options) | ||
end | ||
|
||
def process | ||
post(self.datatrans.url(:authorize_transaction), | ||
:headers => { 'Content-Type' => 'application/json' }, | ||
:body => request_body.to_json).parsed_response | ||
end | ||
|
||
def request_body | ||
auto_settle = params[:auto_settle].nil? ? true : params[:auto_settle] | ||
|
||
{ | ||
"currency": params[:currency], | ||
"refno": params[:refno], | ||
"amount": params[:amount], | ||
"card": params[:card], | ||
"autoSettle": auto_settle, | ||
} | ||
end | ||
end | ||
|
||
class MerchantAuthorizeResponse < Response | ||
def successful? | ||
params["error"].blank? && params["transactionId"].present? | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
require "spec_helper" | ||
|
||
describe Datatrans::JSON::Transaction::MerchantAuthorize do | ||
before do | ||
@successful_response = { | ||
"transactionId" => "230223022302230223", | ||
"acquirerAuthorizationCode" => "123456", | ||
"card" => { | ||
"masked" => "411111xxxxxx1111" | ||
} | ||
} | ||
|
||
@failed_response = { | ||
"error" => { | ||
"code" => "INVALID_PROPERTY", | ||
"message" => "authorize.card.alias or number is mandatory" | ||
} | ||
} | ||
|
||
@valid_params = { | ||
currency: "CHF", | ||
refno: "B4B4B4B4B", | ||
amount: 1000, | ||
card: { | ||
alias: "AAABcH0Bq92s3kgAESIAAbGj5NIsAHWC", | ||
expiryMonth: "01", | ||
expiryYear: "23" | ||
}, | ||
auto_settle: true | ||
} | ||
|
||
@expected_request_body = { | ||
"currency": "CHF", | ||
"refno": "B4B4B4B4B", | ||
"amount": 1000, | ||
"card": { | ||
"alias": "AAABcH0Bq92s3kgAESIAAbGj5NIsAHWC", | ||
"expiryMonth": "01", | ||
"expiryYear": "23" | ||
}, | ||
"autoSettle": true | ||
} | ||
|
||
@invalid_params = { | ||
currency: "CHF", | ||
refno: "B4B4B4B4B", | ||
amount: 1000, | ||
card: { | ||
expiryMonth: "01", | ||
expiryYear: "23" | ||
} | ||
} | ||
end | ||
|
||
context "successful response" do | ||
before do | ||
allow_any_instance_of(Datatrans::JSON::Transaction::MerchantAuthorize).to receive(:process).and_return(@successful_response) | ||
end | ||
|
||
it "generates correct request_body" do | ||
request = Datatrans::JSON::Transaction::MerchantAuthorize.new(@datatrans, @valid_params) | ||
expect(request.request_body).to eq(@expected_request_body) | ||
end | ||
|
||
it "#process handles a valid datatrans merchant authorize response" do | ||
transaction = Datatrans::JSON::Transaction.new(@datatrans, @valid_params) | ||
expect(transaction.merchant_authorize).to be true | ||
end | ||
end | ||
|
||
context "with autoSettle specified" do | ||
it "handles autoSettle correctly in request_body" do | ||
params_with_auto_settle = @valid_params.merge(auto_settle: false) | ||
request = Datatrans::JSON::Transaction::MerchantAuthorize.new(@datatrans, params_with_auto_settle) | ||
|
||
expected_request_body_without_auto_settle = @expected_request_body.merge(autoSettle: false) | ||
expect(request.request_body).to eq(expected_request_body_without_auto_settle) | ||
end | ||
end | ||
|
||
context "failed response" do | ||
before do | ||
allow_any_instance_of(Datatrans::JSON::Transaction::MerchantAuthorize).to receive(:process).and_return(@failed_response) | ||
@transaction = Datatrans::JSON::Transaction.new(@datatrans, @invalid_params) | ||
end | ||
|
||
it "#process handles a failed datatrans merchant authorize response" do | ||
expect(@transaction.merchant_authorize).to be false | ||
end | ||
|
||
it "returns error details" do | ||
@transaction.merchant_authorize | ||
expect(@transaction.response.error_code).to eq "INVALID_PROPERTY" | ||
expect(@transaction.response.error_message).to eq "authorize.card.alias or number is mandatory" | ||
end | ||
end | ||
end |