Code Samples


[HttpPost]
public async Task<IActionResult> Post(
	string grant_type,
	string client_id,
	string client_secret,
	string? scope)
{
	string iam_url = "https://iamapi.sb.ecospend.com";

	var client = _httpClientFactory.CreateClient();
	client.DefaultRequestHeaders.Clear();
	client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
	client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");


	var parameters = new List<KeyValuePair<string, string>>();
	parameters.Add(new KeyValuePair<string, string>("grant_type", grant_type));
	parameters.Add(new KeyValuePair<string, string>("client_id", client_id));
	parameters.Add(new KeyValuePair<string, string>("client_secret", client_secret));
	if (scope != null)
		parameters.Add(new KeyValuePair<string, string>("scope", scope));


	var uri = new UriBuilder(iam_url) { Path = "/connect/token" }.Uri;
	using (var msg = new HttpRequestMessage(HttpMethod.Post, uri) { Content = new FormUrlEncodedContent(parameters) })
	{
		msg.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
		using (var response = await client.SendAsync(msg).ConfigureAwait(false))
		{
			String body = response.Content.ReadAsStringAsync().Result;
			if (response.IsSuccessStatusCode)
			{
				return Ok(body);
			}
			else
			{
				return Problem(statusCode: (int)response.StatusCode, detail: $"{response.ReasonPhrase} -> {body}");
			}
		}
	}
}
						

package com.ecospend.java.authentication;
import com.ecospend.java.authentication.models.AuthenticationResponse;
import com.ecospend.java.configurations.ApplicationProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.Collections;
import java.util.Optional;
@Service
@Slf4j
@FieldDefaults(level = AccessLevel.PRIVATE)
@RequiredArgsConstructor
public class AuthenticationService {
    final RestTemplate restTemplate;
    final ObjectMapper objectMapper;
    final ApplicationProperties applicationProperties;
    public ResponseEntity getToken(
            String grant_type,
            String client_id,
            String client_secret,
            Optional<String> scope) throws JsonProcessingException {
        String iam_url = "https://iamapi.sb.ecospend.com";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
        params.add("grant_type", grant_type);
        params.add("client_id", client_id);
        params.add("client_secret", client_secret);
        if (scope.isPresent()) {
            params.add("scope", scope.get());
        }
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(params, headers);
        ResponseEntity<String> response = restTemplate.exchange(iam_url + "/connect/token", HttpMethod.POST, request, String.class);
        if (response.getStatusCode() == HttpStatus.OK) {
            String body = response.getBody();
            AuthenticationResponse token = objectMapper.readValue(body, AuthenticationResponse.class);
            return ResponseEntity.ok(token);
        } else {
            return ResponseEntity.status(response.getStatusCode()).body("");
        }
    }
}

const superagent = require("superagent");
const logger = require("superagent-logger");
const connectTokenAsync = (
  client_id,
  client_secret,
  scope,
  grant_type
) => {
  return new Promise((resolve, reject) => {
    superagent
      .post("https://iamapi.sb.ecospend.com/connect/token")
      .use(logger({ outgoing: true, timestamp: true }))
      .send({
        client_id: client_id,
        client_secret: client_secret,
        scope: scope,
        grant_type: grant_type,
      }) // sends a JSON post body
      .set("Content-Type", "application/x-www-form-urlencoded")
      .set("accept", "json")
      .end((err, res) => {
        if (err) {
          if (err.response)
            return reject({
              status: err.response.status,
              text: err.response.text,
            });
          else
            return reject({
              errno: err.errno,
              code: err.code,
            });
        }
        return resolve(res._body);
      });
  });
};
module.exports = {
  connectTokenAsync: connectTokenAsync
};

import requests
import urllib.parse
import json
from typing import Any
from fastapi import APIRouter, HTTPException
from api.models.response_models.authentication_response import AuthenticationResponse
router = APIRouter()
@router.post("/", response_model=AuthenticationResponse)
def authenticate(
    grant_type : str,
    client_id : str,
    client_secret : str,
    scope : str = None
) -> Any:
    """
    Calls authentication API with given credential information.
    """
    authentication_request = {
        "grant_type" : grant_type,
        "client_id" :client_id,
        "client_secret" :client_secret
    }
    if scope is not None:
        authentication_request["scope"] = scope
    response = requests.post('https://iamapi.sb.ecospend.com/connect/token', data = urllib.parse.urlencode(authentication_request), headers={"Content-Type":"application/x-www-form-urlencoded"})
    if response.status_code == 200:
        return json.loads(response.text)
    else:
        raise HTTPException(status_code=response.status_code, detail= f'{response.reason} -> {response.text}')


