|
| 1 | +# Receiving payments using LNURL-Pay |
| 2 | + |
| 3 | +Breez SDK - Native *(Greenlight Implementation)* users have the ability to receive Lightning payments using [LNURL-Pay](https://github.com/lnurl/luds/blob/luds/06.md). |
| 4 | + |
| 5 | +LNURL-Pay requires a web service that serves LNURL-Pay requests. This service needs to communicate with the SDK in order to fetch the necessary metadata data and the associated payment request. |
| 6 | +To interact with the SDK, the service uses a simple protocol over push notifications: |
| 7 | +* The service sends a push notification to the user's mobile app with the LNURL-Pay request and a reply URL. |
| 8 | +* The app responds to reply URL with the required data. |
| 9 | +* The service forwards the data to the payer. |
| 10 | + |
| 11 | +## General workflow |
| 12 | +The following workflow is application specific and the steps detailed below refer to the misty-breez wallet implementation which requires running <b>[breez-lnurl](https://github.com/breez/breez-lnurl) </b>service. |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +### Step 1: Registering for an LNURL-Pay service |
| 17 | +Use a POST request to the service endpoint ```https://app.domain/lnurlpay/[pubkey]``` with the following payload to register the app for an LNURL-Pay service: |
| 18 | + |
| 19 | +```json |
| 20 | +{ |
| 21 | + "time": 1231006505, // Current UNIX timestamp |
| 22 | + "webhook_url": "[notification service webhook URL]", |
| 23 | + "username": "[optional username]", |
| 24 | + "signature": "[signed message]" |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +The `signature` refers to the result of a message signed by the private key of the `pubkey`, where the message is comprised of the following text: |
| 29 | + |
| 30 | +``` |
| 31 | +[time]-[webhook_url] |
| 32 | +``` |
| 33 | +or, when the optional `username` field is set: |
| 34 | +``` |
| 35 | +[time]-[webhook_url]-[username] |
| 36 | +``` |
| 37 | +where `time`, `webhook_url` and `username` are the payload fields. |
| 38 | + |
| 39 | +The service responds with following payload: |
| 40 | +```json |
| 41 | +{ |
| 42 | + "lnurl": "[LNURL-pay encoded endpoint]", |
| 43 | + "lightning_address": "username@app.domain" // Only set when username is included |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +<div class="warning"> |
| 48 | +<h4>Developer note</h4> |
| 49 | + |
| 50 | +When a user changes their already registered username, this previous username is freely available to be registered by another user. |
| 51 | + |
| 52 | +</div> |
| 53 | + |
| 54 | +### Step 2: Processing an LNURL-Pay request |
| 55 | +When an LNURL-Pay GET request is received at ```https://app.domain/lnurlp/[identifier]``` (or ```https://app.domain/.well-known/lnurlp/[identifier]``` for lightning addresses) the service then sends a push notification to the app with the LNURL-Pay request and a callback URL. The payload may look like the following: |
| 56 | + |
| 57 | +```json |
| 58 | +{ |
| 59 | + "template": "lnurlpay_info", |
| 60 | + "data": { |
| 61 | + "reply_url": "https://app.domain/respond/[request_id]" |
| 62 | + "callback_url": "https://app.domain/lnurlpay/[identifier]/invoice" |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +The ```reply_url``` is used by the app to respond to the LNURL-Pay request. |
| 68 | +The ```callback_url``` is the LNURL-Pay callback URL, used by the payer to fetch the invoice. |
| 69 | + |
| 70 | +### Step 3: Responding to the callback url |
| 71 | +When the app receives the push notification, it parses the payload and then uses the ```reply_url``` to respond with the required data, for example: |
| 72 | + |
| 73 | +```json |
| 74 | +{ |
| 75 | + "callback": "https://app.domain/lnurlpay/[identifier]/invoice", |
| 76 | + "maxSendable": 10000, |
| 77 | + "minSendable": 1000, |
| 78 | + "metadata": "[[\"text/plain\",\"Pay to Breez\"]]", |
| 79 | + "tag": "payRequest" |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +The service receives the response from the app and forwards it to the sender. |
| 84 | + |
| 85 | +### Step 4: Fetching a bolt11 invoice |
| 86 | + |
| 87 | +The sender fetches a bolt11 invoice by invoking a GET request to the ```callback``` URL when a specific amount is added as a query parameter. For example: |
| 88 | +``` |
| 89 | +https://app.domain/lnurlpay/[identifier]/invoice?amount=1000 |
| 90 | +``` |
| 91 | +An additional push notification is triggered to send the invoice request to the app. Then the app responds with the bolt11 invoice data. |
| 92 | + |
| 93 | +### Step 5: Paying the invoice |
| 94 | +In the last step, the payer pays the received bolt11 invoice. Follow the steps [here](/notifications/getting_started.md) to receive payments via push notifications. |
| 95 | + |
| 96 | +## Reference implementation |
| 97 | +For a complete reference implementation, see: |
| 98 | +* [Breez's NotificationService](https://github.com/breez/c-breez/blob/main/ios/Breez%20Notification%20Service%20Extension/NotificationService.swift) |
| 99 | +* [Breez's LNURL-Pay service](https://github.com/breez/breez-lnurl) |
0 commit comments