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

feat(payments-plugin): allow partial payments #2092

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
2 changes: 2 additions & 0 deletions packages/payments-plugin/e2e/graphql/admin-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ export const GET_ORDER_PAYMENTS = gql`
query order($id: ID!) {
order(id: $id) {
id
state
totalWithTax
payments {
id
transactionId
Expand Down
622 changes: 329 additions & 293 deletions packages/payments-plugin/e2e/mollie-payment.e2e-spec.ts

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions packages/payments-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"braintree": "3.x",
"stripe": "8.x"
},
"dependencies": {
"currency.js":"2.0.4"
},
"devDependencies": {
"@mollie/api-client": "^3.6.0",
"@types/braintree": "^2.22.15",
Expand Down
6 changes: 3 additions & 3 deletions packages/payments-plugin/src/mollie/mollie.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const molliePaymentHandler = new PaymentMethodHandler({
createPayment: async (
ctx,
order,
amount,
_amount, // Don't use this amount, but the amount from the metadata
args,
metadata,
): Promise<CreatePaymentResult | CreatePaymentErrorResult> => {
Expand All @@ -60,9 +60,9 @@ export const molliePaymentHandler = new PaymentMethodHandler({
if (metadata.status !== 'Authorized' && metadata.status !== 'Settled') {
throw Error(`Cannot create payment for status ${metadata.status} for order ${order.code}. Only Authorized or Settled are allowed.`);
}
Logger.info(`Payment for order ${order.code} created with state '${metadata.status}'`, loggerCtx);
Logger.info(`Payment for order ${order.code} with amount ${metadata.amount} created with state '${metadata.status}'`, loggerCtx);
return {
amount,
amount: metadata.amount,
state: metadata.status,
transactionId: metadata.orderId, // The plugin now only supports 1 payment per order, so a mollie order equals a payment
metadata, // Store all given metadata on a payment
Expand Down
8 changes: 8 additions & 0 deletions packages/payments-plugin/src/mollie/mollie.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CreateParameters } from '@mollie/api-client/dist/types/src/binders/orde
import { Amount } from '@mollie/api-client/dist/types/src/data/global';
import { OrderAddress as MollieOrderAddress } from '@mollie/api-client/dist/types/src/data/orders/data';
import { Customer, Order } from '@vendure/core';
import currency from 'currency.js';

import { OrderAddress } from './graphql/generated-shop-types';

Expand Down Expand Up @@ -79,6 +80,13 @@ export function toAmount(value: number, orderCurrency: string): Amount {
};
}

/**
* Return to number of cents
*/
export function amountToCents(amount: Amount): number {
return currency(amount.value).intValue;
}

/**
* Recalculate tax amount per order line instead of per unit for Mollie.
* Vendure calculates tax per unit, but Mollie expects the tax to be calculated per order line (the total of the quantities).
Expand Down
3 changes: 2 additions & 1 deletion packages/payments-plugin/src/mollie/mollie.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
MolliePaymentIntentResult,
MolliePaymentMethod,
} from './graphql/generated-shop-types';
import { getLocale, toAmount, toMollieAddress, toMollieOrderLines } from './mollie.helpers';
import { amountToCents, getLocale, toAmount, toMollieAddress, toMollieOrderLines } from './mollie.helpers';
import { MolliePluginOptions } from './mollie.plugin';

interface OrderStatusInput {
Expand Down Expand Up @@ -219,6 +219,7 @@ export class MollieService {
const addPaymentToOrderResult = await this.orderService.addPaymentToOrder(ctx, order.id, {
method: paymentMethodCode,
metadata: {
amount: amountToCents(mollieOrder.amount),
status,
orderId: mollieOrder.id,
mode: mollieOrder.mode,
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7431,6 +7431,11 @@ cuint@^0.2.2:
resolved "https://registry.npmjs.org/cuint/-/cuint-0.2.2.tgz#408086d409550c2631155619e9fa7bcadc3b991b"
integrity sha1-QICG1AlVDCYxFVYZ6fp7ytw7mRs=

currency.js@2.0.4:
version "2.0.4"
resolved "https://registry.yarnpkg.com/currency.js/-/currency.js-2.0.4.tgz#a8a4d69be3b2e509bf67a560c78220bc04809cf1"
integrity sha512-6/OplJYgJ0RUlli74d93HJ/OsKVBi8lB1+Z6eJYS1YZzBuIp4qKKHpJ7ad+GvTlWmLR/hLJOWTykN5Nm8NJ7+w==

custom-event@~1.0.0:
version "1.0.1"
resolved "https://registry.npmjs.org/custom-event/-/custom-event-1.0.1.tgz#5d02a46850adf1b4a317946a3928fccb5bfd0425"
Expand Down