{"id":20305707,"url":"https://github.com/bitpay/jsonpaymentprotocol","last_synced_at":"2025-04-07T08:19:47.812Z","repository":{"id":41541797,"uuid":"119898044","full_name":"bitpay/jsonPaymentProtocol","owner":"bitpay","description":"JSON Payment Protocol Interface","archived":false,"fork":false,"pushed_at":"2024-04-09T00:07:55.000Z","size":282,"stargazers_count":47,"open_issues_count":16,"forks_count":58,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-03-31T05:07:23.272Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bitpay.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"securityUpdates.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-01T21:50:56.000Z","updated_at":"2024-10-27T13:20:27.000Z","dependencies_parsed_at":"2024-11-24T18:00:20.325Z","dependency_job_id":null,"html_url":"https://github.com/bitpay/jsonPaymentProtocol","commit_stats":{"total_commits":44,"total_committers":5,"mean_commits":8.8,"dds":0.5454545454545454,"last_synced_commit":"24014a7d29a0fac8a47523843aaafd5b7e07b901"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitpay%2FjsonPaymentProtocol","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitpay%2FjsonPaymentProtocol/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitpay%2FjsonPaymentProtocol/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitpay%2FjsonPaymentProtocol/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bitpay","download_url":"https://codeload.github.com/bitpay/jsonPaymentProtocol/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247615385,"owners_count":20967184,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-14T17:09:28.332Z","updated_at":"2025-04-07T08:19:47.786Z","avatar_url":"https://github.com/bitpay.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"### JSON Payment Protocol Interface v2\n\nThis is the second version of the JSON payment protocol interface. If you have questions about the v2 specification itself, [view the documentation](v2/specification.md).\n\n[If you have questions about the first version of the specification view the documentation](v1/specification.md).\n\n\n### Getting Started with v2\n\n`npm install json-payment-protocol`\n\n### v2 Usage\n\nThis library is now using async await structure for all functions. Be careful to follow the notes about when to broadcast your payment.\nBroadcasting a payment before getting a success notification back from the server in most cases will lead to a failed payment for the sender.\nThe sender will bear the cost of paying transaction fees yet again to get their money back.\n \n#### Example\n```js\nconst JsonPaymentProtocol = require('json-payment-protocol');\n\n// Options such as additional headers, etc which you want to pass to the node https client on every request\nconst requestOptions = {};\n\nconst trustedKeys = {\n  'mh65MN7drqmwpCRZcEeBEE9ceQCQ95HtZc': {\n    // This is displayed to the user, somewhat like the organization field on an SSL certificate\n    owner: 'BitPay (TESTNET ONLY - DO NOT TRUST FOR ACTUAL BITCOIN)',\n    // Which domains this key is valid for\n    domains: ['test.bitpay.com'],\n    // The actual public key which should be used to validate the signatures\n    publicKey: '03159069584176096f1c89763488b94dbc8d5e1fa7bf91f50b42f4befe4e45295a',\n  }\n};\n\nconst client = new JsonPaymentProtocol(requestOptions, trustedKeys);\n\n\nlet requestUrl = 'bitcoin:?r=https://test.bitpay.com/i/Jr629pwsXKdTCneLyZja4t';\n\nconst paymentOptions = await client.getPaymentOptions(requestUrl);\n\n// The paymentOptions response will contain one or more currency / chain options. If you are a multi-currency wallet then you should\n// display the compatible payment options to the user. If only one option is supported it is \n\nconst { responseData: paymentRequest } = await client.selectPaymentOption(paymentOptions.requestUrl, userChoice.chain, userChoice.currency);\n\n// Parse response data instructions and create an appropriate unsigned and signed transaction\n// This is pseudocode\nlet unsignedTransaction = await myWallet.createTransaction(responseData);\nlet signedTransaction = await myWallet.signTransaction(unsignedTransaction);\n\n// We send the unsigned transaction(s) first with their size to verify if this payment will be accepted\ntry {\n  await client.verifyUnsignedPayment({\n    paymentUrl: paymentOptions.requestUrl,\n    chain: userChoice.chain,\n    // For chains which can support multiple currencies via tokens, a currency code is required to identify which token is being used\n    currency: userChoice.currency,\n    unsignedTransactions: [{\n      tx: unsignedTransaction.rawHex,\n      // `vsize` for bitcoin core w/ segwit support, `size` for other clients\n      weightedSize: signedTransaction.vsize || signedTransaction.size\n    }]\n  });\n} catch (e) {\n  // If an error occurs here, it is most likely an issue with the transaction (insufficient fee, rbf, unconfirmed inputs, etc)\n  // It could also be a network error or the invoice may no longer be accepting payments (already paid or expired)\n  return console.log('Error verifying payment with server', e);\n}\n\n// If the payment is valid we send the signed payment\ntry {\n  await client.sendSignedPayment({\n    paymentUrl: paymentOptions.requestUrl,\n    chain: choice.chain,\n    currency: choice.currency,\n    signedTransactions: [{\n      tx: signedTransaction,\n      // `vsize` for bitcoin core w/ segwit support, `size` for other clients\n      weightedSize: signedTransaction.vsize || signedTransaction.size\n    }]\n  });\n  await broadcastP2P(signedTransaction);\n  console.log('Payment successfully sent');\n} catch (e) {\n  console.log('Error sending payment', e);\n}\n```\n\n### Options\n\nOptions passed to `new JsonPaymentProtocol()` are passed to request, so if you need to use a proxy or set any other request.js flags you can do so by including them when instantiating your instance. For example:\n\n```js\nnew JsonPaymentProtocol({\n  proxy: 'socks://mySocksProxy.local',\n  headers: {\n    'user-agent': 'myWallet'\n  }\n})\n```\n\n### URI Formats\nYou can provide either the `bitcoin:?r=https://bitpay.com/i/invoice` format or `https://bitpay.com/i/invoice` directly.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbitpay%2Fjsonpaymentprotocol","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbitpay%2Fjsonpaymentprotocol","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbitpay%2Fjsonpaymentprotocol/lists"}