diff options
author | Christian Grothoff <christian@grothoff.org> | 2021-10-21 15:00:38 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2021-10-21 15:00:38 +0200 |
commit | 99e7729ede69e218198a8c846e8096ad83127f6b (patch) | |
tree | 4a246f9021c79f6249609299cfb2e5ab34e39e16 /src | |
parent | 14c5d00ebd98fdb299fa16c9721588d55f8d3b35 (diff) |
start with /kyc support in libtalermerchant
Diffstat (limited to 'src')
-rw-r--r-- | src/include/taler_merchant_service.h | 181 | ||||
-rw-r--r-- | src/lib/Makefile.am | 1 | ||||
-rw-r--r-- | src/lib/merchant_api_get_kyc.c | 306 |
3 files changed, 486 insertions, 2 deletions
diff --git a/src/include/taler_merchant_service.h b/src/include/taler_merchant_service.h index cc3063f6..82e81b4b 100644 --- a/src/include/taler_merchant_service.h +++ b/src/include/taler_merchant_service.h @@ -3497,8 +3497,8 @@ TALER_MERCHANT_merchant_tip_get (struct GNUNET_CURL_Context *ctx, * @param tgh handle to the request to be canceled */ void -TALER_MERCHANT_merchant_tip_get_cancel (struct - TALER_MERCHANT_TipMerchantGetHandle *tgh); +TALER_MERCHANT_merchant_tip_get_cancel ( + struct TALER_MERCHANT_TipMerchantGetHandle *tgh); /** @@ -3734,4 +3734,181 @@ TALER_MERCHANT_tip_pickup2_cancel ( struct TALER_MERCHANT_TipPickup2Handle *tp); + +/* ********************* /kyc ************************** */ + +/** + * Handle for GETing the KYC status of instance(s). + */ +struct TALER_MERCHANT_KycGetHandle; + + +/** + * Information about KYC actions the merchant still must perform. + */ +struct TALER_MERCHANT_AccountKycRedirectDetail +{ + + /** + * URL that the user should open in a browser to + * proceed with the KYC process (as returned + * by the exchange's /kyc-check/ endpoint). + */ + const char *kyc_url; + + /** + * Base URL of the exchange this is about. + */ + const char *exchange_url; + + /** + * Our bank wire account this is about. + */ + const char *payto_uri; +}; + + +/** + * Information about KYC status failures at the exchange. + */ +struct TALER_MERCHANT_ExchangeKycFailureDetail +{ + /** + * Base URL of the exchange this is about. + */ + const char *exchange_url; + + /** + * Error code indicating errors the exchange + * returned, or #TALER_EC_INVALID for none. + */ + enum TALER_ErrorCode exchange_code; + + /** + * HTTP status code returned by the exchange when we asked for + * information about the KYC status. + * 0 if there was no response at all. + */ + unsigned int exchange_http_status; +}; + + +/** + * Details in a response to a GET /kyc request. + */ +struct TALER_MERCHANT_KycResponse +{ + struct TALER_MERCHANT_HttpResponse hr; + + /** + * Response details. + */ + union + { + /** + * Information returned if the status was #MHD_HTTP_ACCEPTED, + * #MHD_HTTP_BAD_GATEWAY or #MHD_HTTP_GATEWAY_TIMEOUT. + */ + struct + { + + /** + * Array with information about KYC actions the merchant still must perform. + */ + struct TALER_MERCHANT_AccountKycRedirectDetail *pending_kycs; + + /** + * Array with information about KYC failures at the exchange. + */ + struct TALER_MERCHANT_ExchangeKycFailureDetail *timeout_kycs; + + /** + * Length of the @e pending_kycs array. + */ + unsigned int pending_kycs_length; + + /** + * Length of the @e timeout_kycs array. + */ + unsigned int timeout_kycs_length; + } kyc_status; + + } details; + +}; + + +/** + * Callback to with a response from a GET [/private]/kyc request + * + * @param cls closure + * @param kr response details + */ +typedef void +(*TALER_MERCHANT_KycGetCallback) ( + void *cls, + const struct TALER_MERCHANT_KycResponse *kr); + + +/** + * Issue a GET /private/kycs/$KYC_ID (private variant) request to the backend. + * Returns KYC status of bank accounts. + * + * @param ctx execution context + * @param backend_url base URL of the merchant backend + * @param h_wire which bank account to query, NULL for all + * @param exchange_url which exchange to query, NULL for all + * @param timeout how long to wait for a (positive) reply + * @param cb function to call with the result + * @param cb_cls closure for @a cb + * @return handle for this operation, NULL upon errors + */ +struct TALER_MERCHANT_KycGetHandle * +TALER_MERCHANT_kyc_get (struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const struct GNUNET_HashCode *h_wire, + const char *exchange_url, + struct GNUNET_TIME_Relative timeout, + TALER_MERCHANT_KycGetCallback cb, + void *cb_cls); + + +/** + * Issue a GET /management/instances/$INSTANCE/kyc request to the backend. + * Returns KYC status of bank accounts. + * + * @param ctx execution context + * @param backend_url base URL of the merchant backend + * @param instance_id specific instance to query + * @param h_wire which bank account to query, NULL for all + * @param exchange_url which exchange to query, NULL for all + * @param timeout how long to wait for a (positive) reply + * @param cb function to call with the result + * @param cb_cls closure for @a cb + * @return handle for this operation, NULL upon errors + */ +struct TALER_MERCHANT_KycGetHandle * +TALER_MERCHANT_management_kyc_get (struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const char *instance_id, + const struct GNUNET_HashCode *h_wire, + const char *exchange_url, + struct GNUNET_TIME_Relative timeout, + TALER_MERCHANT_KycGetCallback cb, + void *cb_cls); + + +/** + * Cancel a GET [/private]/kyc/$KYC_ID request. + * + * @param kyc handle to the request to be canceled + */ +void +TALER_MERCHANT_kyc_get_cancel ( + struct TALER_MERCHANT_KycGetHandle *kyc); + + + + + #endif /* _TALER_MERCHANT_SERVICE_H */ diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 52d6dd84..8b906e16 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -23,6 +23,7 @@ libtalermerchant_la_SOURCES = \ merchant_api_get_config.c \ merchant_api_get_instance.c \ merchant_api_get_instances.c \ + merchant_api_get_kyc.c \ merchant_api_get_orders.c \ merchant_api_get_product.c \ merchant_api_get_products.c \ diff --git a/src/lib/merchant_api_get_kyc.c b/src/lib/merchant_api_get_kyc.c new file mode 100644 index 00000000..816b3cc0 --- /dev/null +++ b/src/lib/merchant_api_get_kyc.c @@ -0,0 +1,306 @@ +/* + This file is part of TALER + Copyright (C) 2021 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU Lesser General Public License as published by the Free Software + Foundation; either version 2.1, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License along with + TALER; see the file COPYING.LGPL. If not, see + <http://www.gnu.org/licenses/> +*/ +/** + * @file merchant_api_get_kyc.c + * @brief Implementation of the GET /kyc request of the merchant's HTTP API + * @author Christian Grothoff + */ +#include "platform.h" +#include <curl/curl.h> +#include <jansson.h> +#include <microhttpd.h> /* just for HTTP status codes */ +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_curl_lib.h> +#include "taler_merchant_service.h" +#include <taler/taler_json_lib.h> +#include <taler/taler_signatures.h> + + +/** + * Handle for a GET /kyc operation. + */ +struct TALER_MERCHANT_KycGetHandle +{ + /** + * The url for this request. + */ + char *url; + + /** + * Handle for the request. + */ + struct GNUNET_CURL_Job *job; + + /** + * Function to call with the result. + */ + TALER_MERCHANT_KycGetCallback cb; + + /** + * Closure for @a cb. + */ + void *cb_cls; + + /** + * Reference to the execution context. + */ + struct GNUNET_CURL_Context *ctx; + +}; + + +/** + * Parse @a kyc response and call the continuation on success. + * + * @param kyc operation handle + * @param[in,out] kr response details + * @param pends pending_kycs array from the reply + * @param touts timeout_kycs array from the reply + * @return #GNUNET_OK on success (callback was called) + */ +static enum GNUNET_GenericReturnValue +parse_kyc (struct TALER_MERCHANT_KycGetHandle *kyc, + struct TALER_MERCHANT_KycResponse *kr, + json_t *pends, + json_t *touts) +{ + // FIXME... + GNUNET_break (0); + return GNUNET_SYSERR; +} + + +/** + * Function called when we're done processing the + * HTTP /kyc request. + * + * @param cls the `struct TALER_MERCHANT_KycGetHandle` + * @param response_code HTTP response code, 0 on error + * @param response response body, NULL if not in JSON + */ +static void +handle_get_kyc_finished (void *cls, + long response_code, + const void *response) +{ + struct TALER_MERCHANT_KycGetHandle *kyc = cls; + const json_t *json = response; + struct TALER_MERCHANT_KycResponse kr = { + .hr.http_status = (unsigned int) response_code, + .hr.reply = json + }; + + kyc->job = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Got /kyc response with status code %u\n", + (unsigned int) response_code); + switch (response_code) + { + case MHD_HTTP_NO_CONTENT: + break; + case MHD_HTTP_ACCEPTED: + case MHD_HTTP_BAD_GATEWAY: + case MHD_HTTP_GATEWAY_TIMEOUT: + { + json_t *pends; + json_t *touts; + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_json ("pending_kycs", + &pends), + GNUNET_JSON_spec_json ("timeout_kycs", + &touts), + GNUNET_JSON_spec_end () + }; + + if (GNUNET_OK != + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) + { + kr.hr.http_status = 0; + kr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + break; + } + if ( (! json_is_array (pends)) || + (! json_is_array (touts)) || + (GNUNET_OK != + parse_kyc (kyc, + &kr, + pends, + touts)) ) + { + kr.hr.http_status = 0; + kr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + break; + } + /* parse_kyc called the continuation already */ + GNUNET_JSON_parse_free (spec); + TALER_MERCHANT_kyc_get_cancel (kyc); + return; + } + case MHD_HTTP_UNAUTHORIZED: + kr.hr.ec = TALER_JSON_get_error_code (json); + kr.hr.hint = TALER_JSON_get_error_hint (json); + /* Nothing really to verify, merchant says we need to authenticate. */ + break; + default: + /* unexpected response code */ + kr.hr.ec = TALER_JSON_get_error_code (json); + kr.hr.hint = TALER_JSON_get_error_hint (json); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected response code %u/%d\n", + (unsigned int) response_code, + (int) kr.hr.ec); + break; + } + kyc->cb (kyc->cb_cls, + &kr); + TALER_MERCHANT_kyc_get_cancel (kyc); +} + + +/** + * Issue a GET KYC request to the backend. + * Returns KYC status of bank accounts. + * + * @param ctx execution context + * @param[in] url URL to use for the request, consumed! + * @param h_wire which bank account to query, NULL for all + * @param exchange_url which exchange to query, NULL for all + * @param timeout how long to wait for a (positive) reply + * @param cb function to call with the result + * @param cb_cls closure for @a cb + * @return handle for this operation, NULL upon errors + */ +static struct TALER_MERCHANT_KycGetHandle * +kyc_get (struct GNUNET_CURL_Context *ctx, + char *url, + const struct GNUNET_HashCode *h_wire, + const char *exchange_url, + struct GNUNET_TIME_Relative timeout, + TALER_MERCHANT_KycGetCallback cb, + void *cb_cls) +{ + struct TALER_MERCHANT_KycGetHandle *kyc; + CURL *eh; + char timeout_ms[32]; + + kyc = GNUNET_new (struct TALER_MERCHANT_KycGetHandle); + kyc->ctx = ctx; + kyc->cb = cb; + kyc->cb_cls = cb_cls; + GNUNET_snprintf (timeout_ms, + sizeof (timeout_ms), + "%llu", + (unsigned long long) (timeout.rel_value_us / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us)); + kyc->url = TALER_url_join (url, + "h_wire", + NULL == h_wire + ? NULL + : GNUNET_h2s_full (h_wire), + "exchange_url", + NULL == exchange_url + ? NULL + : exchange_url, + "timeout_ms", + GNUNET_TIME_relative_is_zero (timeout) + ? NULL + : timeout_ms, + NULL); + GNUNET_free (url); + if (NULL == kyc->url) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not construct request URL.\n"); + GNUNET_free (kyc); + return NULL; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Requesting URL '%s'\n", + kyc->url); + eh = curl_easy_init (); + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_URL, + kyc->url)); + kyc->job = GNUNET_CURL_job_add (ctx, + eh, + &handle_get_kyc_finished, + kyc); + return kyc; +} + + +struct TALER_MERCHANT_KycGetHandle * +TALER_MERCHANT_kyc_get (struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const struct GNUNET_HashCode *h_wire, + const char *exchange_url, + struct GNUNET_TIME_Relative timeout, + TALER_MERCHANT_KycGetCallback cb, + void *cb_cls) +{ + char *url; + + GNUNET_asprintf (&url, + "%sprivate/kyc", + backend_url); + return kyc_get (ctx, + url, + h_wire, + exchange_url, + timeout, + cb, + cb_cls); +} + + +struct TALER_MERCHANT_KycGetHandle * +TALER_MERCHANT_management_kyc_get (struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const char *instance_id, + const struct GNUNET_HashCode *h_wire, + const char *exchange_url, + struct GNUNET_TIME_Relative timeout, + TALER_MERCHANT_KycGetCallback cb, + void *cb_cls) +{ + char *url; + + GNUNET_asprintf (&url, + "%smanagement/instances/%s/kyc", + backend_url, + instance_id); + return kyc_get (ctx, + url, + h_wire, + exchange_url, + timeout, + cb, + cb_cls); +} + + +void +TALER_MERCHANT_kyc_get_cancel ( + struct TALER_MERCHANT_KycGetHandle *kyc) +{ + if (NULL != kyc->job) + GNUNET_CURL_job_cancel (kyc->job); + GNUNET_free (kyc->url); + GNUNET_free (kyc); +} |