diff options
-rw-r--r-- | src/include/taler_merchant_service.h | 72 | ||||
-rw-r--r-- | src/lib/Makefile.am | 3 | ||||
-rw-r--r-- | src/lib/merchant_api_common.c | 86 | ||||
-rw-r--r-- | src/lib/merchant_api_common.h | 13 |
4 files changed, 173 insertions, 1 deletions
diff --git a/src/include/taler_merchant_service.h b/src/include/taler_merchant_service.h index adcf0067..3462288e 100644 --- a/src/include/taler_merchant_service.h +++ b/src/include/taler_merchant_service.h @@ -918,4 +918,76 @@ void TALER_MERCHANT_tip_pickup_cancel (struct TALER_MERCHANT_TipPickupOperation *tp); +/* ********************** /check-payment ************************* */ + + +/** + * Handle for a /tip-pickup operation. + */ +struct TALER_MERCHANT_CheckPaymentOperation; + + +/** + * Callback to process a GET /check-payment request + * + * @param cls closure + * @param http_status HTTP status code for this request + * @param obj raw response body + * @param paid #GNUNET_YES if the payment is settled, #GNUNET_NO if not + * settled, $GNUNET_SYSERR on error + * (note that refunded payments are returned as paid!) + * @param refunded #GNUNET_YES if there is at least on refund on this payment, + * #GNUNET_NO if refunded, #GNUNET_SYSERR or error + * @param refunded_amount amount that was refunded, NULL if there + * was no refund + * @param payment_redirect_url URL to redirect the browser to in order to + * execute or re-play the payment (NULL if not applicable) + */ +typedef void +(*TALER_MERCHANT_CheckPaymentCallback) (void *cls, + unsigned int http_status, + const json_t *obj, + int paid, + int refunded, + struct TALER_Amount *refund_amount, + const char *payment_redirect_url); + + +/** + * Issue a /check-payment request to the backend. Checks the status + * of a payment. + * + * @param ctx execution context + * @param backend_url base URL of the merchant backend + * @param instance instance used for the transaction + * @param order_id order id to identify the payment + * @parem resource_url resource URL to identify duplicate payments (can be NULL) + * @parem session_id sesion id for the payment (or NULL if the payment is not bound to a session) + * @parem session_id sesion signature for the payment (or NULL if the payment + * is not bound to a session or the session is not signed yet) + * @param check_payment_cb callback which will work the response gotten from the backend + * @param check_payment_cb_cls closure to pass to @a check_payment_cb + * @return handle for this operation, NULL upon errors + */ +struct TALER_MERCHANT_CheckPaymentOperation * +TALER_MERCHANT_check_payment (struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const char *instance, + const char *order_id, + const char *resource_url, + const char *session_id, + const char *session_sig, + TALER_MERCHANT_CheckPaymentCallback check_payment_cb, + void *check_payment_cls); + + +/** + * Cancel a GET /check-payment request. + * + * @param cph handle to the request to be canceled + */ +void +TALER_MERCHANT_check_payment_cancel (struct TALER_MERCHANT_CheckPaymentOperation *cph); + + #endif /* _TALER_MERCHANT_SERVICE_H */ diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 4c6d5f8f..d407ee01 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -22,7 +22,8 @@ libtalermerchant_la_SOURCES = \ merchant_api_track_transaction.c \ merchant_api_track_transfer.c \ merchant_api_history.c \ - merchant_api_refund.c + merchant_api_refund.c \ + merchant_api_check_payment.c libtalermerchant_la_LIBADD = \ -ltalerexchange \ diff --git a/src/lib/merchant_api_common.c b/src/lib/merchant_api_common.c index 05e2637a..12bc81d4 100644 --- a/src/lib/merchant_api_common.c +++ b/src/lib/merchant_api_common.c @@ -20,6 +20,7 @@ * @author Christian Grothoff */ #include "platform.h" +#include <curl/curl.h> #include <gnunet/gnunet_util_lib.h> @@ -47,4 +48,89 @@ MAH_path_to_url_ (const char *base_url, return url; } + +/** + * Concatenate two strings and grow the first buffer (of size n) + * if necessary. + */ +#define STR_CAT_GROW(s, p, n) do { \ + for (; strlen (s) + strlen (p) >= n; (n) = (n) * 2); \ + (s) = GNUNET_realloc ((s), (n)); \ + GNUNET_assert (NULL != (s)); \ + strncat (s, p, n); \ + } while (0) + + +/** + * Make an absolute URL with query parameters. + * + * @param base_url absolute base URL to use + * @param path path of the url + * @param ... NULL-terminated key-value pairs (char *) for query parameters + * @returns the URL, must be freed with #GNUNET_free + */ +char * +MAH_make_url (const char *base_url, + const char *path, + ...) +{ + static CURL *curl = NULL; + if (NULL == curl) + { + curl = curl_easy_init(); + GNUNET_assert (NULL != curl); + } + + size_t n = 256; + char *res = GNUNET_malloc (n); + + GNUNET_assert (NULL != res); + + STR_CAT_GROW (res, base_url, n); + + if ( ('/' == path[0]) && + (0 < strlen (base_url)) && + ('/' == base_url[strlen (base_url) - 1]) ) + { + /* avoid generating URL with "//" from concat */ + path++; + } + else if ( ('/' != path[0]) && + ('/' != base_url[strlen (base_url) - 1])) + { + /* put '/' between path and base URL if necessary */ + STR_CAT_GROW (res, "/", n); + } + + STR_CAT_GROW (res, path, n); + + va_list args; + va_start (args, path); + + unsigned int iparam = 0; + + while (1) { + char *key = va_arg (args, char *); + if (NULL == key) + break; + char *value = va_arg (args, char *); + if (NULL == value) + continue; + if (0 == iparam) + STR_CAT_GROW (res, "?", n); + else + STR_CAT_GROW (res, "&", n); + iparam++; + char *urlencoded_value = curl_easy_escape (curl, value, strlen (value)); + STR_CAT_GROW (res, key, n); + STR_CAT_GROW (res, "=", n); + STR_CAT_GROW (res, urlencoded_value, n); + curl_free (urlencoded_value); + } + + va_end (args); + + return res; +} + /* end of merchant_api_common.c */ diff --git a/src/lib/merchant_api_common.h b/src/lib/merchant_api_common.h index 683f1dbd..09696e3b 100644 --- a/src/lib/merchant_api_common.h +++ b/src/lib/merchant_api_common.h @@ -33,4 +33,17 @@ char * MAH_path_to_url_ (const char *base_url, const char *path); +/** + * Make an absolute URL with query parameters. + * + * @param base_url absolute base URL to use + * @param path path of the url + * @param ... NULL-terminated key-value pairs (char *) for query parameters + * @returns the URL, must be freed with #GNUNET_free + */ +char * +MAH_make_url (const char *base_url, + const char *path, + ...); + #endif |