From 41acea77cb9540847e1995194c0745e4ea621d8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mili=C4=87?= Date: Tue, 9 Jul 2024 15:16:18 +0200 Subject: [PATCH] feature/Add SEPA payment at OBP Flow --- .../auth/controller/OtherController.java | 50 +++++++++ .../PostJsonCreateTransactionRequestSepa.java | 97 +++++++++++++++++ .../static/js/main-html-obp-consent-flow.js | 102 +++++++++++++++++- 3 files changed, 247 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/openbankproject/model/PostJsonCreateTransactionRequestSepa.java diff --git a/src/main/java/com/openbankproject/hydra/auth/controller/OtherController.java b/src/main/java/com/openbankproject/hydra/auth/controller/OtherController.java index 92b81d9..1da00f7 100644 --- a/src/main/java/com/openbankproject/hydra/auth/controller/OtherController.java +++ b/src/main/java/com/openbankproject/hydra/auth/controller/OtherController.java @@ -87,6 +87,9 @@ public class OtherController { @Value("${obp.base_url}/obp/v5.1.0/banks/BANK_ID/accounts/ACCOUNT_ID/VIEW_ID/transaction-request-types/COUNTERPARTY/transaction-requests") private String makePaymentCouterpartyObp; + + @Value("${obp.base_url}/obp/v5.1.0/banks/BANK_ID/accounts/ACCOUNT_ID/VIEW_ID/transaction-request-types/SEPA/transaction-requests") + private String makePaymentSepaObp; @Resource private RestTemplate restTemplate; @@ -294,6 +297,52 @@ public ResponseEntity makePaymentObp(@PathVariable String bankId, .replace("VIEW_ID", viewId) .replace("BANK_ID", bankId); + // Create response headers + HttpHeaders responseHeaders = new HttpHeaders(); + responseHeaders.add("Path-Of-Call", HttpMethod.POST + ": " + url); + try { + ResponseEntity response = restTemplate.exchange( + url, + HttpMethod.POST, + request, + HashMap.class + ); + return new ResponseEntity<>(response.getBody(), responseHeaders, response.getStatusCode()); + } catch (HttpClientErrorException e) { + return new ResponseEntity<>(e.getResponseBodyAsString(), responseHeaders, e.getStatusCode()); + } + } + @GetMapping("/payment_obp_sepa/{bankId}/{accountId}/{viewId}/{iban}/{currency}/{amount}/{description}/{chargePolicy}/{futureDate}") + public ResponseEntity makePaymentSepaObp(@PathVariable String bankId, + @PathVariable String accountId, + @PathVariable String viewId, + @PathVariable String iban, + @PathVariable String currency, + @PathVariable String amount, + @PathVariable String description, + @PathVariable String chargePolicy, + @PathVariable String futureDate, + HttpSession session) { + String consentId = SessionData.getConsentId(session); + HttpHeaders headers = new HttpHeaders(); + headers.add("Consent-Id", consentId); + HttpEntity entity = new HttpEntity<>(headers); + + PostJsonCreateTransactionRequestSepa body = new PostJsonCreateTransactionRequestSepa( + iban, + currency, + amount, + description, + chargePolicy, + futureDate + ); + + HttpEntity request = new HttpEntity<>(body, headers); + String url = makePaymentSepaObp + .replace("ACCOUNT_ID", accountId) + .replace("VIEW_ID", viewId) + .replace("BANK_ID", bankId); + // Create response headers HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.add("Path-Of-Call", HttpMethod.POST + ": " + url); @@ -309,6 +358,7 @@ public ResponseEntity makePaymentObp(@PathVariable String bankId, return new ResponseEntity<>(e.getResponseBodyAsString(), responseHeaders, e.getStatusCode()); } } + @GetMapping("/revoke_consent_obp") public Object revokeConsentObp(HttpSession session) { String consentId = SessionData.getConsentId(session); diff --git a/src/main/java/com/openbankproject/model/PostJsonCreateTransactionRequestSepa.java b/src/main/java/com/openbankproject/model/PostJsonCreateTransactionRequestSepa.java new file mode 100644 index 0000000..27b40eb --- /dev/null +++ b/src/main/java/com/openbankproject/model/PostJsonCreateTransactionRequestSepa.java @@ -0,0 +1,97 @@ +package com.openbankproject.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +class Iban { + @JsonProperty("iban") + private String iban; + + public String getIban() { + return iban; + } + + public void setIban(String iban) { + this.iban = iban; + } +} + + +public class PostJsonCreateTransactionRequestSepa { + @JsonProperty("to") + private Iban to; + + @JsonProperty("value") + private Value value; + + @JsonProperty("description") + private String description; + + @JsonProperty("charge_policy") + private String chargePolicy; + + @JsonProperty("future_date") + private String futureDate; + + public PostJsonCreateTransactionRequestSepa(String iban, + String currency, + String amount, + String description, + String chargePolicy, + String futureDate) { + Iban to = new Iban(); + to.setIban(iban); + this.to = to; + + Value value = new Value(); + value.setCurrency(currency); + value.setAmount(amount); + this.value = value; + + this.description = description; + this.chargePolicy = chargePolicy; + this.futureDate = futureDate; + + + } + + public Iban getTo() { + return to; + } + + + public void setTo(Iban to) { + this.to = to; + } + + public Value getValue() { + return value; + } + + public void setValue(Value value) { + this.value = value; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getChargePolicy() { + return chargePolicy; + } + + public void setChargePolicy(String chargePolicy) { + this.chargePolicy = chargePolicy; + } + + public String getFutureDate() { + return futureDate; + } + + public void setFutureDate(String futureDate) { + this.futureDate = futureDate; + } +} diff --git a/src/main/resources/static/js/main-html-obp-consent-flow.js b/src/main/resources/static/js/main-html-obp-consent-flow.js index 232ebc5..a4126dd 100644 --- a/src/main/resources/static/js/main-html-obp-consent-flow.js +++ b/src/main/resources/static/js/main-html-obp-consent-flow.js @@ -1,4 +1,4 @@ -function createTransactioRequestObp(button) { +function createTransactionRequestObp(button) { let resultBox = $(button).siblings('.payments_obp').empty().append('

Response:

'); let bankId = $(button).attr('bank_id'); let accountId = $(button).attr('account_id'); @@ -68,6 +68,77 @@ function createTransactioRequestObp(button) { }; +function createTransactionRequestObpSepa(button) { + let resultBox = $(button).siblings('.payments_obp_sepa').empty().append('

Response:

'); + let bankId = $(button).attr('bank_id'); + let accountId = $(button).attr('account_id'); + const viewHtmlId = "views-" + accountId; + const selectedViewId = $('#' + viewHtmlId).find(":selected").text(); + let iban = document.getElementById("creditor_iban_obp_sepa_" + accountId).value; + let amount = document.getElementById("obp_payment_amount_of_money_sepa_" + accountId).value; + let currency = document.getElementById("obp_payment_currency_sepa_" + accountId).value; + let description = document.getElementById("obp_payment_description_sepa_" + accountId).value; + + // The data to be sent to the server + var data = { + to: { + iban: iban + }, + value: { + currency: currency, + amount: amount + }, + description: description, + charge_policy: "SHARED", + future_date: "20200127" + }; + + // URL to which the request is sent + var url = '/payment_obp_sepa/' + bankId + '/' + accountId + "/" + selectedViewId + "/" + + data.to.iban + "/" + + data.value.currency + "/" + data.value.amount + "/" + + data.description + "/" + + data.charge_policy + "/" + + data.future_date; + + function setResult(dataToSend) { + let zson = JSON.stringify(dataToSend, null, 2); + let iconId = "result_copy_icon_" + accountId + button.id; + let resultBoxId = "result_box_" + accountId + button.id; + resultBox.append(`
${zson}
`).append('
'); + } + function setPathOfCall(path) { + document.getElementById("path-of-endpoint-sepa-" + accountId).textContent = path; + } + + // Sending the GET request + $.ajax({ + url: url, + method: 'GET', + dataType: 'json', + success: function(receivedData, textStatus, jqXHR) { + // Handle the response data here + console.log(receivedData); + setResult(receivedData); + // Access response headers + const customHeader = jqXHR.getResponseHeader('Path-Of-Call'); + setPathOfCall(customHeader); + console.log('Path-Of-Call:', customHeader); + }, + error: function(jqXHR, textStatus, errorThrown) { + console.log('Request Failed:', textStatus, errorThrown); + setResult(jqXHR.responseJSON); + + // Access response headers + const customHeader = jqXHR.getResponseHeader('Path-Of-Call'); + setPathOfCall(customHeader); + console.log('Path-Of-Call:', customHeader); + } + }); + +}; + + function getAccountDetails(button) { let resultBox = $(button).siblings('.account_detail_obp').empty().append('

Account Detail:

'); let accountId = $(button).attr('account_id'); @@ -151,11 +222,13 @@ $(function () { +
+ + + +