diff --git a/plugins/libplugin-pay.c b/plugins/libplugin-pay.c index d803df4e6..ad873743e 100644 --- a/plugins/libplugin-pay.c +++ b/plugins/libplugin-pay.c @@ -66,6 +66,8 @@ struct payment *payment_new(tal_t *ctx, struct command *cmd, p->routetxt = NULL; p->max_htlcs = UINT32_MAX; p->aborterror = NULL; + p->on_payment_success = NULL; + p->on_payment_failure = NULL; /* Copy over the relevant pieces of information. */ if (parent != NULL) { @@ -1869,6 +1871,10 @@ static void payment_finished(struct payment *p) assert(result.leafstates & PAYMENT_STEP_SUCCESS); assert(result.preimage != NULL); + /* Call any callback we might have registered. */ + if (p->on_payment_success != NULL) + p->on_payment_success(p); + ret = jsonrpc_stream_success(cmd); json_add_node_id(ret, "destination", p->destination); json_add_sha256(ret, "payment_hash", p->payment_hash); @@ -1911,6 +1917,9 @@ static void payment_finished(struct payment *p) if (command_finished(cmd, ret)) {/* Ignore result. */} return; } else if (result.failure == NULL || result.failure->failcode < NODE) { + if (p->on_payment_failure != NULL) + p->on_payment_failure(p); + /* This is failing because we have no more routes to try */ msg = tal_fmt(cmd, "Ran out of routes to try after " @@ -1929,6 +1938,8 @@ static void payment_finished(struct payment *p) } else { struct payment_result *failure = result.failure; assert(failure!= NULL); + if (p->on_payment_failure != NULL) + p->on_payment_failure(p); ret = jsonrpc_stream_fail(cmd, failure->code, failure->message); diff --git a/plugins/libplugin-pay.h b/plugins/libplugin-pay.h index 2daf1f73f..92566b745 100644 --- a/plugins/libplugin-pay.h +++ b/plugins/libplugin-pay.h @@ -298,6 +298,11 @@ struct payment { /* A human readable error message that is used as a top-level * explanation if a payment is aborted. */ char *aborterror; + + /* Callback to be called when the entire payment process + * completes successfully. */ + void (*on_payment_success)(struct payment *p); + void (*on_payment_failure)(struct payment *p); }; struct payment_modifier { diff --git a/plugins/pay.c b/plugins/pay.c index da497732b..acf2f9b8c 100644 --- a/plugins/pay.c +++ b/plugins/pay.c @@ -1936,6 +1936,16 @@ static const char *init(struct plugin *p, return NULL; } +static void on_payment_success(struct payment *payment) +{ + return; +} + +static void on_payment_failure(struct payment *payment) +{ + return; +} + /* We are interested in any prior attempts to pay this payment_hash / * invoice so we can set the `groupid` correctly and ensure we don't * already have a pending payment running. We also collect the summary @@ -2043,6 +2053,8 @@ payment_listsendpays_previous(struct command *cmd, const char *buf, last_group); } p->groupid = last_group + 1; + p->on_payment_success = on_payment_success; + p->on_payment_failure = on_payment_failure; payment_start(p); return command_still_pending(cmd); }