[HttpPost]
public async Task<IActionResult> CreatePayment(
    [FromHeader] string authorization,
    string bankId,
    float amount,
    string reference,
    string redirectUrl,
    string creditorAccountIdentification,
    string creditorAccountType,
    string creditorAccountCurrency,
    string creditorAccountOwnerName
)
{
    string pis_url = "https://pisapi.sb.ecospend.com";
    var client = _httpClientFactory.CreateClient();
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authorization);
    var creditorAccount = new CreditorAccount(creditorAccountType, creditorAccountIdentification,
        creditorAccountOwnerName,
        creditorAccountCurrency);
    var paymentRequest = new PaymentRequest(redirectUrl, amount, reference, redirectUrl, creditorAccount);
    string json = JsonConvert.SerializeObject(paymentRequest);
    var content = new StringContent(json, Encoding.UTF8, "application/json");
    var uri = new UriBuilder(pis_url) { Path = "/api/v2/payments/" }.Uri;
    using (var msg = new HttpRequestMessage(HttpMethod.Post, uri)
           {
               Content = content
           })
    {
        msg.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        using (var response = await client.SendAsync(msg).ConfigureAwait(false))
        {
            String body = response.Content.ReadAsStringAsync().Result;
            if (response.IsSuccessStatusCode)
            {
                return Ok(body);
            }
            else
            {
                return Problem(statusCode: (int)response.StatusCode, detail: $"{response.ReasonPhrase} -> {body}");
            }
        }
    }
}
                        

package com.ecospend.java.payment;
import com.ecospend.java.configurations.ApplicationProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
@Slf4j
@FieldDefaults(level = AccessLevel.PRIVATE)
@RequiredArgsConstructor
public class PaymentService {
    final RestTemplate restTemplate;
    final ObjectMapper objectMapper;
    final ApplicationProperties applicationProperties;
    public ResponseEntity createPayment(String authorization,
                                        String bankId,
                                        float amount,
                                        String reference,
                                        String redirectUrl,
                                        String creditorAccountIdentification,
                                        String creditorAccountType,
                                        String creditorAccountCurrency,
                                        String creditorAccountOwnerName) throws JsonProcessingException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setBearerAuth(authorization);
        // create a JSON object
        ObjectNode paymentRequest = objectMapper.createObjectNode();
        paymentRequest.put("bank_id", bankId);
        paymentRequest.put("amount", amount);
        paymentRequest.put("reference", reference);
        paymentRequest.put("redirect_url", redirectUrl);
        ObjectNode creditorAccount = objectMapper.createObjectNode();
        creditorAccount.put("identification", creditorAccountIdentification);
        creditorAccount.put("type", creditorAccountType);
        creditorAccount.put("owner_name", creditorAccountOwnerName);
        creditorAccount.put("currency", creditorAccountCurrency);
        paymentRequest.put("creditor_account", creditorAccount);
        String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(paymentRequest);
        HttpEntity<String> request =
                new HttpEntity<String>(json, headers);
        ResponseEntity<String> response = restTemplate.exchange(applicationProperties.getPISUrl() + "/api/v2/payments/", HttpMethod.POST, request, String.class);
        if (response.getStatusCode() == HttpStatus.OK) {
            String body = response.getBody();
            return ResponseEntity.ok(body);
        } else {
            return ResponseEntity.status(response.getStatusCode()).body("");
        }
    }
}
                        
    
const superagent = require("superagent");
const logger = require("superagent-logger");
const createPaymentAsync = (
  authorization,
  bankId,
  amount,
  reference,
  redirectUrl,
  creditorAccountIdentification,
  creditorAccountType,
  creditorAccountCurrency,
  creditorAccountOwnerName
) => {
  return new Promise((resolve, reject) => {
    const url = "https://pisapi.sb.ecospend.com/api/v2/payments";
    superagent
      .post(url)
      .use(logger)
      .send({
        bank_id: bankId,
        creditor_account: {
          identification: creditorAccountIdentification,
          type: creditorAccountType,
          currency: creditorAccountCurrency,
          owner_name: creditorAccountOwnerName,
        },
        amount: amount,
        reference: reference,
        redirect_url: redirectUrl,
      }) // sends a JSON post body
      .set("Content-Type", "application/json")
      .set("Authorization", authorization)
      .set("accept", "json")
      .end((err, res) => {
        if (err) {
          if (err.response)
            return reject({
              status: err.response.status,
              text: err.response.text,
            });
          else
            return reject({
              errno: err.errno,
              code: err.code,
            });
        }
        return resolve(res._body);
      });
  });
};
module.exports = {
  createPaymentAsync,
};
						

