Skip to content

Commit

Permalink
Rename ::JSON::Authorize to ::JSON::Init
Browse files Browse the repository at this point in the history
  • Loading branch information
andyundso committed Jan 19, 2024
1 parent fa0cfa6 commit ee6dc82
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 54 deletions.
12 changes: 6 additions & 6 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,16 @@ Saving Payment Information
According to the [docs](https://docs.datatrans.ch/docs/customer-initiated-payments#saving-payment-information), there are three possible flows:

- **Customer Initiated Payments**: _Your customer pays and nothing is registered._
- This is the most basic setup and does _not_ save any payment information: First, call `transaction.authorize`, and then redirect the user to the `transaction_path` (see the sections `Authorize` and `Start a transaction` below).
- This is the most basic setup and does _not_ save any payment information: First, call `transaction.init`, and then redirect the user to the `transaction_path` (see the sections `Initialize` and `Start a transaction` below).
- **Customer Initiated Payment** and creating an `alias` for subsequent **Merchant Initiated Payments**: _Your customer pays and the card or payment method information is registered. You receive an alias which you save for later merchant initiated payments or one-click checkouts._
- In order to save payment information after your customer has finalized their payment, without them having to re-enter their payment information and go through the 3D-Secure flow, pass `option: {"createAlias": true}`. More information can be found [here](https://docs.datatrans.ch/docs/redirect-lightbox#saving-payment-information).
- **Merchant Initiated Payments**: _Your customer registers their card or payment method information without any payment. Their account is not charged. This is what we call a dedicated registration._
- This setup allows you to save a customers payment information without any charge in the beginning. This is useful in the context of setting up a subscription model (e.g., usage-based billing at the end of a billing period). See the section `Merchant Initiated Payments` below.

Authorize
Initialize
---------

Authorize JSON transaction:
Initialize a JSON transaction:

```ruby
transaction = datatrans.json_transaction(
Expand All @@ -132,9 +132,9 @@ transaction = datatrans.json_transaction(
error_url: <your_application_return_url>
)

# call to init endpoint to initialize a transaction
# call to initialize endpoint to initialize a transaction
# returns true or false depending if response was successful or not
init = transaction.authorize
init = transaction.init

# successful authorization call returns in response a transaction id
if init
Expand Down Expand Up @@ -230,7 +230,7 @@ transaction = datatrans.json_transaction(
error_url: <your_application_return_url>
)

init = transaction.authorize
init = transaction.init

# successful authorization call returns in response a transaction id
if init
Expand Down
8 changes: 8 additions & 0 deletions lib/datatrans/json/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ def initialize(datatrans, params)
end

def authorize
warn "[DEPRECATION] `Datatrans::JSON::Transaction#authorize` is deprecated. Please use `Datatrans::JSON::Transaction#init` instead."

self.request = Authorize.new(datatrans, params)
@response = AuthorizeResponse.new(datatrans, request.process)
@response.successful?
end

def init
self.request = Init.new(datatrans, params)
@response = InitResponse.new(datatrans, request.process)
@response.successful?
end

def merchant_authorize
self.request = MerchantAuthorize.new(datatrans, params)
@response = MerchantAuthorizeResponse.new(datatrans, request.process)
Expand Down
52 changes: 4 additions & 48 deletions lib/datatrans/json/transaction/authorize.rb
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
61 changes: 61 additions & 0 deletions lib/datatrans/json/transaction/init.rb
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
100 changes: 100 additions & 0 deletions spec/json/init_spec.rb
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.

0 comments on commit ee6dc82

Please sign in to comment.