Sfoglia il codice sorgente

refactor(server): Export only a single example payment provider

Michael Bromley 7 anni fa
parent
commit
8af82d3d4b

+ 3 - 4
server/cli/assets/vendure-config.hbs

@@ -1,13 +1,12 @@
 {{#if isTs }}import{{ else }}const{{/if}} {
 {{#if isTs }}import{{ else }}const{{/if}} {
-    fakePalPaymentHandler,
-    gripePaymentHandler,
+    examplePaymentHandler,
     DefaultAssetServerPlugin,
     DefaultAssetServerPlugin,
     DefaultEmailPlugin,
     DefaultEmailPlugin,
     DefaultSearchPlugin,
     DefaultSearchPlugin,
     {{#if isTs}}VendureConfig,{{/if}}
     {{#if isTs}}VendureConfig,{{/if}}
 } {{#if isTs}}from 'vendure'; {{ else }}= require('vendure');{{/if}}
 } {{#if isTs}}from 'vendure'; {{ else }}= require('vendure');{{/if}}
 {{#if isTs }}
 {{#if isTs }}
-import path from 'path';
+import * as path from 'path';
 {{ else }}
 {{ else }}
 const path = require('path');
 const path = require('path');
 {{/if}}
 {{/if}}
@@ -33,7 +32,7 @@ const path = require('path');
         {{/if}}
         {{/if}}
     },
     },
     paymentOptions: {
     paymentOptions: {
-        paymentMethodHandlers: [fakePalPaymentHandler, gripePaymentHandler],
+        paymentMethodHandlers: [examplePaymentHandler],
     },
     },
     customFields: {},
     customFields: {},
     importExportOptions: {
     importExportOptions: {

+ 3 - 4
server/dev-config.ts

@@ -2,8 +2,7 @@ import path from 'path';
 
 
 import { API_PATH, API_PORT } from '../shared/shared-constants';
 import { API_PATH, API_PORT } from '../shared/shared-constants';
 
 
-import { fakePalPaymentHandler } from './src/config/payment-method/fakepal-payment-method-config';
-import { gripePaymentHandler } from './src/config/payment-method/gripe-payment-method-config';
+import { examplePaymentHandler } from './src/config/payment-method/example-payment-method-config';
 import { OrderProcessOptions, VendureConfig } from './src/config/vendure-config';
 import { OrderProcessOptions, VendureConfig } from './src/config/vendure-config';
 import { defaultEmailTypes } from './src/email/default-email-types';
 import { defaultEmailTypes } from './src/email/default-email-types';
 import { HandlebarsMjmlGenerator } from './src/email/handlebars-mjml-generator';
 import { HandlebarsMjmlGenerator } from './src/email/handlebars-mjml-generator';
@@ -33,11 +32,11 @@ export const devConfig: VendureConfig = {
     },
     },
     orderProcessOptions: {} as OrderProcessOptions<any>,
     orderProcessOptions: {} as OrderProcessOptions<any>,
     paymentOptions: {
     paymentOptions: {
-        paymentMethodHandlers: [fakePalPaymentHandler, gripePaymentHandler],
+        paymentMethodHandlers: [examplePaymentHandler],
     },
     },
     customFields: {},
     customFields: {},
     emailOptions: {
     emailOptions: {
-        emailTemplatePath: path.join(__dirname, 'src', 'email', 'templates'),
+        emailTemplatePath: path.join(__dirname, 'src/email/templates'),
         emailTypes: defaultEmailTypes,
         emailTypes: defaultEmailTypes,
         generator: new HandlebarsMjmlGenerator(),
         generator: new HandlebarsMjmlGenerator(),
         transport: {
         transport: {

+ 1 - 2
server/src/config/index.ts

@@ -8,8 +8,7 @@ export * from './entity-id-strategy/auto-increment-id-strategy';
 export * from './entity-id-strategy/entity-id-strategy';
 export * from './entity-id-strategy/entity-id-strategy';
 export * from './entity-id-strategy/uuid-id-strategy';
 export * from './entity-id-strategy/uuid-id-strategy';
 export * from './order-merge-strategy/order-merge-strategy';
 export * from './order-merge-strategy/order-merge-strategy';
-export * from './payment-method/fakepal-payment-method-config';
-export * from './payment-method/gripe-payment-method-config';
+export * from './payment-method/example-payment-method-config';
 export * from './payment-method/payment-method-handler';
 export * from './payment-method/payment-method-handler';
 export * from './promotion/default-promotion-actions';
 export * from './promotion/default-promotion-actions';
 export * from './promotion/default-promotion-conditions';
 export * from './promotion/default-promotion-conditions';

+ 6 - 3
server/src/config/payment-method/gripe-payment-method-config.ts → server/src/config/payment-method/example-payment-method-config.ts

@@ -1,5 +1,8 @@
 import { PaymentConfig, PaymentMethodHandler } from './payment-method-handler';
 import { PaymentConfig, PaymentMethodHandler } from './payment-method-handler';
 
 
+/**
+ * A dummy API to simulate an SDK provided by a popular payments service.
+ */
 const gripeSDK = {
 const gripeSDK = {
     charges: {
     charges: {
         create: (options: any) => {
         create: (options: any) => {
@@ -16,9 +19,9 @@ const gripeSDK = {
  * An example of a payment method which sets up and authorizes the payment on the client side and then
  * An example of a payment method which sets up and authorizes the payment on the client side and then
  * requires a further step on the server side to charge the card.
  * requires a further step on the server side to charge the card.
  */
  */
-export const gripePaymentHandler = new PaymentMethodHandler({
-    code: 'gripe',
-    name: 'Gripe Checkout',
+export const examplePaymentHandler = new PaymentMethodHandler({
+    code: 'example-payment-provider',
+    name: 'Example Payment Provider',
     args: {
     args: {
         apiKey: 'string',
         apiKey: 'string',
     },
     },

+ 0 - 22
server/src/config/payment-method/fakepal-payment-method-config.ts

@@ -1,22 +0,0 @@
-import { Order } from '../../entity/order/order.entity';
-import { PaymentMetadata } from '../../entity/payment/payment.entity';
-
-import { PaymentConfig, PaymentMethodHandler } from './payment-method-handler';
-
-/**
- * An example of a simple client-side payment method, where the payment authorization & settlement is
- * done on the client and no further steps are required on the server.
- */
-export const fakePalPaymentHandler = new PaymentMethodHandler({
-    code: 'fakepal',
-    name: 'FakePal Checkout',
-    args: {},
-    createPayment: (order: Order, metadata: PaymentMetadata = {}): PaymentConfig => {
-        return {
-            amount: order.total,
-            state: 'Settled',
-            transactionId: metadata.transactionId.toString(),
-            metadata,
-        };
-    },
-});

+ 2 - 2
server/src/config/payment-method/payment-method-handler.ts

@@ -33,7 +33,7 @@ export type CreatePaymentFn<T extends PaymentMethodArgs> = (
     metadata: PaymentMetadata,
     metadata: PaymentMetadata,
 ) => PaymentConfig | Promise<PaymentConfig>;
 ) => PaymentConfig | Promise<PaymentConfig>;
 
 
-export interface PaymentMethodConfigOptions<T extends PaymentMethodArgs = {}> {
+export interface PaymentMethodConfigOptions<T extends PaymentMethodArgs = PaymentMethodArgs> {
     code: string;
     code: string;
     name: string;
     name: string;
     createPayment: CreatePaymentFn<T>;
     createPayment: CreatePaymentFn<T>;
@@ -47,7 +47,7 @@ export interface PaymentMethodConfigOptions<T extends PaymentMethodArgs = {}> {
  * third-party payment gateway before the Payment is created and can also define actions to fire
  * third-party payment gateway before the Payment is created and can also define actions to fire
  * when the state of the payment is changed.
  * when the state of the payment is changed.
  */
  */
-export class PaymentMethodHandler<T extends PaymentMethodArgs = {}> {
+export class PaymentMethodHandler<T extends PaymentMethodArgs = PaymentMethodArgs> {
     readonly code: string;
     readonly code: string;
     readonly name: string;
     readonly name: string;
     readonly args: T;
     readonly args: T;

+ 6 - 2
server/src/service/services/payment-method.service.ts

@@ -83,11 +83,14 @@ export class PaymentMethodService {
         const paymentMethodHandlers: Array<PaymentMethodHandler<PaymentMethodArgs>> = this.configService
         const paymentMethodHandlers: Array<PaymentMethodHandler<PaymentMethodArgs>> = this.configService
             .paymentOptions.paymentMethodHandlers;
             .paymentOptions.paymentMethodHandlers;
         const existingPaymentMethods = await this.connection.getRepository(PaymentMethod).find();
         const existingPaymentMethods = await this.connection.getRepository(PaymentMethod).find();
-        const missing = paymentMethodHandlers.filter(
+        const toCreate = paymentMethodHandlers.filter(
             h => !existingPaymentMethods.find(pm => pm.code === h.code),
             h => !existingPaymentMethods.find(pm => pm.code === h.code),
         );
         );
+        const toRemove = existingPaymentMethods.filter(
+            h => !paymentMethodHandlers.find(pm => pm.code === h.code),
+        );
 
 
-        for (const handler of paymentMethodHandlers) {
+        for (const handler of toCreate) {
             let paymentMethod = existingPaymentMethods.find(pm => pm.code === handler.code);
             let paymentMethod = existingPaymentMethods.find(pm => pm.code === handler.code);
 
 
             if (!paymentMethod) {
             if (!paymentMethod) {
@@ -112,6 +115,7 @@ export class PaymentMethodService {
             );
             );
             await this.connection.getRepository(PaymentMethod).save(paymentMethod);
             await this.connection.getRepository(PaymentMethod).save(paymentMethod);
         }
         }
+        await this.connection.getRepository(PaymentMethod).remove(toRemove);
     }
     }
 
 
     private getDefaultValue(type: PaymentMethodArgType): string {
     private getDefaultValue(type: PaymentMethodArgType): string {