import jsonpickle
import requests
import json
from typing import Any
from fastapi import APIRouter, HTTPException, Header
from api.models.response_models.payment_post_response import PaymentPostResponse
router = APIRouter()
@router.post("/", response_model=PaymentPostResponse)
def payment(
    bank_id : str,
    amount : float,
    reference : str,
    redirect_url : str,
    creditor_account_identification : str,
    creditor_account_type : str,
    creditor_account_currency : str,
    creditor_account_owner_name : str,
    access_token: str = Header(None)
) -> Any:
    """
    Creates a basic payment with given creditor account information from the given bank
    """
    paylink_request = {
        "bank_id" : bank_id,
        "redirect_url" : redirect_url,
        "amount" :amount,
        "reference" :reference,
        "creditor_account" : {
            "type" :creditor_account_type,
            "identification" :creditor_account_identification,
            "owner_name" :creditor_account_owner_name,
            "currency" :creditor_account_currency
        }
    }
    response = requests.post('https://pisapi.sb.ecospend.com/api/v2/payments/', data = jsonpickle.dumps(paylink_request), headers={"Content-Type":"application/json", "Authorization":f"Bearer {access_token}"})
    if response.status_code == 200:
        return json.loads(response.text)
    else:
        raise HTTPException(status_code=response.status_code, detail= f'{response.reason} -> {response.text}')
						


using System.Net.Http.Headers;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace EcopendWebApplication.Controllers;
[ApiController]
[Route("[controller]")]
public class PaylinkController : ControllerBase
{
    private readonly IHttpClientFactory _httpClientFactory;
    public PaylinkController(IHttpClientFactory httpClientFactory)
    {
        _httpClientFactory = httpClientFactory;
    }
    record PaylinkRequest(string redirect_url,float amount,string reference,Account creditor_account);
    record Account(string type,string identification,string name,string currency );
    [HttpPost]
    public async Task<IActionResult> CreatePaylink(
        string redirectUrl,
        float amount,
        string reference,
        string creditorAccountType,
        string creditorAccountIdentification,
         string creditorAccountName,
        string creditorAccountCurrency,
        string accessToken
        )
    {
        string pis_url = "https://pisapi.sb.ecospend.com";
        var client = _httpClientFactory.CreateClient(); 
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
        /*
           Creates basic paylink with just required fields (these fields are mandatory required fields).\n\n
           For creating a paylink, you must provide the fields given below in minimum:\n
               * redirect_url\n
               * amount\n
               * reference\n
               * creditor account\n
        */
        var creditorAccount = new Account(creditorAccountType, creditorAccountIdentification,creditorAccountName,creditorAccountCurrency);
        var paylinkRequest = new  PaylinkRequest(redirectUrl,amount,reference,creditorAccount);
        string json = JsonConvert.SerializeObject(paylinkRequest);
        var content = new StringContent(json, Encoding.UTF8, "application/json");
        var uri = new UriBuilder(pis_url) { Path = "/api/v2/paylinks/" }.Uri;
        using (var msg = new HttpRequestMessage(HttpMethod.Post, uri)
               {
                   Content = content
               })
        {
            msg.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            using (var response = await client.SendAsync(msg).ConfigureAwait(false))
            {
                String body = response.Content.ReadAsStringAsync().Result;
                if (response.IsSuccessStatusCode)
                {
                    return Ok(body);
                }
                else
                {
                    return Problem(statusCode: (int)response.StatusCode, detail: $"{response.ReasonPhrase} -> {body}");
                }
            }
        }
    }
}

