-
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.
Rename
::JSON::Authorize
to ::JSON::Init
- Loading branch information
Showing
7 changed files
with
179 additions
and
54 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 |
---|---|---|
@@ -1,54 +1,10 @@ | ||
require "httparty" | ||
require "datatrans/json/transaction/response" | ||
require_relative "init" | ||
|
||
class Datatrans::JSON::Transaction | ||
class Authorize | ||
# class to initialize a new transaction https://api-reference.datatrans.ch/#tag/v1transactions/operation/init | ||
attr_accessor :params, :datatrans | ||
|
||
class Authorize < Init | ||
def initialize(datatrans, params) | ||
@datatrans = datatrans | ||
@params = params | ||
end | ||
|
||
def post(url, options = {}) | ||
options = options | ||
.merge(datatrans.proxy) | ||
.merge(basic_auth: {username: datatrans.merchant_id, password: datatrans.password}) | ||
HTTParty.post(url, **options) | ||
end | ||
|
||
def process | ||
post(datatrans.url(:init_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] | ||
|
||
body = { | ||
currency: params[:currency], | ||
refno: params[:refno], | ||
amount: params[:amount], | ||
autoSettle: auto_settle, | ||
paymentMethods: params[:payment_methods], | ||
redirect: { | ||
successUrl: params[:success_url], | ||
cancelUrl: params[:cancel_url], | ||
errorUrl: params[:error_url] | ||
} | ||
} | ||
|
||
body["option"] = params[:option] if params[:option].present? | ||
|
||
body | ||
end | ||
end | ||
|
||
class AuthorizeResponse < Response | ||
def successful? | ||
params["error"].blank? && params["transactionId"].present? | ||
warn "[DEPRECATION] `Datatrans::JSON::Transaction::Authorize` is deprecated. Please use `Datatrans::JSON::Transaction::Init` instead." | ||
super(datatrans, params) | ||
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,61 @@ | ||
require "httparty" | ||
require "datatrans/json/transaction/response" | ||
|
||
class Datatrans::JSON::Transaction | ||
class Init | ||
# class to initialize a new transaction https://api-reference.datatrans.ch/#tag/v1transactions/operation/init | ||
attr_accessor :params, :datatrans | ||
|
||
def initialize(datatrans, params) | ||
@datatrans = datatrans | ||
@params = params | ||
end | ||
|
||
def post(url, options = {}) | ||
options = options | ||
.merge(datatrans.proxy) | ||
.merge(basic_auth: {username: datatrans.merchant_id, password: datatrans.password}) | ||
HTTParty.post(url, **options) | ||
end | ||
|
||
def process | ||
post(datatrans.url(:init_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] | ||
|
||
body = { | ||
currency: params[:currency], | ||
refno: params[:refno], | ||
amount: params[:amount], | ||
autoSettle: auto_settle, | ||
paymentMethods: params[:payment_methods], | ||
redirect: { | ||
successUrl: params[:success_url], | ||
cancelUrl: params[:cancel_url], | ||
errorUrl: params[:error_url] | ||
} | ||
} | ||
|
||
body["option"] = params[:option] if params[:option].present? | ||
|
||
body | ||
end | ||
end | ||
|
||
class InitResponse < Response | ||
def successful? | ||
params["error"].blank? && params["transactionId"].present? | ||
end | ||
end | ||
|
||
class AuthorizeResponse < InitResponse | ||
def initialize(datatrans, params) | ||
warn "[DEPRECATION] `Datatrans::JSON::Transaction::AuthorizeResponse` is deprecated. Please use `Datatrans::JSON::Transaction::InitResponse` instead." | ||
super(datatrans, params) | ||
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,100 @@ | ||
require "spec_helper" | ||
|
||
describe Datatrans::JSON::Transaction::Init do | ||
before do | ||
@successful_response = { | ||
"transactionId" => "230223022302230223" | ||
} | ||
|
||
@failed_response = { | ||
"error" => { | ||
"code" => "INVALID_PROPERTY", | ||
"message" => "init.refno must not be null" | ||
} | ||
} | ||
|
||
@valid_params = { | ||
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" | ||
} | ||
|
||
@expected_request_body = { | ||
currency: "CHF", | ||
refno: "B4B4B4B4B", | ||
amount: 1337, | ||
autoSettle: true, | ||
paymentMethods: ["ECA", "VIS"], | ||
redirect: { | ||
successUrl: "https://pay.sandbox.datatrans.com/upp/merchant/successPage.jsp", | ||
cancelUrl: "https://pay.sandbox.datatrans.com/upp/merchant/cancelPage.jsp", | ||
errorUrl: "https://pay.sandbox.datatrans.com/upp/merchant/errorPage.jsp" | ||
} | ||
} | ||
|
||
@invalid_params = { | ||
currency: "CHF", | ||
refno: nil, | ||
amount: 1337, | ||
payment_methods: ["ECA", "VIS"] | ||
} | ||
end | ||
|
||
context "successful response" do | ||
before do | ||
allow_any_instance_of(Datatrans::JSON::Transaction::Init).to receive(:process).and_return(@successful_response) | ||
end | ||
|
||
it "generates correct request_body" do | ||
request = Datatrans::JSON::Transaction::Init.new(@datatrans, @valid_params) | ||
|
||
expect(request.request_body).to eq(@expected_request_body) | ||
end | ||
|
||
it "#process handles a valid datatrans authorize response" do | ||
@transaction = Datatrans::JSON::Transaction.new(@datatrans, @valid_params) | ||
expect(@transaction.init).to be true | ||
end | ||
end | ||
|
||
context "with autoSettle specified" do | ||
it "uses autoSettle=false in request_body" do | ||
params_with_auto_settle = @valid_params.merge(auto_settle: false) | ||
request = Datatrans::JSON::Transaction::Init.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 "with option specified" do | ||
it "uses option in request_body" do | ||
params_with_option = @valid_params.merge(option: {createAlias: true}) | ||
request = Datatrans::JSON::Transaction::Init.new(@datatrans, params_with_option) | ||
|
||
expected_request_body_with_option = @expected_request_body.merge("option" => {createAlias: true}) | ||
expect(request.request_body).to eq(expected_request_body_with_option) | ||
end | ||
end | ||
|
||
context "failed response" do | ||
before do | ||
allow_any_instance_of(Datatrans::JSON::Transaction::Init).to receive(:process).and_return(@failed_response) | ||
@transaction = Datatrans::JSON::Transaction.new(@datatrans, @invalid_params) | ||
end | ||
|
||
it "#process handles a failed datatrans authorize response" do | ||
expect(@transaction.init).to be false | ||
end | ||
|
||
it "returns error details" do | ||
@transaction.init | ||
expect(@transaction.response.error_code).to eq "INVALID_PROPERTY" | ||
expect(@transaction.response.error_message).to eq "init.refno must not be null" | ||
end | ||
end | ||
end |
File renamed without changes.
File renamed without changes.