/* PaymentSurcharge ( _amount ; _paymentType ; _ccType ; _allowed ; _achPercent ; _defaultCardPercent ) Calculates surcharge fees in both inclusive and exclusive modes and returns a JSON object following the same structural style as TaxSplitAll — but with paymentType and ccType embedded into each mode object rather than at the top level. Parameters: _amount = Base amount or total amount _paymentType = "Card", "ACH", "Cash", "Check", etc. _ccType = Card type (Visa, Mastercard, Amex, etc.) _allowed = 1 or 0 → whether surcharges are permitted _achPercent = ACH percent (1.5 or 0.015) _defaultCardPercent = percent for non-Visa/Mastercard cards Output JSON: { "inclusive": { paymentType, ccType, base, fee, grandTotal, percent }, "exclusive": { paymentType, ccType, base, fee, grandTotal, percent } } */ Let ([ _amount = GetAsNumber ( _amount ) ; _paymentType = _paymentType ; _ccType = _ccType ; _allowed = Case ( _allowed = 1 ; 1 ; 0 ) ; // Normalize ACH percent (accepts 1.5 or 0.015) _achPercent = Case ( Abs ( _achPercent ) > 1 ; _achPercent / 100 ; _achPercent ) ; // Normalize default card percent (2.5 or 0.025) _defaultCardPercent = Case ( Abs ( _defaultCardPercent ) > 1 ; _defaultCardPercent / 100 ; _defaultCardPercent ) ; // Determine a rate (decimal) _percent = Case ( not _allowed ; 0 ; _paymentType = "Card" and _ccType = "Visa" ; 0.03 ; _paymentType = "Card" and _ccType = "Mastercard" ; 0.04 ; _paymentType = "Card" ; _defaultCardPercent ; _paymentType = "ACH" ; _achPercent ; 0 ); // ======================================================= // Inclusive block (amount already includes surcharge) // ======================================================= _jsonInclusive = Case ( _percent = 0 ; JSONSetElement ( "{}" ; [ [ "paymentType" ; _paymentType ; 4 ] ; [ "ccType" ; _ccType ; 4 ] ; [ "base" ; _amount ; 1 ] ; [ "fee" ; 0 ; 1 ] ; [ "grandTotal" ; _amount ; 1 ] ; [ "percent" ; _percent ; 1 ] ] ); Let ([ _totalInc = _amount ; _baseInc = Round ( _totalInc / ( 1 + _percent ) ; 2 ) ; _feeInc = Round ( _totalInc - _baseInc ; 2 ) ]; JSONSetElement ( "{}" ; [ [ "paymentType" ; _paymentType ; 4 ] ; [ "ccType" ; _ccType ; 4 ] ; [ "base" ; _baseInc ; 1 ] ; [ "fee" ; _feeInc ; 1 ] ; [ "grandTotal" ; _totalInc ; 1 ] ; [ "percent" ; _percent ; 1 ] ] ) ) ); // ======================================================= // Exclusive block (base → add surcharge) // ======================================================= _jsonExclusive = Case ( _percent = 0 ; JSONSetElement ( "{}" ; [ [ "paymentType" ; _paymentType ; 4 ] ; [ "ccType" ; _ccType ; 4 ] ; [ "base" ; _amount ; 1 ] ; [ "fee" ; 0 ; 1 ] ; [ "grandTotal" ; _amount ; 1 ] ; [ "percent" ; _percent ; 1 ] ] ); Let ([ _baseEx = _amount ; _feeEx = Round ( _baseEx * _percent ; 2 ) ; _totalEx = _baseEx + _feeEx ]; JSONSetElement ( "{}" ; [ [ "paymentType" ; _paymentType ; 4 ] ; [ "ccType" ; _ccType ; 4 ] ; [ "base" ; _baseEx ; 1 ] ; [ "fee" ; _feeEx ; 1 ] ; [ "grandTotal" ; _totalEx ; 1 ] ; [ "percent" ; _percent ; 1 ] ] ) ) ) ]; // ======================================================= // Final JSON result // ======================================================= JSONSetElement ( "{}" ; [ [ "inclusive" ; _jsonInclusive ; JSONRaw ] ; [ "exclusive" ; _jsonExclusive ; JSONRaw ] ] ) )