diff options
author | Christian Grothoff <christian@grothoff.org> | 2020-06-13 23:58:31 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2020-06-13 23:58:31 +0200 |
commit | b8fa0dde35ddd207a3239f97954c547676e89b21 (patch) | |
tree | 47722b502afdd50ef765f22e15c7b04b57191805 /src/lib/merchant_api_merchant_get_order.c | |
parent | ab22ee348ef322cde1b74f54d2d939b191406eec (diff) | |
download | merchant-b8fa0dde35ddd207a3239f97954c547676e89b21.tar.gz merchant-b8fa0dde35ddd207a3239f97954c547676e89b21.tar.bz2 merchant-b8fa0dde35ddd207a3239f97954c547676e89b21.zip |
starting with merchant_api_merchant_get_order.c
Diffstat (limited to 'src/lib/merchant_api_merchant_get_order.c')
-rw-r--r-- | src/lib/merchant_api_merchant_get_order.c | 304 |
1 files changed, 161 insertions, 143 deletions
diff --git a/src/lib/merchant_api_merchant_get_order.c b/src/lib/merchant_api_merchant_get_order.c index 6b0190af..279376d3 100644 --- a/src/lib/merchant_api_merchant_get_order.c +++ b/src/lib/merchant_api_merchant_get_order.c @@ -15,8 +15,8 @@ <http://www.gnu.org/licenses/> */ /** - * @file lib/merchant_api_poll_payment.c - * @brief Implementation of the /poll-payment GET request + * @file lib/merchant_api_merchant_get_order.c + * @brief Implementation of the GET /private/orders/$ORDER request * @author Christian Grothoff * @author Marcello Stanisci * @author Florian Dold @@ -33,9 +33,9 @@ /** - * @brief A poll payment operation handle + * @brief A GET /private/orders/$ORDER handle */ -struct TALER_MERCHANT_PollPaymentOperation +struct TALER_MERCHANT_OrderMerchantGetHandle { /** @@ -51,7 +51,7 @@ struct TALER_MERCHANT_PollPaymentOperation /** * Function to call with the result. */ - TALER_MERCHANT_PollPaymentCallback cb; + TALER_MERCHANT_OrderMerchantGetCallback cb; /** * Closure for @a cb. @@ -66,18 +66,20 @@ struct TALER_MERCHANT_PollPaymentOperation /** - * Function called when we're done processing the GET /poll-payment request. + * Function called when we're done processing the GET /private/orders/$ORDER + * request. * - * @param cls the `struct TALER_MERCHANT_PollPaymentOperation` + * @param cls the `struct TALER_MERCHANT_OrderMerchantGetHandle` * @param response_code HTTP response code, 0 on error * @param json response body, should be NULL */ static void -handle_poll_payment_finished (void *cls, - long response_code, - const void *response) +handle_merchant_order_get_finished (void *cls, + long response_code, + const void *response) { - struct TALER_MERCHANT_PollPaymentOperation *cpo = cls; + struct TALER_MERCHANT_OrderMerchantGetHandle *omgh = cls; +#if FIXME struct TALER_Amount refund_amount = { 0 }; const json_t *json = response; const json_t *refunded; @@ -91,20 +93,20 @@ handle_poll_payment_finished (void *cls, GNUNET_JSON_spec_end () }; - cpo->job = NULL; + omgh->job = NULL; switch (response_code) { case MHD_HTTP_NOT_FOUND: hr.ec = TALER_JSON_get_error_code (json); hr.hint = TALER_JSON_get_error_hint (json); - cpo->cb (cpo->cb_cls, - &hr, - GNUNET_NO, - GNUNET_NO, - NULL, - NULL); - TALER_MERCHANT_poll_payment_cancel (cpo); + omgh->cb (omgh->cb_cls, + &hr, + GNUNET_NO, + GNUNET_NO, + NULL, + NULL); + TALER_MERCHANT_merchant_order_get_cancel (omgh); return; case MHD_HTTP_OK: /* see below */ @@ -117,13 +119,13 @@ handle_poll_payment_finished (void *cls, (unsigned int) response_code, (int) hr.ec); GNUNET_break_op (0); - cpo->cb (cpo->cb_cls, - &hr, - GNUNET_SYSERR, - GNUNET_SYSERR, - NULL, - NULL); - TALER_MERCHANT_poll_payment_cancel (cpo); + omgh->cb (omgh->cb_cls, + &hr, + GNUNET_SYSERR, + GNUNET_SYSERR, + NULL, + NULL); + TALER_MERCHANT_merchant_order_get_cancel (omgh); return; } @@ -138,24 +140,24 @@ handle_poll_payment_finished (void *cls, "no taler_pay_uri in unpaid poll-payment response\n"); GNUNET_break_op (0); hr.http_status = 0; - hr.ec = TALER_EC_POLL_PAYMENT_REPLY_MALFORMED; - cpo->cb (cpo->cb_cls, - &hr, - GNUNET_SYSERR, - GNUNET_SYSERR, - NULL, - NULL); + hr.ec = TALER_EC_MERCHANT_ORDER_GET_REPLY_MALFORMED; + omgh->cb (omgh->cb_cls, + &hr, + GNUNET_SYSERR, + GNUNET_SYSERR, + NULL, + NULL); } else { - cpo->cb (cpo->cb_cls, - &hr, - GNUNET_NO, - GNUNET_NO, - NULL, - taler_pay_uri); + omgh->cb (omgh->cb_cls, + &hr, + GNUNET_NO, + GNUNET_NO, + NULL, + taler_pay_uri); } - TALER_MERCHANT_poll_payment_cancel (cpo); + TALER_MERCHANT_merchant_order_get_cancel (omgh); return; } @@ -171,155 +173,171 @@ handle_poll_payment_finished (void *cls, "poll payment failed to parse JSON\n"); GNUNET_break_op (0); hr.http_status = 0; - hr.ec = TALER_EC_POLL_PAYMENT_REPLY_MALFORMED; - cpo->cb (cpo->cb_cls, - &hr, - GNUNET_SYSERR, - GNUNET_SYSERR, - NULL, - NULL); - TALER_MERCHANT_poll_payment_cancel (cpo); + hr.ec = TALER_EC_MERCHANT_ORDER_GET_REPLY_MALFORMED; + omgh->cb (omgh->cb_cls, + &hr, + GNUNET_SYSERR, + GNUNET_SYSERR, + NULL, + NULL); + TALER_MERCHANT_merchant_order_get_cancel (omgh); return; } - cpo->cb (cpo->cb_cls, - &hr, - GNUNET_YES, - (json_true () == refunded), - (json_true () == refunded) ? &refund_amount : NULL, - NULL); - TALER_MERCHANT_poll_payment_cancel (cpo); + omgh->cb (omgh->cb_cls, + &hr, + GNUNET_YES, + (json_true () == refunded), + (json_true () == refunded) ? &refund_amount : NULL, + NULL); +#endif + TALER_MERCHANT_merchant_order_get_cancel (omgh); } /** - * Issue a /poll-payment request to the backend. Polls the status - * of a payment. + * Checks the status of a payment. Issue a GET /private/orders/$ID request to + * the backend. * * @param ctx execution context * @param backend_url base URL of the merchant backend * @param order_id order id to identify the payment - * @param h_contract hash of the contract for @a order_id - * @param session_id sesion id for the payment (or NULL if the payment is not bound to a session) + * @param session_id sesion id for the payment (or NULL if the check is not + * bound to a session) + * @param transfer if true, obtain the wire transfer status from the exhcange. + * Otherwise, the wire transfer status MAY be returned if it is available. * @param timeout timeout to use in long polling (how long may the server wait to reply * before generating an unpaid response). Note that this is just provided to * the server, we as client will block until the response comes back or until - * #TALER_MERCHANT_poll_payment_cancel() is called. - * @param min_refund long poll for the service to approve a refund exceeding this value; - * use NULL to not wait for any refund (only for payment). Only makes sense - * with a non-zero @a timeout. - * @param poll_payment_cb callback which will work the response gotten from the backend - * @param poll_payment_cb_cls closure to pass to @a poll_payment_cb + * #TALER_MERCHANT_order_get_cancel() is called. + * @param cb callback which will work the response gotten from the backend + * @param cb_cls closure to pass to @a cb * @return handle for this operation, NULL upon errors */ -struct TALER_MERCHANT_PollPaymentOperation * -TALER_MERCHANT_poll_payment ( - struct GNUNET_CURL_Context *ctx, - const char *backend_url, - const char *order_id, - const struct GNUNET_HashCode *h_contract, - const char *session_id, - struct GNUNET_TIME_Relative timeout, - const struct TALER_Amount *min_refund, - TALER_MERCHANT_PollPaymentCallback poll_payment_cb, - void *poll_payment_cb_cls) +struct TALER_MERCHANT_OrderMerchantGetHandle * +TALER_MERCHANT_merchant_order_get (struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const char *order_id, + const char *session_id, + bool transfer, + struct GNUNET_TIME_Relative timeout, + TALER_MERCHANT_OrderMerchantGetCallback cb, + void *cb_cls) { - struct TALER_MERCHANT_PollPaymentOperation *cpo; - CURL *eh; - char *h_contract_s; - char *timeout_s; - unsigned int ts; + struct TALER_MERCHANT_OrderMerchantGetHandle *omgh; + unsigned long long tms; long tlong; GNUNET_assert (NULL != backend_url); GNUNET_assert (NULL != order_id); - h_contract_s = GNUNET_STRINGS_data_to_string_alloc (h_contract, - sizeof (*h_contract)); - ts = (unsigned int) (timeout.rel_value_us - / GNUNET_TIME_UNIT_SECONDS.rel_value_us); + tms = (unsigned long long) (timeout.rel_value_us + / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us); /* set curl timeout to *our* long poll timeout plus one minute (for network latency and processing delays) */ tlong = (long) (GNUNET_TIME_relative_add (timeout, GNUNET_TIME_UNIT_MINUTES). rel_value_us / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us); - GNUNET_asprintf (&timeout_s, - "%u", - ts); - cpo = GNUNET_new (struct TALER_MERCHANT_PollPaymentOperation); - cpo->ctx = ctx; - cpo->cb = poll_payment_cb; - cpo->cb_cls = poll_payment_cb_cls; - cpo->url = TALER_url_join (backend_url, - "public/poll-payment", - "order_id", order_id, - "session_id", session_id, - "h_contract", h_contract_s, - (0 != ts) ? "timeout" : NULL, - timeout_s, - (NULL != min_refund) ? "refund" : NULL, - (NULL != min_refund) ? TALER_amount2s ( - min_refund) : NULL, - NULL); - GNUNET_free (h_contract_s); - GNUNET_free (timeout_s); - if (NULL == cpo->url) + omgh = GNUNET_new (struct TALER_MERCHANT_OrderMerchantGetHandle); + omgh->ctx = ctx; + omgh->cb = cb; + omgh->cb_cls = cb_cls; { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Could not construct request URL.\n"); - GNUNET_free (cpo); - return NULL; + char *path; + char timeout_ms[32]; + + GNUNET_snprintf (timeout_ms, + sizeof (timeout_ms), + "%llu", + tms); + GNUNET_asprintf (&path, + "orders/%s", + order_id); + omgh->url = TALER_url_join (backend_url, + path, + "session_id", session_id, + "transfer", transfer ? "YES" : "NO", + (0 != tms) ? "timeout_ms" : NULL, + timeout_ms, + NULL); } - eh = curl_easy_init (); - if (CURLE_OK != curl_easy_setopt (eh, - CURLOPT_URL, - cpo->url)) + if (NULL == omgh->url) { - GNUNET_break (0); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not construct request URL.\n"); + GNUNET_free (omgh); return NULL; } - if (CURLE_OK != curl_easy_setopt (eh, - CURLOPT_TIMEOUT_MS, - tlong)) + { - GNUNET_break (0); - return NULL; - } + CURL *eh; - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "polling payment from %s\n", - cpo->url); + eh = curl_easy_init (); + if (NULL == eh) + { + GNUNET_break (0); + GNUNET_free (omgh->url); + GNUNET_free (omgh); + return NULL; + } + if (CURLE_OK != curl_easy_setopt (eh, + CURLOPT_URL, + omgh->url)) + { + GNUNET_break (0); + curl_easy_cleanup (eh); + GNUNET_free (omgh->url); + GNUNET_free (omgh); + return NULL; + } + if (CURLE_OK != curl_easy_setopt (eh, + CURLOPT_TIMEOUT_MS, + tlong)) + { + GNUNET_break (0); + curl_easy_cleanup (eh); + GNUNET_free (omgh->url); + GNUNET_free (omgh); + return NULL; + } - if (NULL == (cpo->job = GNUNET_CURL_job_add (ctx, - eh, - GNUNET_YES, - &handle_poll_payment_finished, - cpo))) - { - GNUNET_break (0); - return NULL; + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Getting order status from %s\n", + omgh->url); + if (NULL == (omgh->job = + GNUNET_CURL_job_add (ctx, + eh, + GNUNET_YES, + &handle_merchant_order_get_finished, + omgh))) + { + GNUNET_break (0); + GNUNET_free (omgh->url); + GNUNET_free (omgh); + return NULL; + } } - return cpo; + return omgh; } /** - * Cancel a GET /poll-payment request. + * Cancel a GET /private/orders/$ORDER request. * - * @param cph handle to the request to be canceled + * @param omgh handle to the request to be canceled */ void -TALER_MERCHANT_poll_payment_cancel ( - struct TALER_MERCHANT_PollPaymentOperation *cph) +TALER_MERCHANT_merchant_order_get_cancel ( + struct TALER_MERCHANT_OrderMerchantGetHandle *omgh) { - if (NULL != cph->job) + if (NULL != omgh->job) { - GNUNET_CURL_job_cancel (cph->job); - cph->job = NULL; + GNUNET_CURL_job_cancel (omgh->job); + omgh->job = NULL; } - GNUNET_free (cph->url); - GNUNET_free (cph); + GNUNET_free (omgh->url); + GNUNET_free (omgh); } -/* end of merchant_api_poll_payment.c */ +/* end of merchant_api_merchant_get_order.c */ |