UNPKG

@open-tender/store

Version:

A library of hooks, reducers, utility functions, and types for use with Open Tender applications that utilize our in-store POS API

112 lines (111 loc) 5.53 kB
import { __awaiter, __generator } from "tslib"; import { handleRespError } from '@open-tender/utils'; import { useEffect, useState } from 'react'; import { useAppDispatch, useAppSelector } from '../../app/hooks'; import { completeChipDNA, selectApi, selectOrder } from '../../slices'; import { processMessage } from './useChipDNATender.utils'; // JC Amex card token: 440abfc9cc7518f1ad76e2be56f19fe4 var useChipDNATender = function (amount) { var dispatch = useAppDispatch(); var orderId = useAppSelector(selectOrder).orderId; var api = useAppSelector(selectApi); var _a = useState(false), reading = _a[0], setReading = _a[1]; var _b = useState('Pending...'), message = _b[0], setMessage = _b[1]; var _c = useState(null), success = _c[0], setSuccess = _c[1]; var _d = useState(null), canceled = _d[0], setCanceled = _d[1]; var _e = useState(null), error = _e[0], setError = _e[1]; useEffect(function () { var readChipDNA = function () { return __awaiter(void 0, void 0, void 0, function () { var data, response, reader_1, card_token_1, card_msg_1, err_1, error_1, detail; return __generator(this, function (_a) { switch (_a.label) { case 0: setReading(true); _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); if (!amount) return [2 /*return*/]; data = { tender_type: 'CREDIT', amount: amount }; if (!orderId) return [2 /*return*/]; return [4 /*yield*/, api.postChipDNATender(orderId, data)]; case 2: response = _a.sent(); reader_1 = response.getReader(); card_token_1 = null; card_msg_1 = null; reader_1.read().then(function processText(_a) { var done = _a.done, value = _a.value; // done - true if the stream has already given you all its data. // value - some data. Always undefined when done is true. if (done) { if (card_token_1 || card_msg_1 === 'TENDER_CREATED') { setSuccess('Card authorized!'); } else if (card_msg_1 === 'TRANSACTION_DECLINED') { setError('Transaction DECLINED. Please try again.'); } else if (card_msg_1 !== 'TRANSACTION_CANCELED') { setError('Card read error. Please try again.'); } console.log('card_token', card_token_1); dispatch(completeChipDNA(card_token_1)); return; } var _b = processMessage(value), msg = _b.msg, msgType = _b.msgType, token = _b.token; var decoded = new TextDecoder('utf-8').decode(value); console.log('done', done); console.log(decoded); console.log(msg, msgType, token); if (msg) setMessage(msg); card_token_1 = token; card_msg_1 = msgType; // console.log('card_msg', card_msg) // console.log('card_token', card_token) return reader_1.read().then(processText); }); return [3 /*break*/, 4]; case 3: err_1 = _a.sent(); error_1 = handleRespError(err_1); detail = error_1.detail; setError(detail || 'Unknown payment error. Please contact support.'); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }; if (!reading && amount && amount !== '0.00') readChipDNA(); }, [api, dispatch, reading, orderId, amount]); var cancel = function () { return __awaiter(void 0, void 0, void 0, function () { var err_2, detail; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, api.postChipDNACancel()]; case 1: _a.sent(); setCanceled('EMV transaction canceled'); return [3 /*break*/, 3]; case 2: err_2 = _a.sent(); detail = handleRespError(err_2).detail; setError(detail || 'Unknown cancellation error. Please contact support.'); return [3 /*break*/, 3]; case 3: return [2 /*return*/]; } }); }); }; return { message: message, success: success, canceled: canceled, error: error, cancel: cancel }; }; export default useChipDNATender;