Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
feat: promotion based on Pagar.me paymentMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
jonyw4 committed Jul 20, 2020
1 parent 0a325ba commit 0751649
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './payment-method-handler';
export * from './promotion-action';
export * from './plugin';
51 changes: 50 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,60 @@
import { PluginCommonModule, VendurePlugin } from '@vendure/core';
import { PluginCommonModule, VendurePlugin, LanguageCode } from '@vendure/core';
import { pagarmePaymentMethodHandler } from './payment-method-handler';
import { PagarmePostbackController } from './postback-controller';
import {
pagarmeBoletoPromotionAction,
pagarmeCreditCardPromotionAction
} from './promotion-action';

@VendurePlugin({
imports: [PluginCommonModule],
controllers: [PagarmePostbackController],
configuration: (config) => {
config.customFields.Order.push({
name: 'pagarmePaymentMethod',
type: 'string',
public: true,
options: [
{
value: 'boleto',
label: [{ languageCode: LanguageCode.en, value: 'Boleto' }]
},
{
value: 'credit_card',
label: [
{ languageCode: LanguageCode.en, value: 'Credit Card' },
{ languageCode: LanguageCode.pt_BR, value: 'Cartão de Crédito' }
]
}
],
label: [
{
languageCode: LanguageCode.en,
value: 'Pagar.me Payment Method'
},
{
languageCode: LanguageCode.pt_BR,
value: 'Método de Pagamento do Pagar.me'
}
],
description: [
{
languageCode: LanguageCode.en,
value: 'Used internally by Promotion to calculate discount for order'
},
{
languageCode: LanguageCode.pt_BR,
value:
'Usado internamente para usar a condição de promoção baseada no método de pagamento do Pagar.me'
}
]
});
config.promotionOptions.promotionActions?.push(
pagarmeBoletoPromotionAction
);
config.promotionOptions.promotionActions?.push(
pagarmeCreditCardPromotionAction
);
config.paymentOptions.paymentMethodHandlers.push(
pagarmePaymentMethodHandler
);
Expand Down
59 changes: 59 additions & 0 deletions src/promotion-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { PromotionOrderAction, LanguageCode } from '@vendure/core';

export const pagarmeBoletoPromotionAction = new PromotionOrderAction({
code: 'pagarme-boleto-discount',
args: {
discount: {
type: 'int'
}
},
execute(order, args) {
// @ts-ignore
const paymentMethod = order.customFields.pagarmePaymentMethod;
if (paymentMethod && paymentMethod === 'boleto') {
return -order.subTotal * (args.discount / 100);
}
return order.subTotal;
},
description: [
{
languageCode: LanguageCode.en,
value:
'Discount of {discount}% on the order with boleto payment method in Pagar.me'
},
{
languageCode: LanguageCode.pt_BR,
value:
'Desconto de {discount}% no pedido no método de pagamento boleto usando o Pagar.me'
}
]
});

export const pagarmeCreditCardPromotionAction = new PromotionOrderAction({
code: 'pagarme-credit-card-discount',
args: {
discount: {
type: 'int'
}
},
execute(order, args) {
// @ts-ignore
const paymentMethod = order.customFields.pagarmePaymentMethod;
if (paymentMethod && paymentMethod === 'credit_card') {
return -order.subTotal * (args.discount / 100);
}
return order.subTotal;
},
description: [
{
languageCode: LanguageCode.en,
value:
'Discount of {discount}% on the order with credit card payment method in Pagar.me'
},
{
languageCode: LanguageCode.pt_BR,
value:
'Desconto de {discount}% no pedido no método de pagamento cartão de crédito usando o Pagar.me'
}
]
});

0 comments on commit 0751649

Please sign in to comment.