Skip to content

Commit

Permalink
resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
volodymyr-basiuk committed Nov 1, 2024
1 parent 9e36bc0 commit 119ad86
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
46 changes: 20 additions & 26 deletions src/iden3comm/handlers/payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import {
MultiChainPaymentConfig,
PaymentMessage,
PaymentRequestInfo,
PaymentRequestMessage
PaymentRequestMessage,
PaymentRequestTypeUnion,
PaymentTypeUnion
} from '../types/protocol/payment';
import {
PaymentFeatures,
Expand Down Expand Up @@ -82,7 +84,7 @@ export type PaymentRailsChainInfo = {
amount: bigint;
currency: SupportedCurrencies | string;
chainId: string;
expirationDate?: Date;
expirationDate?: string;
features?: PaymentFeatures[];
type:
| PaymentRequestDataType.Iden3PaymentRailsRequestV1
Expand All @@ -100,7 +102,7 @@ export type PaymentRailsChainInfo = {
export function createPayment(
sender: DID,
receiver: DID,
payments: (Iden3PaymentCryptoV1 | Iden3PaymentRailsV1 | Iden3PaymentRailsERC20V1)[]
payments: PaymentTypeUnion[]
): PaymentMessage {
const uuidv4 = uuid.v4();
const request: PaymentMessage = {
Expand Down Expand Up @@ -174,9 +176,7 @@ export interface IPaymentHandler {

/** @beta PaymentRequestMessageHandlerOptions represents payment-request handler options */
export type PaymentRequestMessageHandlerOptions = {
paymentHandler: (
data: Iden3PaymentRequestCryptoV1 | Iden3PaymentRailsRequestV1 | Iden3PaymentRailsERC20RequestV1
) => Promise<string>;
paymentHandler: (data: PaymentRequestTypeUnion) => Promise<string>;
/*
selected payment nonce (for Iden3PaymentRequestCryptoV1 type it should be equal to Payment id field)
*/
Expand All @@ -187,10 +187,7 @@ export type PaymentRequestMessageHandlerOptions = {
/** @beta PaymentHandlerOptions represents payment handler options */
export type PaymentHandlerOptions = {
paymentRequest: PaymentRequestMessage;
paymentValidationHandler: (
txId: string,
data: Iden3PaymentRequestCryptoV1 | Iden3PaymentRailsRequestV1 | Iden3PaymentRailsERC20RequestV1
) => Promise<void>;
paymentValidationHandler: (txId: string, data: PaymentRequestTypeUnion) => Promise<void>;
};

/** @beta PaymentHandlerParams represents payment handler params */
Expand Down Expand Up @@ -277,18 +274,16 @@ export class PaymentHandler
const senderDID = DID.parse(paymentRequest.to);
const receiverDID = DID.parse(paymentRequest.from);

const payments: (Iden3PaymentCryptoV1 | Iden3PaymentRailsV1 | Iden3PaymentRailsERC20V1)[] = [];
const payments: PaymentTypeUnion[] = [];
for (let i = 0; i < paymentRequest.body.payments.length; i++) {
const paymentReq = paymentRequest.body.payments[i];
const dataArray = Array.isArray(paymentReq.data) ? paymentReq.data : [paymentReq.data];
const selectedPayment =
dataArray.length === 1
? dataArray[0]
: dataArray.find((p) => {
return p.type === PaymentRequestDataType.Iden3PaymentRequestCryptoV1
? p.id === ctx.nonce
: p.nonce === ctx.nonce;
});
const { data } = paymentRequest.body.payments[i];
const selectedPayment = Array.isArray(data)
? data.find((p) => {
return p.type === PaymentRequestDataType.Iden3PaymentRequestCryptoV1
? p.id === ctx.nonce
: p.nonce === ctx.nonce;
})
: data;

if (!selectedPayment) {
throw new Error(`failed request. no payment in request for nonce ${ctx.nonce}`);
Expand Down Expand Up @@ -441,9 +436,8 @@ export class PaymentHandler
if (type === PaymentRequestDataType.Iden3PaymentRailsERC20RequestV1 && !tokenAddress) {
throw new Error(`failed request. no token address for currency ${currency}`);
}
const expiration = expirationDate
? expirationDate.getTime()
: new Date(new Date().setHours(new Date().getHours() + 1)).getTime();
const expiration =
expirationDate ?? new Date(new Date().setHours(new Date().getHours() + 1)).toISOString();
const typeUrl = `https://schema.iden3.io/core/json/${type}.json`;
const typesFetchResult = await fetch(typeUrl);
const types = await typesFetchResult.json();
Expand Down Expand Up @@ -499,7 +493,7 @@ export class PaymentHandler
recipient,
amount: amount.toString(),
currency,
expirationDate: new Date(expiration).toISOString(),
expirationDate: expiration,
nonce: nonce.toString(),
metadata: '0x',
proof
Expand All @@ -515,7 +509,7 @@ export class PaymentHandler
recipient,
amount: amount.toString(),
currency,
expirationDate: new Date(expiration).toISOString(),
expirationDate: expiration,
nonce: nonce.toString(),
metadata: '0x',
proof
Expand Down
20 changes: 19 additions & 1 deletion src/iden3comm/types/protocol/payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export type PaymentMessage = BasicMessage & {

/** @beta PaymentMessageBody is struct the represents body for payment message */
export type PaymentMessageBody = {
payments: (Iden3PaymentCryptoV1 | Iden3PaymentRailsV1 | Iden3PaymentRailsERC20V1)[];
payments: PaymentTypeUnion[];
};

/** @beta Iden3PaymentCryptoV1 is struct the represents payment info for payment */
Expand Down Expand Up @@ -137,3 +137,21 @@ export type MultiChainPaymentConfig = {
address: string;
}[];
};

/**
* @beta
* PaymentRequestTypeUnion is a type of supported payment request types
*/
export type PaymentRequestTypeUnion =
| Iden3PaymentRequestCryptoV1
| Iden3PaymentRailsRequestV1
| Iden3PaymentRailsERC20RequestV1;

/**
* @beta
* PaymentTypeUnion is a type of supported payment types
*/
export type PaymentTypeUnion =
| Iden3PaymentCryptoV1
| Iden3PaymentRailsV1
| Iden3PaymentRailsERC20V1;
5 changes: 3 additions & 2 deletions tests/handlers/payment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ import {
Iden3PaymentRailsERC20RequestV1,
Iden3PaymentRailsRequestV1,
Iden3PaymentRequestCryptoV1,
PaymentRequestInfo
PaymentRequestInfo,
PaymentRequestTypeUnion
} from '../../src/iden3comm/types/protocol/payment';
import { Contract, ethers, JsonRpcProvider } from 'ethers';
import fetchMock from '@gr2m/fetch-mock';
Expand Down Expand Up @@ -380,7 +381,7 @@ describe('payment-request handler', () => {

const paymentValidationIntegrationHandlerFunc = async (
txId: string,
data: Iden3PaymentRequestCryptoV1 | Iden3PaymentRailsRequestV1 | Iden3PaymentRailsERC20RequestV1
data: PaymentRequestTypeUnion
): Promise<void> => {
const rpcProvider = new JsonRpcProvider(RPC_URL);
const tx = await rpcProvider.getTransaction(txId);
Expand Down

0 comments on commit 119ad86

Please sign in to comment.