diff --git a/README.markdown b/README.markdown index d8ca91c..243b67b 100644 --- a/README.markdown +++ b/README.markdown @@ -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 ------------------ diff --git a/lib/datatrans/json/transaction/authorize.rb b/lib/datatrans/json/transaction/authorize.rb index d6c4ab0..a1c9bf9 100644 --- a/lib/datatrans/json/transaction/authorize.rb +++ b/lib/datatrans/json/transaction/authorize.rb @@ -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], diff --git a/spec/json/authorize_spec.rb b/spec/json/authorize_spec.rb index 6f3bfce..508379a 100644 --- a/spec/json/authorize_spec.rb +++ b/spec/json/authorize_spec.rb @@ -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, @@ -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 @@ -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)