Skip to content

Commit

Permalink
Made changes for other payment networks
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Mar 12, 2020
1 parent 3d84e57 commit 38f8020
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,30 +105,27 @@ const ethInputDataCreateParams = {
})();

/**
* ### Paying an ether request with Request payment processor
* ## Checking balance
*
* Requests using ether payment network can be paid with the Request payment processor
* The function getData() of a request provides its balance
*/

// Import necessary packages
import { hasSufficientFunds, payRequest } from '@requestnetwork/payment-processor';
import { Wallet } from 'ethers';

// Create a wallet for the payer for demo purpose
const wallet = Wallet.createRandom();

(async () => {
const request = await requestNetwork.createRequest(ethInputDataCreateParams);

// Check the payer has sufficient fund for the data
const payerAddress = wallet.address;
const requestData = request.getData();
if (!(await hasSufficientFunds(requestData, payerAddress))) {
throw new Error('You do not have enough funds to pay this request');
}

// Pay the request
// The value provided for wallet can be a Web3Provider from 'ethers/providers' to be able to pay with Metamask
const tx = await payRequest(requestData, wallet);
await tx.wait(1);
})();
// Import Big Number package
const BN = require('bn.js')

(async () => {
const request = await requestNetwork.createRequest(ethInputDataCreateParams);

// Check the balance of the request
const requestData = request.getData();
const balance = requestData.balance;
console.log(`Balance of the ether input data request: ${balance}`);

// Check if the request has been paid
// Convert the balance to big number type for comparison
const expectedAmount = new BN(requestData.expectedAmount);
const balanceBigNumber = new BN(balance);

// Check if balanceBigNumber is greater or equal to expectedAmount
const paid = balanceBigNumber.gte(expectedAmount);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,28 @@ const bitcoinAddressBasedCreateParams = {
console.log(`Request created with Bitcoin payment network: ${request.requestId}`);
})();

/**
* ### Paying a Bitcoin request
/**
* ## Checking balance
*
* The Request payment processor package doesn't support the payment of Bitcoin request.
* The Bitcoin payment network is of type "Address Based", therefore, the payer can simply pay the request by sending the right amount of BTC to the payment address.
* The function getData() of a request provides its balance
*/

// Import Big Number package
const BN = require('bn.js')

(async () => {
const request = await requestNetwork.createRequest(bitcoinAddressBasedCreateParams);

// Check the balance of the request
const requestData = request.getData();
const balance = requestData.balance;
console.log(`Balance of the bitcoin address based request: ${balance}`);

// Check if the request has been paid
// Convert the balance to big number type for comparison
const expectedAmount = new BN(requestData.expectedAmount);
const balanceBigNumber = new BN(balance);

// Check if balanceBigNumber is greater or equal to expectedAmount
const paid = balanceBigNumber.gte(expectedAmount);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,15 @@ const declarativeCreateParams = {
})();

/**
* ### Declaring sent and received payments
* ## Declaring sent and received payments and checking balance
*
* The Declarative payment network doesn't provide payment detection method to determine the balance of the request
* The balance of the request is defined by the declared payments by the payee and the declared refunds by the payer
*/

// Import Big Number package
const BN = require('bn.js')

(async () => {
const request = await requestNetwork.createRequest(declarativeCreateParams);

Expand All @@ -133,5 +136,17 @@ const declarativeCreateParams = {

// Declaring a sent refund, this amount is not taken into account for the request balance
request.declareSentPayment('900', 'received too much', payeeIdentity);
})();

// Check the balance of the request
const requestData = request.getData();
const balance = requestData.balance;
console.log(`Balance of the declarative request: ${balance}`);

// Check if the request has been paid
// Convert the balance to big number type for comparison
const expectedAmount = new BN(requestData.expectedAmount);
const balanceBigNumber = new BN(balance);

// Check if balanceBigNumber is greater or equal to expectedAmount
const paid = balanceBigNumber.gte(expectedAmount);
})();

0 comments on commit 38f8020

Please sign in to comment.