Skip to content

Commit

Permalink
Merge pull request #48 from visini/support-manual-settlements
Browse files Browse the repository at this point in the history
Add support for manual settlements
  • Loading branch information
andyundso authored Jan 3, 2024
2 parents 5921834 + f1de0c6 commit b828d9e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ Once you have a transaction id, you can start a transaction. Users of your appli
render js: "window.location='#{path}'"
```

You do not have to [settle a transaction](https://api-reference.datatrans.ch/#tag/v1transactions/operation/settle) by yourself: we set `"autoSettle": true` by default when authorizing a transaction, which means the transaction will be settled automatically.
You do not have to [settle a transaction](https://api-reference.datatrans.ch/#tag/v1transactions/operation/settle) by yourself: we set `"autoSettle": true` by default when authorizing a transaction, which means the transaction will be settled automatically. This can be overridden by setting `auto_settle: false` when authorizing a transaction.

Transaction status
------------------
Expand Down
4 changes: 3 additions & 1 deletion lib/datatrans/json/transaction/authorize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ def process
end

def request_body
auto_settle = params[:auto_settle].nil? ? true : params[:auto_settle]

{
"currency": params[:currency],
"refno": params[:refno],
"amount": params[:amount],
"autoSettle": true,
"autoSettle": auto_settle,
"paymentMethods": params[:payment_methods],
"redirect": {
"successUrl": params[:success_url],
Expand Down
37 changes: 24 additions & 13 deletions spec/json/authorize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@
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,
Expand All @@ -37,21 +50,9 @@
end

it "generates correct request_body" do
expected = {
"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"
}
}
request = Datatrans::JSON::Transaction::Authorize.new(@datatrans, @valid_params)

expect(request.request_body).to eq(expected)
expect(request.request_body).to eq(@expected_request_body)
end

it "#process handles a valid datatrans authorize response" do
Expand All @@ -60,6 +61,16 @@
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::Authorize.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::Authorize).to receive(:process).and_return(@failed_response)
Expand Down

0 comments on commit b828d9e

Please sign in to comment.