-
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 #52 from visini/add-json-transaction-settle
Add `JSON::Transaction::Settle`
- Loading branch information
Showing
4 changed files
with
107 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'httparty' | ||
require 'datatrans/json/transaction/response' | ||
|
||
class Datatrans::JSON::Transaction | ||
class Settle | ||
# Class to settle a transaction https://api-reference.datatrans.ch/#tag/v1transactions/operation/settle | ||
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(:json_settle_url, transaction_id: params[:transaction_id]), | ||
:headers => { 'Content-Type' => 'application/json' }, | ||
:body => request_body.to_json).parsed_response | ||
end | ||
|
||
def request_body | ||
{ | ||
"currency": params[:currency], | ||
"amount": params[:amount], | ||
"refno": params[:refno] | ||
} | ||
end | ||
end | ||
|
||
class SettleResponse < Response | ||
def successful? | ||
params["error"].blank? | ||
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,55 @@ | ||
require "spec_helper" | ||
|
||
describe Datatrans::JSON::Transaction::Settle do | ||
before do | ||
@valid_params_authorize = { | ||
currency: "CHF", | ||
refno: "B4B4B4B4B", | ||
amount: 1337, | ||
payment_methods: ["ECA", "VIS"], | ||
success_url: "https://pay.sandbox.datatrans.com/upp/merchant/successPage.jsp", | ||
cancel_url: "https://pay.sandbox.datatrans.com/upp/merchant/cancelPage.jsp", | ||
error_url: "https://pay.sandbox.datatrans.com/upp/merchant/errorPage.jsp" | ||
} | ||
|
||
@valid_params_settle = { | ||
transaction_id: '230223022302230223', | ||
amount: 1337, | ||
currency: "CHF", | ||
refno: "B4B4B4B4B" | ||
} | ||
|
||
@successful_response_settle = {} # Empty hash as 204 No Content is expected for successful settlement | ||
|
||
@failed_response_settle = { | ||
"error" => { | ||
"code" => "INVALID_REQUEST", | ||
"message" => "Invalid transaction ID or parameters" | ||
} | ||
} | ||
end | ||
|
||
context "successful response" do | ||
before do | ||
allow_any_instance_of(Datatrans::JSON::Transaction::Settle).to receive(:process).and_return(@successful_response_settle) | ||
end | ||
|
||
it "handles a successful settle request" do | ||
transaction = Datatrans::JSON::Transaction.new(@datatrans, @valid_params_settle) | ||
expect(transaction.settle).to be true | ||
end | ||
end | ||
|
||
context "failed response" do | ||
before do | ||
allow_any_instance_of(Datatrans::JSON::Transaction::Settle).to receive(:process).and_return(@failed_response_settle) | ||
end | ||
|
||
it "handles a failed settle request" do | ||
transaction = Datatrans::JSON::Transaction.new(@datatrans, @valid_params_settle) | ||
expect(transaction.settle).to be false | ||
expect(transaction.response.error_code).to eq "INVALID_REQUEST" | ||
expect(transaction.response.error_message).to eq "Invalid transaction ID or parameters" | ||
end | ||
end | ||
end |