Skip to content

Commit

Permalink
Merge pull request #330 from mollie/JoostMollie-Embedded-Captures
Browse files Browse the repository at this point in the history
Added captures as embedded objects to payment.
  • Loading branch information
Pimm authored Sep 20, 2023
2 parents 53132cf + 81de71c commit 3761948
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
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

0 comments on commit 3761948

Please sign in to comment.