Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added captures as embedded objects to payment #330

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/data/payments/Payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import type Chargeback from '../chargebacks/Chargeback';
import { transform as transformChargeback } from '../chargebacks/Chargeback';
import type Refund from '../refunds/Refund';
import { transform as transformRefund } from '../refunds/Refund';
import type Capture from './captures/Capture';
import { transform as transformCapture } from './captures/Capture';
import { type PaymentData } from './data';
import PaymentHelper from './PaymentHelper';

Expand All @@ -12,6 +14,7 @@ type Payment = Seal<
_embedded?: {
refunds?: Refund[];
chargebacks?: Chargeback[];
captures?: Capture[];
};
},
PaymentHelper
Expand All @@ -29,6 +32,9 @@ export function transform(networkClient: TransformingNetworkClient, input: Payme
if (input._embedded.refunds != undefined) {
_embedded.refunds = input._embedded.refunds.map(transformRefund.bind(undefined, networkClient));
}
if (input._embedded.captures != undefined) {
_embedded.captures = input._embedded.captures.map(transformCapture.bind(undefined, networkClient));
}
}
return Object.assign(Object.create(new PaymentHelper(networkClient, input._links, _embedded)), input, { _embedded });
}
6 changes: 5 additions & 1 deletion src/data/payments/PaymentHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,11 @@ export default class PaymentHelper extends Helper<PaymentData, Payment> {
* @since 3.6.0
*/
public getCaptures(parameters?: ThrottlingParameter): HelpfulIterator<Capture> {
return runIf(this.links.captures, ({ href }) => this.networkClient.iterate<CaptureData, Capture>(href, 'captures', undefined, parameters?.valuesPerMinute)) ?? emptyHelpfulIterator;
return (
runIf(this.embedded?.captures, captures => new HelpfulIterator(makeAsync(captures[Symbol.iterator]()))) ??
runIf(this.links.captures, ({ href }) => this.networkClient.iterate<CaptureData, Capture>(href, 'captures', undefined, parameters?.valuesPerMinute)) ??
emptyHelpfulIterator
);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/data/payments/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type ChargebackData } from '../chargebacks/Chargeback';
import { type Address, type Amount, type ApiMode, type CardAudience, type CardFailureReason, type CardLabel, type FeeRegion, type HistoricPaymentMethod, type Links, type Locale, type PaymentMethod, type SequenceType, type Url } from '../global';
import type Model from '../Model';
import { type RefundData } from '../refunds/data';
import { type CaptureData } from './captures/data'

export interface PaymentData extends Model<'payment'> {
/**
Expand Down Expand Up @@ -254,6 +255,7 @@ export interface PaymentData extends Model<'payment'> {
_embedded?: {
refunds?: Omit<RefundData, '_embedded'>[];
chargebacks?: Omit<ChargebackData, '_embedded'>[];
captures?: Omit<CaptureData, '_embedded'>[];
};
}

Expand Down Expand Up @@ -849,6 +851,7 @@ export type PaymentInclude = 'details.qrCode';
export enum PaymentEmbed {
refunds = 'refunds',
chargebacks = 'chargebacks',
captures = 'captures',
}

export interface GiftCard {
Expand Down