merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

commit 09e5b8f57295e763e9e90812dd94c3bb44bf31f0
parent 3be8637b90adac677a0be1eea3ccb2b5a151e9de
Author: priscilla <priscilla.huang@efrei.net>
Date:   Fri,  2 Dec 2022 10:48:29 -0500

webhook

Diffstat:
Asrc/lib/merchant_api_delete_webhook.c | 184+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/lib/merchant_api_get_templates.c | 4++--
Asrc/lib/merchant_api_get_webhook.c | 219+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/lib/merchant_api_get_webhooks.c | 251+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/lib/merchant_api_patch_webhook.c | 253+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/lib/merchant_api_post_templates.c | 2+-
Asrc/lib/merchant_api_post_webhooks.c | 239+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 1149 insertions(+), 3 deletions(-)

diff --git a/src/lib/merchant_api_delete_webhook.c b/src/lib/merchant_api_delete_webhook.c @@ -0,0 +1,184 @@ +/* + This file is part of TALER + Copyright (C) 2014-2018, 2020 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_delete_template.c + * @brief Implementation of the DELETE /webhooks/$ID request of the merchant's HTTP API + * @author Priscilla HUANG + */ +#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 "merchant_api_curl_defaults.h" +#include <taler/taler_json_lib.h> +#include <taler/taler_signatures.h> + + +/** + * Handle for a DELETE /webhooks/$ID operation. + */ +struct TALER_MERCHANT_WebhookDeleteHandle +{ + /** + * The url for this request. + */ + char *url; + + /** + * Handle for the request. + */ + struct GNUNET_CURL_Job *job; + + /** + * Function to call with the result. + */ + TALER_MERCHANT_WebhookDeleteCallback cb; + + /** + * Closure for @a cb. + */ + void *cb_cls; + + /** + * Reference to the execution context. + */ + struct GNUNET_CURL_Context *ctx; + +}; + + +/** + * Function called when we're done processing the + * HTTP GET /webhooks/$ID request. + * + * @param cls the `struct TALER_MERCHANT_WebhookDeleteHandle` + * @param response_code HTTP response code, 0 on error + * @param response response body, NULL if not in JSON + */ +static void +handle_delete_webhook_finished (void *cls, + long response_code, + const void *response) +{ + struct TALER_MERCHANT_WebhookDeleteHandle *wdh = cls; + const json_t *json = response; + struct TALER_MERCHANT_HttpResponse hr = { + .http_status = (unsigned int) response_code, + .reply = json + }; + + tdh->job = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Got /webhooks/$ID response with status code %u\n", + (unsigned int) response_code); + switch (response_code) + { + case MHD_HTTP_NO_CONTENT: + break; + case MHD_HTTP_UNAUTHORIZED: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + /* Nothing really to verify, merchant says we need to authenticate. */ + break; + case MHD_HTTP_NOT_FOUND: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + break; + case MHD_HTTP_CONFLICT: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + break; + default: + /* unexpected response code */ + hr.ec = TALER_JSON_get_error_code (json); + 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) hr.ec); + break; + } + wdh->cb (wdh->cb_cls, + &hr); + TALER_MERCHANT_webhook_delete_cancel (tdh); +} + + +struct TALER_MERCHANT_WebhookDeleteHandle * +TALER_MERCHANT_webhook_delete ( + struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const char *webhook_id, + TALER_MERCHANT_WebhookDeleteCallback cb, + void *cb_cls) +{ + struct TALER_MERCHANT_WebhookDeleteHandle *wdh; + + wdh = GNUNET_new (struct TALER_MERCHANT_WebhookDeleteHandle); + wdh->ctx = ctx; + wdh->cb = cb; + wdh->cb_cls = cb_cls; + { + char *path; + + GNUNET_asprintf (&path, + "private/webhooks/%s", + webhook_id); + wdh->url = TALER_url_join (backend_url, + path, + NULL); + GNUNET_free (path); + } + if (NULL == wdh->url) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not construct request URL.\n"); + GNUNET_free (wdh); + return NULL; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Requesting URL '%s'\n", + wdh->url); + { + CURL *eh; + + eh = TALER_MERCHANT_curl_easy_get_ (wdh->url); + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_CUSTOMREQUEST, + MHD_HTTP_METHOD_DELETE)); + wdh->job = GNUNET_CURL_job_add (ctx, + eh, + &handle_delete_template_finished, + wdh); + } + return wdh; +} + + +void +TALER_MERCHANT_webhook_delete_cancel ( + struct TALER_MERCHANT_WebhookDeleteHandle *wdh) +{ + if (NULL != wdh->job) + GNUNET_CURL_job_cancel (wdh->job); + GNUNET_free (wdh->url); + GNUNET_free (wdh); +} diff --git a/src/lib/merchant_api_get_templates.c b/src/lib/merchant_api_get_templates.c @@ -32,7 +32,7 @@ /** - * Handle for a GET /products operation. + * Handle for a GET /templates operation. */ struct TALER_MERCHANT_TemplatesGetHandle { @@ -68,7 +68,7 @@ struct TALER_MERCHANT_TemplatesGetHandle * Parse template information from @a ia. * * @param ia JSON array (or NULL!) with template data - * @param pgh operation handle + * @param wgh operation handle * @return #GNUNET_OK on success */ static int diff --git a/src/lib/merchant_api_get_webhook.c b/src/lib/merchant_api_get_webhook.c @@ -0,0 +1,219 @@ +/* + This file is part of TALER + Copyright (C) 2014-2018, 2020 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_webhook.c + * @brief Implementation of the GET /webhooks/$ID request of the merchant's HTTP API + * @author Priscilla HUANG + */ +#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 "merchant_api_curl_defaults.h" +#include <taler/taler_json_lib.h> +#include <taler/taler_signatures.h> + + +/** + * Handle for a GET /webhooks/$ID operation. + */ +struct TALER_MERCHANT_WebhookGetHandle +{ + /** + * The url for this request. + */ + char *url; + + /** + * Handle for the request. + */ + struct GNUNET_CURL_Job *job; + + /** + * Function to call with the result. + */ + TALER_MERCHANT_WebhookGetCallback cb; + + /** + * Closure for @a cb. + */ + void *cb_cls; + + /** + * Reference to the execution context. + */ + struct GNUNET_CURL_Context *ctx; + +}; + + +/** + * Function called when we're done processing the + * HTTP GET /webhooks/$ID request. + * + * @param cls the `struct TALER_MERCHANT_WebhookGetHandle` + * @param response_code HTTP response code, 0 on error + * @param response response body, NULL if not in JSON + */ +static void +handle_get_webhook_finished (void *cls, + long response_code, + const void *response) +{ + struct TALER_MERCHANT_WebhookGetHandle *wgh = cls; + const json_t *json = response; + struct TALER_MERCHANT_HttpResponse hr = { + .http_status = (unsigned int) response_code, + .reply = json + }; + + wgh->job = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Got /webhooks/$ID response with status code %u\n", + (unsigned int) response_code); + switch (response_code) + { + case MHD_HTTP_OK: + { + const char *event_type; + const char *url; + const char *http_method; + const char *header_template; + const char *body_template; + bool rst_ok = true; + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_string ("event_type", + &event_type), + GNUNET_JSON_spec_string ("url", + &url), + GNUNET_JSON_spec_string ("http_method", + &http_method), + GNUNET_JSON_spec_string ("header_template", + &header_template), + GNUNET_JSON_spec_string ("body_template", + &body_template), + GNUNET_JSON_spec_end () + }; + + + if ( (rst_ok) && + (GNUNET_OK == + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) ) + { + wgh->cb (wgh->cb_cls, + &hr, + event_type, + url, + http_method, + header_template, + body_template); + GNUNET_JSON_parse_free (spec); + TALER_MERCHANT_webhook_get_cancel (wgh); + return; + } + hr.http_status = 0; + hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + GNUNET_JSON_parse_free (spec); + break; + } + case MHD_HTTP_UNAUTHORIZED: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + /* Nothing really to verify, merchant says we need to authenticate. */ + break; + case MHD_HTTP_NOT_FOUND: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + break; + default: + /* unexpected response code */ + hr.ec = TALER_JSON_get_error_code (json); + 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) hr.ec); + break; + } + wgh->cb (wgh->cb_cls, + &hr, + NULL, + NULL, + NULL); + TALER_MERCHANT_webhook_get_cancel (wgh); +} + + +struct TALER_MERCHANT_WebhookGetHandle * +TALER_MERCHANT_webhook_get ( + struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const char *webhook_id, + TALER_MERCHANT_WebhookGetCallback cb, + void *cb_cls) +{ + struct TALER_MERCHANT_WebhookGetHandle *wgh; + CURL *eh; + + wgh = GNUNET_new (struct TALER_MERCHANT_WebhookGetHandle); + wgh->ctx = ctx; + wgh->cb = cb; + wgh->cb_cls = cb_cls; + { + char *path; + + GNUNET_asprintf (&path, + "private/webhooks/%s", + template_id); + wgh->url = TALER_url_join (backend_url, + path, + NULL); + GNUNET_free (path); + } + if (NULL == wgh->url) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not construct request URL.\n"); + GNUNET_free (wgh); + return NULL; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Requesting URL '%s'\n", + wgh->url); + eh = TALER_MERCHANT_curl_easy_get_ (wgh->url); + wgh->job = GNUNET_CURL_job_add (ctx, + eh, + &handle_get_webhook_finished, + wgh); + return wgh; +} + + +void +TALER_MERCHANT_webhook_get_cancel ( + struct TALER_MERCHANT_WebhookGetHandle *wgh) +{ + if (NULL != wgh->job) + GNUNET_CURL_job_cancel (wgh->job); + GNUNET_free (wgh->url); + GNUNET_free (wgh); +} diff --git a/src/lib/merchant_api_get_webhooks.c b/src/lib/merchant_api_get_webhooks.c @@ -0,0 +1,251 @@ +/* + This file is part of TALER + Copyright (C) 2014-2018, 2020 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_webhooks.c + * @brief Implementation of the GET /webhooks request of the merchant's HTTP API + * @author Priscilla HUANG + */ +#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 "merchant_api_curl_defaults.h" +#include <taler/taler_json_lib.h> +#include <taler/taler_signatures.h> + + +/** + * Handle for a GET /webhooks operation. + */ +struct TALER_MERCHANT_WebhooksGetHandle +{ + /** + * The url for this request. + */ + char *url; + + /** + * Handle for the request. + */ + struct GNUNET_CURL_Job *job; + + /** + * Function to call with the result. + */ + TALER_MERCHANT_WebhooksGetCallback cb; + + /** + * Closure for @a cb. + */ + void *cb_cls; + + /** + * Reference to the execution context. + */ + struct GNUNET_CURL_Context *ctx; + +}; + + +/** + * Parse webhook information from @a ia. + * + * @param ia JSON array (or NULL!) with webhook data + * @param wgh operation handle + * @return #GNUNET_OK on success + */ +static int +parse_webhooks (const json_t *ia, + struct TALER_MERCHANT_webhooksGetHandle *wgh) +{ + unsigned int ies_len = json_array_size (ia); + struct TALER_MERCHANT_WebhookEntry ies[ies_len]; + size_t index; + json_t *value; + int ret; + + ret = GNUNET_OK; + json_array_foreach (ia, index, value) { + struct TALER_MERCHANT_WebhookEntry *ie = &ies[index]; + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_string ("webhook_id", + &ie->webhook_id), + GNUNET_JSON_spec_end () + }; + + if (GNUNET_OK != + GNUNET_JSON_parse (value, + spec, + NULL, NULL)) + { + GNUNET_break_op (0); + ret = GNUNET_SYSERR; + continue; + } + if (GNUNET_SYSERR == ret) + break; + } + if (GNUNET_OK == ret) + { + struct TALER_MERCHANT_HttpResponse hr = { + .http_status = MHD_HTTP_OK + }; + + wgh->cb (tgh->cb_cls, + &hr, + ies_len, + ies); + wgh->cb = NULL; /* just to be sure */ + } + return ret; +} + + +/** + * Function called when we're done processing the + * HTTP /webhooks request. + * + * @param cls the `struct TALER_MERCHANT_WebhooksGetHandle` + * @param response_code HTTP response code, 0 on error + * @param response response body, NULL if not in JSON + */ +static void +handle_get_webhooks_finished (void *cls, + long response_code, + const void *response) +{ + struct TALER_MERCHANT_WebhooksGetHandle *tgh = cls; + const json_t *json = response; + struct TALER_MERCHANT_HttpResponse hr = { + .http_status = (unsigned int) response_code, + .reply = json + }; + + wgh->job = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Got /webhooks response with status code %u\n", + (unsigned int) response_code); + switch (response_code) + { + case MHD_HTTP_OK: + { + json_t *webhooks; + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_json ("webhooks", + &webhooks), + GNUNET_JSON_spec_end () + }; + + if (GNUNET_OK != + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) + { + hr.http_status = 0; + hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + } + else + { + if ( (! json_is_array (webhooks)) || + (GNUNET_OK == + parse_templates (webhooks, + wgh)) ) + { + GNUNET_JSON_parse_free (spec); + TALER_MERCHANT_webhooks_get_cancel (wgh); + return; + } + else + { + hr.http_status = 0; + hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + } + } + GNUNET_JSON_parse_free (spec); + break; + } + case MHD_HTTP_UNAUTHORIZED: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + /* Nothing really to verify, merchant says we need to authenticate. */ + break; + default: + /* unexpected response code */ + hr.ec = TALER_JSON_get_error_code (json); + 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) hr.ec); + break; + } + tgh->cb (wgh->cb_cls, + &hr, + 0, + NULL); + TALER_MERCHANT_webhooks_get_cancel (wgh); +} + + +struct TALER_MERCHANT_WebhooksGetHandle * +TALER_MERCHANT_webhooks_get ( + struct GNUNET_CURL_Context *ctx, + const char *backend_url, + TALER_MERCHANT_WebhooksGetCallback cb, + void *cb_cls) +{ + struct TALER_MERCHANT_WebhooksGetHandle *wgh; + CURL *eh; + + wgh = GNUNET_new (struct TALER_MERCHANT_WebhooksGetHandle); + wgh->ctx = ctx; + wgh->cb = cb; + wgh->cb_cls = cb_cls; + wgh->url = TALER_url_join (backend_url, + "private/webhooks", + NULL); + if (NULL == wgh->url) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not construct request URL.\n"); + GNUNET_free (wgh); + return NULL; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Requesting URL '%s'\n", + wgh->url); + eh = TALER_MERCHANT_curl_easy_get_ (wgh->url); + wgh->job = GNUNET_CURL_job_add (ctx, + eh, + &handle_get_webhooks_finished, + wgh); + return wgh; +} + + +void +TALER_MERCHANT_webhooks_get_cancel ( + struct TALER_MERCHANT_WebhooksGetHandle *wgh) +{ + if (NULL != wgh->job) + GNUNET_CURL_job_cancel (wgh->job); + GNUNET_free (wgh->url); + GNUNET_free (wgh); +} diff --git a/src/lib/merchant_api_patch_webhook.c b/src/lib/merchant_api_patch_webhook.c @@ -0,0 +1,253 @@ +/* + This file is part of TALER + Copyright (C) 2020, 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_patch_webhook.c + * @brief Implementation of the PATCH /webhooks/$ID request + * of the merchant's HTTP API + * @author Priscilla HUANG + */ +#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 "taler_merchant_service.h" +#include "merchant_api_curl_defaults.h" +#include <taler/taler_json_lib.h> +#include <taler/taler_curl_lib.h> + + +/** + * Handle for a PATCH /webhooks/$ID operation. + */ +struct TALER_MERCHANT_WebhookPatchHandle +{ + + /** + * The url for this request. + */ + char *url; + + /** + * Handle for the request. + */ + struct GNUNET_CURL_Job *job; + + /** + * Function to call with the result. + */ + TALER_MERCHANT_WebhookPatchCallback cb; + + /** + * Closure for @a cb. + */ + void *cb_cls; + + /** + * Reference to the execution context. + */ + struct GNUNET_CURL_Context *ctx; + + /** + * Minor context that holds body and headers. + */ + struct TALER_CURL_PostContext post_ctx; + +}; + + +/** + * Function called when we're done processing the + * HTTP PATCH /webhooks/$ID request. + * + * @param cls the `struct TALER_MERCHANT_WebhookPatchHandle` + * @param response_code HTTP response code, 0 on error + * @param response response body, NULL if not in JSON + */ +static void +handle_patch_webhook_finished (void *cls, + long response_code, + const void *response) +{ + struct TALER_MERCHANT_WebhookPatchHandle *wph = cls; + const json_t *json = response; + struct TALER_MERCHANT_HttpResponse hr = { + .http_status = (unsigned int) response_code, + .reply = json + }; + + wph->job = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "PATCH /webhooks/$ID completed with response code %u\n", + (unsigned int) response_code); + switch (response_code) + { + case 0: + hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + break; + case MHD_HTTP_NO_CONTENT: + break; + case MHD_HTTP_BAD_REQUEST: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + GNUNET_break_op (0); + /* This should never happen, either us + * or the merchant is buggy (or API version conflict); + * just pass JSON reply to the application */ + break; + case MHD_HTTP_UNAUTHORIZED: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + /* Nothing really to verify, merchant says we need to authenticate. */ + break; + case MHD_HTTP_FORBIDDEN: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + /* Nothing really to verify, merchant says we tried to abort the payment + * after it was successful. We should pass the JSON reply to the + * application */ + break; + case MHD_HTTP_NOT_FOUND: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + break; + case MHD_HTTP_CONFLICT: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + break; + case MHD_HTTP_INTERNAL_SERVER_ERROR: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + /* Server had an internal issue; we should retry, + but this API leaves this to the application */ + break; + default: + TALER_MERCHANT_parse_error_details_ (json, + response_code, + &hr); + /* unexpected response code */ + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected response code %u/%d\n", + (unsigned int) response_code, + (int) hr.ec); + GNUNET_break_op (0); + break; + } + wph->cb (wph->cb_cls, + &hr); + TALER_MERCHANT_webhook_patch_cancel (wph); +} + + +struct TALER_MERCHANT_WebhookPatchHandle * +TALER_MERCHANT_webhook_patch ( + struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const char *webhook_id, + const char *event_type, + const char *url, + const char *http_method, + const char *header_template, + const char *body_template, + TALER_MERCHANT_WebhookPatchCallback cb, + void *cb_cls) +{ + struct TALER_MERCHANT_WebhookPatchHandle *wph; + json_t *req_obj; + + req_obj = GNUNET_JSON_PACK ( + GNUNET_JSON_pack_string ("event_type", + event_type), + GNUNET_JSON_pack_string ("url", + url), + GNUNET_JSON_pack_string ("http_method", + http_method), + GNUNET_JSON_pack_string ("header_template", + header_template), + GNUNET_JSON_pack_string ("body_template", + body_template)); + wph = GNUNET_new (struct TALER_MERCHANT_WebhookPatchHandle); + wph->ctx = ctx; + wph->cb = cb; + wph->cb_cls = cb_cls; + { + char *path; + + GNUNET_asprintf (&path, + "private/webhooks/%s", + webhook_id); + wph->url = TALER_url_join (backend_url, + path, + NULL); + GNUNET_free (path); + } + if (NULL == wph->url) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not construct request URL.\n"); + json_decref (req_obj); + GNUNET_free (wph); + return NULL; + } + { + CURL *eh; + + eh = TALER_MERCHANT_curl_easy_get_ (wph->url); + if (GNUNET_OK != + TALER_curl_easy_post (&wph->post_ctx, + eh, + req_obj)) + { + GNUNET_break (0); + curl_easy_cleanup (eh); + json_decref (req_obj); + GNUNET_free (wph); + return NULL; + } + json_decref (req_obj); + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_CUSTOMREQUEST, + MHD_HTTP_METHOD_PATCH)); + wph->job = GNUNET_CURL_job_add2 (ctx, + eh, + wph->post_ctx.headers, + &handle_patch_webhook_finished, + wph); + } + return wph; +} + + +void +TALER_MERCHANT_webhook_patch_cancel ( + struct TALER_MERCHANT_WebhookPatchHandle *wph) +{ + if (NULL != wph->job) + { + GNUNET_CURL_job_cancel (wph->job); + wph->job = NULL; + } + TALER_curl_easy_post_finished (&wph->post_ctx); + GNUNET_free (wph->url); + GNUNET_free (wph); +} + + +/* end of merchant_api_patch_webhook.c */ diff --git a/src/lib/merchant_api_post_templates.c b/src/lib/merchant_api_post_templates.c @@ -74,7 +74,7 @@ struct TALER_MERCHANT_TemplatesPostHandle /** * Function called when we're done processing the - * HTTP POST /products request. + * HTTP POST /templates request. * * @param cls the `struct TALER_MERCHANT_TemplatesPostHandle` * @param response_code HTTP response code, 0 on error diff --git a/src/lib/merchant_api_post_webhooks.c b/src/lib/merchant_api_post_webhooks.c @@ -0,0 +1,239 @@ +/* + This file is part of TALER + Copyright (C) 2020-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_post_webhooks.c + * @brief Implementation of the POST /webhooks request + * of the merchant's HTTP API + * @author Priscilla HUANG + */ +#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 "taler_merchant_service.h" +#include "merchant_api_curl_defaults.h" +#include <taler/taler_json_lib.h> +#include <taler/taler_curl_lib.h> + + +/** + * Handle for a POST /webhooks/$ID operation. + */ +struct TALER_MERCHANT_WebhooksPostHandle +{ + + /** + * The url for this request. + */ + char *url; + + /** + * Handle for the request. + */ + struct GNUNET_CURL_Job *job; + + /** + * Function to call with the result. + */ + TALER_MERCHANT_WebhooksPostCallback cb; + + /** + * Closure for @a cb. + */ + void *cb_cls; + + /** + * Reference to the execution context. + */ + struct GNUNET_CURL_Context *ctx; + + /** + * Minor context that holds body and headers. + */ + struct TALER_CURL_PostContext post_ctx; + +}; + + +/** + * Function called when we're done processing the + * HTTP POST /webhooks request. + * + * @param cls the `struct TALER_MERCHANT_WebhooksPostHandle` + * @param response_code HTTP response code, 0 on error + * @param response response body, NULL if not in JSON + */ +static void +handle_post_webhooks_finished (void *cls, + long response_code, + const void *response) +{ + struct TALER_MERCHANT_WebhooksPostHandle *wph = cls; + const json_t *json = response; + struct TALER_MERCHANT_HttpResponse hr = { + .http_status = (unsigned int) response_code, + .reply = json + }; + + wph->job = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "POST /webhooks completed with response code %u\n", + (unsigned int) response_code); + switch (response_code) + { + case 0: + hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + break; + case MHD_HTTP_NO_CONTENT: + break; + case MHD_HTTP_BAD_REQUEST: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + /* This should never happen, either us + * or the merchant is buggy (or API version conflict); + * just pass JSON reply to the application */ + break; + case MHD_HTTP_UNAUTHORIZED: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + /* Nothing really to verify, merchant says we need to authenticate. */ + break; + case MHD_HTTP_FORBIDDEN: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + /* Nothing really to verify, merchant says we tried to abort the payment + * after it was successful. We should pass the JSON reply to the + * application */ + break; + case MHD_HTTP_NOT_FOUND: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + /* Nothing really to verify, this should never + happen, we should pass the JSON reply to the + application */ + break; + case MHD_HTTP_CONFLICT: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + break; + case MHD_HTTP_INTERNAL_SERVER_ERROR: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + /* Server had an internal issue; we should retry, + but this API leaves this to the application */ + break; + default: + TALER_MERCHANT_parse_error_details_ (json, + response_code, + &hr); + /* unexpected response code */ + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected response code %u/%d\n", + (unsigned int) response_code, + (int) hr.ec); + GNUNET_break_op (0); + break; + } + wph->cb (wph->cb_cls, + &hr); + TALER_MERCHANT_webhooks_post_cancel (wph); +} + + +struct TALER_MERCHANT_WebhooksPostHandle * +TALER_MERCHANT_webhooks_post ( + struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const char *webhook_id, + const char *event_type, + const char *url, + const char *http_method, + const char *header_template, + const char *body_template, + TALER_MERCHANT_WebhooksPostCallback cb, + void *cb_cls) +{ + struct TALER_MERCHANT_WebhooksPostHandle *wph; + json_t *req_obj; + + req_obj = GNUNET_JSON_PACK ( + GNUNET_JSON_pack_string ("webhook_id", + webhook_id), + GNUNET_JSON_pack_string ("event_type", + event_type), + GNUNET_JSON_pack_string ("url", + url), + GNUNET_JSON_pack_string ("http_method", + http_method), + GNUNET_JSON_pack_string ("header_template", + header_template), + GNUNET_JSON_pack_string ("body_template", + body_template)); + tph = GNUNET_new (struct TALER_MERCHANT_WebhooksPostHandle); + wph->ctx = ctx; + wph->cb = cb; + wph->cb_cls = cb_cls; + wph->url = TALER_url_join (backend_url, + "private/webhooks", + NULL); + if (NULL == wph->url) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not construct request URL.\n"); + json_decref (req_obj); + GNUNET_free (wph); + return NULL; + } + { + CURL *eh; + + eh = TALER_MERCHANT_curl_easy_get_ (wph->url); + GNUNET_assert (GNUNET_OK == + TALER_curl_easy_post (&wph->post_ctx, + eh, + req_obj)); + json_decref (req_obj); + wph->job = GNUNET_CURL_job_add2 (ctx, + eh, + wph->post_ctx.headers, + &handle_post_webhooks_finished, + wph); + GNUNET_assert (NULL != wph->job); + } + return wph; +} + + +void +TALER_MERCHANT_webhooks_post_cancel ( + struct TALER_MERCHANT_WebhooksPostHandle *wph) +{ + if (NULL != wph->job) + { + GNUNET_CURL_job_cancel (wph->job); + wph->job = NULL; + } + TALER_curl_easy_post_finished (&wph->post_ctx); + GNUNET_free (wph->url); + GNUNET_free (wph); +} + + +/* end of merchant_api_post_webhooks.c */