Skip to content

Commit

Permalink
feat(accept-blue): return declined instead of throwing
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnvdbrug committed Nov 21, 2024
1 parent f456ceb commit 529ae1c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/vendure-plugin-accept-blue/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pinelab/vendure-plugin-accept-blue",
"version": "1.8.1",
"version": "1.9.0",
"description": "Vendure plugin for creating subscriptions with the Accept Blue platform",
"author": "Martijn van de Brug <[email protected]>",
"homepage": "https://pinelab-plugins.com/",
Expand Down
4 changes: 2 additions & 2 deletions packages/vendure-plugin-accept-blue/src/accept-blue-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PluginCommonModule, VendurePlugin } from '@vendure/core';
import { PluginCommonModule, Type, VendurePlugin } from '@vendure/core';
import { SubscriptionStrategy } from '../../util/src/subscription/subscription-strategy';
import { AcceptBlueService } from './api/accept-blue-service';
import { acceptBluePaymentHandler } from './api/accept-blue-handler';
Expand Down Expand Up @@ -61,7 +61,7 @@ export type AcceptBluePluginOptions = Required<AcceptBluePluginOptionsInput>;
export class AcceptBluePlugin {
static options: AcceptBluePluginOptions;

static init(options: AcceptBluePluginOptionsInput): AcceptBluePlugin {
static init(options: AcceptBluePluginOptionsInput): Type<AcceptBluePlugin> {
let vendureHost = options.vendureHost;
// Strip trailing slash
if (vendureHost.endsWith('/')) {
Expand Down
58 changes: 36 additions & 22 deletions packages/vendure-plugin-accept-blue/src/api/accept-blue-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from '../util';
import { AcceptBlueClient } from './accept-blue-client';
import { AcceptBlueService } from './accept-blue-service';
import { asError } from 'catch-unknown';

let service: AcceptBlueService;
export const acceptBluePaymentHandler = new PaymentMethodHandler({
Expand Down Expand Up @@ -106,28 +107,41 @@ export const acceptBluePaymentHandler = new PaymentMethodHandler({
| NoncePaymentMethodInput
| SavedPaymentMethodInput;
const client = new AcceptBlueClient(args.apiKey, args.pin, args.testMode);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
const result = await service.handlePaymentForOrder(
ctx,
order,
amount,
client,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
input
);
const chargeTransactionId = result.chargeResult?.transaction?.id;
Logger.info(
`Settled payment for order '${order.code}', for Accept Blue customer '${result.customerId}' and one time charge transaction '${chargeTransactionId}'`,
loggerCtx
);
return {
amount,
state: 'Settled',
transactionId: chargeTransactionId
? String(chargeTransactionId)
: undefined,
metadata: result,
};
try {
const result = await service.handlePaymentForOrder(
ctx,
order,
amount,
client,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
input
);
const chargeTransactionId = result.chargeResult?.transaction?.id;
Logger.info(
`Settled payment for order '${order.code}', for Accept Blue customer '${result.customerId}' and one time charge transaction '${chargeTransactionId}'`,
loggerCtx
);
return {
amount,
state: 'Settled',
transactionId: chargeTransactionId
? String(chargeTransactionId)
: undefined,
metadata: result,
};
} catch (e) {
const error = asError(e);
Logger.error(
`Error settling payment for order '${order.code}': ${error.message}`,
loggerCtx,
error.stack
);
return {
amount,
state: 'Declined',
errorMessage: error.message,
};
}
},
settlePayment(): SettlePaymentResult {
return {
Expand Down
1 change: 1 addition & 0 deletions packages/vendure-plugin-accept-blue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './api/accept-blue-client';
export * from './api/accept-blue-transaction-event';
export * from './api/accept-blue-handler';
export * from './api/accept-blue-common-resolvers';
export * from './api/custom-field-types';
1 change: 1 addition & 0 deletions packages/vendure-plugin-accept-blue/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export function getDaysBetweenBillingCycles(frequency: Frequency): number {
case 'biannually':
return 730;
default:
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- prevent runtime errors, even though this shouldnt be possible TS wise
throw Error(`Frequency '${frequency}' is not a valid frequency`);
}
}
Expand Down

0 comments on commit 529ae1c

Please sign in to comment.