|
| 1 | +import { ChargeStationEventHandler } from 'lib/ChargeStation/eventHandlers'; |
| 2 | +import { AuthorizationType } from 'lib/settings'; |
| 3 | + |
| 4 | +const vendorId = 'com.g2mobility'; |
| 5 | +const messageIdCardTxReport = 'CardTxReport'; |
| 6 | + |
| 7 | +export const calculateCostsAndSendReceipt: ChargeStationEventHandler = async ( |
| 8 | + params |
| 9 | +) => { |
| 10 | + const { session, chargepoint } = params; |
| 11 | + |
| 12 | + if (session.options.authorizationType !== AuthorizationType.CreditCard) { |
| 13 | + return; // session will stop the normal route |
| 14 | + } |
| 15 | + |
| 16 | + // Calculations below are just general approximations of how costs may be applied. |
| 17 | + const costPerMinute = chargepoint.configuration.getVariableValue( |
| 18 | + 'TariffCostCtrlr.EMVDefaultProfile.CostPerMinute' |
| 19 | + ) as number; |
| 20 | + const costPerkWh = chargepoint.configuration.getVariableValue( |
| 21 | + 'TariffCostCtrlr.EMVDefaultProfile.CostPerkWh' |
| 22 | + ) as number; |
| 23 | + |
| 24 | + const startTime = session.startTime; |
| 25 | + const stopTime = session.stopTime as Date; |
| 26 | + const durationMinutes = |
| 27 | + (stopTime.getTime() - startTime.getTime()) / 1000 / 60; |
| 28 | + |
| 29 | + const durationCost = durationMinutes * costPerMinute; |
| 30 | + const kWhCost = session.kwhElapsed * costPerkWh; |
| 31 | + const sessionCost = durationCost + kWhCost; |
| 32 | + |
| 33 | + chargepoint.writeCall('DataTransfer', { |
| 34 | + vendorId, |
| 35 | + messageId: messageIdCardTxReport, |
| 36 | + data: JSON.stringify({ |
| 37 | + tx_ID: Number(session.transactionId), |
| 38 | + tx_Time: new Date().toISOString(), |
| 39 | + priceSchemeID: 'DEFAULT', |
| 40 | + costID: null, |
| 41 | + totalTaxedCost: Number(sessionCost.toFixed(2)), |
| 42 | + report: 'CARTE BANCAIRE', |
| 43 | + }), |
| 44 | + }); |
| 45 | +}; |
0 commit comments