Skip to content

Commit

Permalink
fix(core): Correctly update Refund state
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Aug 1, 2019
1 parent 8c5ff50 commit 58caba7
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions packages/core/src/service/services/payment-method.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Injectable } from '@nestjs/common';
import { InjectConnection } from '@nestjs/typeorm';
import { ConfigArg, ConfigArgType, RefundOrderInput, UpdatePaymentMethodInput } from '@vendure/common/lib/generated-types';
import {
ConfigArg,
ConfigArgType,
RefundOrderInput,
UpdatePaymentMethodInput,
} from '@vendure/common/lib/generated-types';
import { omit } from '@vendure/common/lib/omit';
import { ID, PaginatedList } from '@vendure/common/lib/shared-types';
import { assertNever } from '@vendure/common/lib/shared-utils';
Expand Down Expand Up @@ -70,10 +75,17 @@ export class PaymentMethodService {
return this.connection.getRepository(PaymentMethod).save(updatedPaymentMethod);
}

async createPayment(ctx: RequestContext, order: Order, method: string, metadata: PaymentMetadata): Promise<Payment> {
async createPayment(
ctx: RequestContext,
order: Order,
method: string,
metadata: PaymentMetadata,
): Promise<Payment> {
const { paymentMethod, handler } = await this.getMethodAndHandler(method);
const result = await handler.createPayment(order, paymentMethod.configArgs, metadata || {});
const payment = await this.connection.getRepository(Payment).save(new Payment({ ...result, state: 'Created' }));
const payment = await this.connection
.getRepository(Payment)
.save(new Payment({ ...result, state: 'Created' }));
await this.paymentStateMachine.transition(ctx, order, payment, result.state);
await this.connection.getRepository(Payment).save(payment);
return payment;
Expand All @@ -84,7 +96,13 @@ export class PaymentMethodService {
return handler.settlePayment(order, payment, paymentMethod.configArgs);
}

async createRefund(ctx: RequestContext, input: RefundOrderInput, order: Order, items: OrderItem[], payment: Payment): Promise<Refund> {
async createRefund(
ctx: RequestContext,
input: RefundOrderInput,
order: Order,
items: OrderItem[],
payment: Payment,
): Promise<Refund> {
const { paymentMethod, handler } = await this.getMethodAndHandler(payment.method);
const itemAmount = items.reduce((sum, item) => sum + item.unitPriceWithTax, 0);
const refundAmount = itemAmount + input.shipping + input.adjustment;
Expand All @@ -100,19 +118,28 @@ export class PaymentMethodService {
state: 'Pending',
metadata: {},
});
const createRefundResult = await handler.createRefund(input, refundAmount, order, payment, paymentMethod.configArgs);
const createRefundResult = await handler.createRefund(
input,
refundAmount,
order,
payment,
paymentMethod.configArgs,
);
if (createRefundResult) {
refund.transactionId = createRefundResult.transactionId || '';
refund.metadata = createRefundResult.metadata || {};
}
refund = await this.connection.getRepository(Refund).save(refund);
if (createRefundResult) {
await this.refundStateMachine.transition(ctx, order, refund, createRefundResult.state);
await this.connection.getRepository(Refund).save(refund);
}
return refund;
}

private async getMethodAndHandler(method: string): Promise<{ paymentMethod: PaymentMethod, handler: PaymentMethodHandler }> {
private async getMethodAndHandler(
method: string,
): Promise<{ paymentMethod: PaymentMethod; handler: PaymentMethodHandler }> {
const paymentMethod = await this.connection.getRepository(PaymentMethod).findOne({
where: {
code: method,
Expand All @@ -122,7 +149,9 @@ export class PaymentMethodService {
if (!paymentMethod) {
throw new UserInputError(`error.payment-method-not-found`, { method });
}
const handler = this.configService.paymentOptions.paymentMethodHandlers.find(h => h.code === paymentMethod.code);
const handler = this.configService.paymentOptions.paymentMethodHandlers.find(
h => h.code === paymentMethod.code,
);
if (!handler) {
throw new UserInputError(`error.no-payment-handler-with-code`, { code: paymentMethod.code });
}
Expand Down Expand Up @@ -167,7 +196,10 @@ export class PaymentMethodService {
await this.connection.getRepository(PaymentMethod).remove(toRemove);
}

private buildConfigArgsArray(handler: PaymentMethodHandler, existingConfigArgs: ConfigArg[]): ConfigArg[] {
private buildConfigArgsArray(
handler: PaymentMethodHandler,
existingConfigArgs: ConfigArg[],
): ConfigArg[] {
let configArgs: ConfigArg[] = [];
for (const [name, type] of Object.entries(handler.args)) {
if (!existingConfigArgs.find(ca => ca.name === name)) {
Expand All @@ -178,9 +210,7 @@ export class PaymentMethodService {
});
}
}
configArgs = configArgs.filter(ca =>
handler.args.hasOwnProperty(ca.name),
);
configArgs = configArgs.filter(ca => handler.args.hasOwnProperty(ca.name));
return [...existingConfigArgs, ...configArgs];
}

Expand Down

0 comments on commit 58caba7

Please sign in to comment.