Skip to content

Commit 26a4483

Browse files
authored
feat: implement the invoicing format (#101)
1 parent a6cbce8 commit 26a4483

File tree

6 files changed

+74
-34
lines changed

6 files changed

+74
-34
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@requestnetwork/web-components",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"private": true,
55
"description": "Request Network Web Components",
66
"main": "index.js",

packages/payment-widget/src/lib/components/payment-confirmation.svelte

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
1818
export let selectedCurrency: Currency;
1919
export let amountInUSD: number;
20+
export let sellerName: string | undefined;
21+
export let productName: string | undefined;
2022
export let sellerAddress: string;
2123
export let currentPaymentStep: PaymentStep;
2224
export let web3Modal: Web3Modal | null;
@@ -200,6 +202,8 @@
200202
try {
201203
const requestParameters = prepareRequestParameters({
202204
currency: selectedCurrency,
205+
productName,
206+
sellerName,
203207
sellerAddress,
204208
payerAddress,
205209
amountInCrypto,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/// <reference types="vite/client" />
22

33
interface ImportMetaEnv {
4-
readonly WEB3MODAL_PROJECT_ID: string
4+
readonly VITE_WEB3MODAL_PROJECT_ID: string;
55
}
66

77
interface ImportMeta {
8-
readonly env: ImportMetaEnv
8+
readonly env: ImportMetaEnv;
99
}

packages/payment-widget/src/lib/payment-widget.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@
194194
/>
195195
{:else if selectedCurrency && currentPaymentStep === "confirmation"}
196196
<PaymentConfirmation
197+
sellerName={sellerInfo.name}
198+
productName={productInfo.name}
197199
{amountInUSD}
198200
{sellerAddress}
199201
{web3Modal}

packages/payment-widget/src/lib/utils/request.ts

Lines changed: 64 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export const prepareRequestParameters = ({
2323
amountInUSD,
2424
createdWith,
2525
builderId,
26+
productName,
27+
sellerName,
2628
}: {
2729
currency: Currency;
2830
sellerAddress: string;
@@ -32,20 +34,22 @@ export const prepareRequestParameters = ({
3234
amountInUSD: number;
3335
builderId: string;
3436
createdWith: string;
37+
productName: string | undefined;
38+
sellerName: string | undefined;
3539
}) => {
3640
const isERC20 = currency.type === Types.RequestLogic.CURRENCY.ERC20;
3741
const currencyValue = isERC20 ? currency.address : "eth";
38-
42+
const amount = utils
43+
.parseUnits(amountInCrypto.toFixed(currency.decimals), currency.decimals)
44+
.toString();
3945
return {
4046
requestInfo: {
4147
currency: {
4248
type: currency.type,
4349
value: currencyValue,
4450
network: currency.network,
4551
},
46-
expectedAmount: utils
47-
.parseUnits(amountInCrypto.toString(), currency.decimals)
48-
.toString(),
52+
expectedAmount: amount,
4953
payee: {
5054
type: Types.Identity.TYPE.ETHEREUM_ADDRESS,
5155
value: sellerAddress,
@@ -69,15 +73,44 @@ export const prepareRequestParameters = ({
6973
},
7074
},
7175
contentData: {
72-
paymentCurrency: {
73-
type: currency.type,
74-
value: currencyValue,
75-
network: currency.network,
76+
meta: {
77+
format: "rnf_invoice",
78+
version: "0.0.3",
79+
},
80+
creationDate: new Date().toISOString(),
81+
invoiceNumber: "rn-checkout",
82+
note: `Sale made with ${currency.symbol} on ${currency.network} for amount of ${amountInUSD} USD with an exchange rate of ${exchangeRate}`,
83+
invoiceItems: [
84+
{
85+
name: productName || "",
86+
quantity: 1,
87+
unitPrice: amount,
88+
discount: "0",
89+
tax: {
90+
type: "percentage",
91+
amount: "0",
92+
},
93+
currency: currencyValue,
94+
},
95+
],
96+
97+
paymentTerms: {
98+
dueDate: new Date().toISOString(),
99+
},
100+
sellerInfo: {
101+
businessName: sellerName || undefined,
102+
},
103+
miscellaneous: {
104+
exchangeRate: exchangeRate.toString(),
105+
amountInUSD: amountInUSD.toString(),
106+
createdWith,
107+
builderId,
108+
paymentCurrency: {
109+
type: currency.type,
110+
value: currencyValue,
111+
network: currency.network,
112+
},
76113
},
77-
exchangeRate: exchangeRate.toString(),
78-
amountInUSD: amountInUSD.toString(),
79-
createdWith,
80-
builderId,
81114
},
82115
signer: {
83116
type: Types.Identity.TYPE.ETHEREUM_ADDRESS,
@@ -164,7 +197,7 @@ export const handleRequestPayment = async ({
164197
await inMemoryRequestNetwork.createRequest(requestParameters);
165198

166199
const signer = await ethersProvider!.getSigner();
167-
const confirmationBlocks = getConfirmations(targetChain!.chainId);
200+
const confirmationBlocks = 1;
168201
if (isERC20) {
169202
const requestData = inMemoryRequest.inMemoryInfo?.requestData!;
170203

@@ -217,25 +250,26 @@ export const handleRequestPayment = async ({
217250

218251
function getChainFromNetwork(network: string): (typeof chains)[0] | undefined {
219252
const networkLower = network.toLowerCase();
220-
return chains.find(
221-
(chain) =>
222-
chain.name.toLowerCase() === networkLower ||
223-
chain.currency.toLowerCase() === networkLower
224-
);
225-
}
226-
227-
const getConfirmations = (chainId: number): number => {
228-
switch (chainId) {
229-
case 137: // Polygon
230-
return 15;
231-
case 56: // Binance Smart Chain
232-
case 43114: // Avalanche
233-
case 250: // Fantom
234-
return 5;
253+
switch (networkLower) {
254+
case "mainnet":
255+
case "ethereum":
256+
return chains.find((chain) => chain.name.toLowerCase() === "ethereum");
257+
case "bsc":
258+
case "binance smart chain":
259+
return chains.find(
260+
(chain) => chain.name.toLowerCase() === "binance smart chain"
261+
);
262+
case "zksyncera":
263+
case "zksync era":
264+
return chains.find((chain) => chain.name.toLowerCase() === "zksync era");
235265
default:
236-
return 2;
266+
return chains.find(
267+
(chain) =>
268+
chain.name.toLowerCase() === networkLower ||
269+
chain.currency.toLowerCase() === networkLower
270+
);
237271
}
238-
};
272+
}
239273

240274
function getNetworkParams(chain: (typeof chains)[0]): any {
241275
return {

packages/payment-widget/src/lib/utils/walletConnector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { chains } from "./chains";
22
import { createWeb3Modal, defaultConfig } from "@web3modal/ethers5";
33

44
export const initWalletConnector = () => {
5-
const projectId = import.meta.env.WEB3MODAL_PROJECT_ID;
5+
const projectId = import.meta.env.VITE_WEB3MODAL_PROJECT_ID;
66

77
const metadata = {
88
name: "Request Checkout",

0 commit comments

Comments
 (0)