This repository has been archived on 2026-03-21. You can view files and clone it, but cannot push or open issues or pull requests.
payfrit-biz/api/orders/submitCash.cfm
John Mizerek 96c2ed3fc1 Fix cash payment fee: use real Payfrit platform fee, not 2.25% cash handling fee
submitCash.cfm: Calculate platform fee from subtotal * PayfritFee,
store in Orders.PlatformFee and Payments.PaymentPayfritsCut on submission.

complete.cfm: Replace bogus 2.25% cash transaction fee with the real
platform fee (customer fee + business fee = 2 × PayfritFee × subtotal).
Credit full Payfrit revenue to User 0. Record business fee in
PaymentPayfritNetworkFees.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-02 12:59:22 -08:00

130 lines
4.5 KiB
Text

<cfsetting showdebugoutput="false">
<cfsetting enablecfoutputonly="true">
<cffunction name="readJsonBody" access="public" returntype="struct" output="false">
<cfset var raw = getHttpRequestData().content>
<cfif isNull(raw) OR len(trim(raw)) EQ 0>
<cfreturn {}>
</cfif>
<cftry>
<cfset var data = deserializeJSON(raw)>
<cfif isStruct(data)>
<cfreturn data>
<cfelse>
<cfreturn {}>
</cfif>
<cfcatch>
<cfreturn {}>
</cfcatch>
</cftry>
</cffunction>
<cffunction name="apiAbort" access="public" returntype="void" output="true">
<cfargument name="payload" type="struct" required="true">
<cfcontent type="application/json; charset=utf-8">
<cfoutput>#serializeJSON(arguments.payload)#</cfoutput>
<cfabort>
</cffunction>
<cfset data = readJsonBody()>
<cfset OrderID = val( structKeyExists(data,"OrderID") ? data.OrderID : 0 )>
<cfset CashAmount = val( structKeyExists(data,"CashAmount") ? data.CashAmount : 0 )>
<cfset Tip = val( structKeyExists(data,"Tip") ? data.Tip : 0 )>
<cfif OrderID LTE 0>
<cfset apiAbort({ "OK": false, "ERROR": "missing_orderid", "MESSAGE": "OrderID is required." })>
</cfif>
<cfif CashAmount LTE 0>
<cfset apiAbort({ "OK": false, "ERROR": "missing_amount", "MESSAGE": "CashAmount is required." })>
</cfif>
<cftry>
<!--- Get order details with business fee rate --->
<cfset qOrder = queryExecute(
"SELECT o.ID, o.StatusID, o.UserID, o.BusinessID, o.ServicePointID, o.PaymentID,
b.PayfritFee
FROM Orders o
INNER JOIN Businesses b ON b.ID = o.BusinessID
WHERE o.ID = ? LIMIT 1",
[ { value = OrderID, cfsqltype = "cf_sql_integer" } ],
{ datasource = "payfrit" }
)>
<cfif qOrder.recordCount EQ 0>
<cfset apiAbort({ "OK": false, "ERROR": "not_found", "MESSAGE": "Order not found." })>
</cfif>
<cfif qOrder.StatusID NEQ 0>
<cfset apiAbort({ "OK": false, "ERROR": "bad_state", "MESSAGE": "Order is not in cart state." })>
</cfif>
<!--- Calculate platform fee (customer portion — shown in cart) --->
<cfset feeRate = (isNumeric(qOrder.PayfritFee) AND val(qOrder.PayfritFee) GT 0) ? val(qOrder.PayfritFee) : 0>
<cfset customerFee = CashAmount * feeRate / (1 + feeRate)>
<!--- CashAmount already includes the customer fee (subtotal+tax+fee), back it out:
Actually CashAmount is what the Android app sends as the order total including fee.
The fee was calculated on subtotal, so we need the actual subtotal from line items. --->
<cfset qSubtotal = queryExecute(
"SELECT COALESCE(SUM(Price * Quantity), 0) AS Subtotal
FROM OrderLineItems WHERE OrderID = ? AND IsDeleted = b'0'",
[ { value = OrderID, cfsqltype = "cf_sql_integer" } ],
{ datasource = "payfrit" }
)>
<cfset subtotal = val(qSubtotal.Subtotal)>
<cfset platformFee = subtotal * feeRate>
<!--- Create Payment record with expected cash amount --->
<cfset CashAmountCents = round(CashAmount * 100)>
<cfset qInsertPayment = queryExecute(
"INSERT INTO Payments (
PaymentPaidInCash,
PaymentFromCreditCard,
PaymentSentByUserID,
PaymentReceivedByUserID,
PaymentOrderID,
PaymentPayfritsCut,
PaymentAddedOn
) VALUES (?, 0, ?, 0, ?, ?, NOW())",
[
{ value = CashAmount, cfsqltype = "cf_sql_decimal" },
{ value = qOrder.UserID, cfsqltype = "cf_sql_integer" },
{ value = OrderID, cfsqltype = "cf_sql_integer" },
{ value = platformFee, cfsqltype = "cf_sql_decimal" }
],
{ datasource = "payfrit", result = "insertResult" }
)>
<cfset PaymentID = insertResult.generatedKey>
<!--- Update order: link payment, set status to submitted, store platform fee --->
<cfset queryExecute(
"UPDATE Orders
SET StatusID = 1,
PaymentID = ?,
PaymentStatus = 'pending',
PlatformFee = ?,
TipAmount = ?,
SubmittedOn = NOW(),
LastEditedOn = NOW()
WHERE ID = ?",
[
{ value = PaymentID, cfsqltype = "cf_sql_integer" },
{ value = platformFee, cfsqltype = "cf_sql_decimal" },
{ value = Tip, cfsqltype = "cf_sql_decimal" },
{ value = OrderID, cfsqltype = "cf_sql_integer" }
],
{ datasource = "payfrit" }
)>
<cfset apiAbort({ "OK": true, "OrderID": OrderID, "PaymentID": PaymentID, "CashAmountCents": CashAmountCents, "MESSAGE": "Order submitted with cash payment." })>
<cfcatch>
<cfset apiAbort({
"OK": false,
"ERROR": "server_error",
"MESSAGE": "Failed to submit cash order",
"DETAIL": cfcatch.message
})>
</cfcatch>
</cftry>