package com.ecospend.java.paylink;
import com.ecospend.java.configurations.ApplicationProperties;
import com.ecospend.java.paylink.models.PaylinkRequest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.NotImplementedException;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
@Slf4j
@FieldDefaults(level = AccessLevel.PRIVATE)
@RequiredArgsConstructor
public class PaylinkService {
    final RestTemplate restTemplate;
    final ObjectMapper objectMapper;
    final ApplicationProperties applicationProperties;
    public ResponseEntity createPaylink(String redirectUrl,
                                        float amount,
                                        String reference,
                                        String creditorAccountType,
                                        String creditorAccountIdentification,
                                        String creditorAccountName,
                                        String creditorAccountCurrency,
                                        String accessToken) throws JsonProcessingException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setBearerAuth(accessToken);
        /*
            Creates basic paylink with just required fields (these fields are mandatory required fields).\n\n
            For creating a paylink, you must provide the fields given below in minimum:\n
                * redirect_url\n
                * amount\n
                * reference\n
                * creditor account\n
         */
        // create a JSON object
        ObjectNode paylinkRequest = objectMapper.createObjectNode();
        paylinkRequest.put("redirect_url", redirectUrl);
        paylinkRequest.put("amount", amount);
        paylinkRequest.put("reference", reference);
        ObjectNode creditorAccount = objectMapper.createObjectNode();
        creditorAccount.put("type", creditorAccountType);
        creditorAccount.put("identification", creditorAccountIdentification);
        creditorAccount.put("name", creditorAccountName);
        creditorAccount.put("currency", creditorAccountCurrency);
        paylinkRequest.put("creditor_account", creditorAccount);
        String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(paylinkRequest);
        HttpEntity request =
                new HttpEntity(json, headers);
        ResponseEntity response = restTemplate.exchange(applicationProperties.getPISUrl() + "/api/v2/paylinks/", HttpMethod.POST, request, String.class);
        if (response.getStatusCode() == HttpStatus.OK) {
            String body = response.getBody();
            return ResponseEntity.ok(body);
        } else {
            return ResponseEntity.status(response.getStatusCode()).body("");
        }
    }
}
}

const superagent = require("superagent");
const logger = require("superagent-logger");
const createPaylinkAsync = (paylinkRequest, authorization) => {
  return new Promise((resolve, reject) => {
    superagent
      .post(process.env.PIS_URL + "/api/v2/paylinks")
      .use(logger)
      .send({
        redirect_url: paylinkRequest.redirect_url,
        amount: paylinkRequest.amount,
        reference: paylinkRequest.reference,
        creditor_account: {
          type: paylinkRequest.creditor_account_type,
          identification: paylinkRequest.creditor_account_identification,
          name: paylinkRequest.creditor_account_name,
          currency: paylinkRequest.creditor_account_currency,
        },
      }) // sends a JSON post body
      .set("Content-Type", "application/json")
      .set("Authorization", authorization)
      .set("accept", "json")
      .end((err, res) => {
        if (err) {
          if (err.response)
            return reject({
              status: err.response.status,
              text: err.response.text,
            });
          else
            return reject({
              errno: err.errno,
              code: err.code,
            });
        }
        return resolve(res._body);
      });
  });
};

module.exports = {
  createPaylinkAsync,
};

import jsonpickle
import requests
import json
from typing import Any
from fastapi import APIRouter, HTTPException, Header
from api.models.response_models.paylink_post_response import PaylinkPostResponse
from api.models.request_models.paylink_post_request import PaylinkRequest
router = APIRouter()
@router.post("/", response_model=PaylinkPostResponse)
def paylink(
    redirect_url : str,
    amount : float,
    reference : str,
    creditor_account_type: str,
    creditor_account_identification: str,
    creditor_account_name: str,
    creditor_account_currency: str,
    access_token: str = Header(None)
) -> Any:
    """
    Creates basic paylink with just required fields (these fields are mandatory required fields).\n\n
    For creating a paylink, you must provide the fields given below in minimum:\n
        * redirect_url\n
        * amount\n
        * reference\n
        * creditor account\n
    """
    paylink_request = {
        "redirect_url" : redirect_url,
        "amount" :amount,
        "reference" :reference,
        "creditor_account" : {
            "type" :creditor_account_type,
            "identification" :creditor_account_identification,
            "name" :creditor_account_name,
            "currency" :creditor_account_currency
        }
    }
    response = requests.post('https://pisapi.sb.ecospend.com/api/v2/paylinks/', data = jsonpickle.dumps(paylink_request), headers={"Content-Type":"application/json", "Authorization":f"Bearer {access_token}"})
    if response.status_code == 200:
        return json.loads(response.text)
    else:
        raise HTTPException(status_code=response.status_code, detail= f'{response.reason} -> {response.text}')
@router.post("/with-debtor", response_model=PaylinkPostResponse)
def paylink_with_debtor(
    redirect_url : str,
    amount : float,
    reference : str,
    creditor_account_type: str,
    creditor_account_identification: str,
    creditor_account_name: str,
    creditor_account_currency: str,
    debtor_account_type: str,
    debtor_account_identification: str,
    debtor_account_name: str,
    debtor_account_currency: str,
    access_token: str = Header(None)
) -> Any: