PaymentFromCreditCard column is NOT NULL with no default value, causing INSERT to fail silently. Set to 0 for cash payments. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
108 lines
3.3 KiB
Text
108 lines
3.3 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 --->
|
|
<cfset qOrder = queryExecute(
|
|
"SELECT ID, StatusID, UserID, BusinessID, ServicePointID, PaymentID
|
|
FROM Orders WHERE 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>
|
|
|
|
<!--- Create Payment record with expected cash amount --->
|
|
<cfset CashAmountCents = round(CashAmount * 100)>
|
|
<cfset qInsertPayment = queryExecute(
|
|
"INSERT INTO Payments (
|
|
PaymentPaidInCash,
|
|
PaymentFromCreditCard,
|
|
PaymentSentByUserID,
|
|
PaymentReceivedByUserID,
|
|
PaymentOrderID,
|
|
PaymentAddedOn
|
|
) VALUES (?, 0, ?, 0, ?, NOW())",
|
|
[
|
|
{ value = CashAmount, cfsqltype = "cf_sql_decimal" },
|
|
{ value = qOrder.UserID, cfsqltype = "cf_sql_integer" },
|
|
{ value = OrderID, cfsqltype = "cf_sql_integer" }
|
|
],
|
|
{ datasource = "payfrit", result = "insertResult" }
|
|
)>
|
|
|
|
<cfset PaymentID = insertResult.generatedKey>
|
|
|
|
<!--- Update order: link payment, set status to submitted, payment pending --->
|
|
<cfset queryExecute(
|
|
"UPDATE Orders
|
|
SET StatusID = 1,
|
|
PaymentID = ?,
|
|
PaymentStatus = 'pending',
|
|
TipAmount = ?,
|
|
SubmittedOn = NOW(),
|
|
LastEditedOn = NOW()
|
|
WHERE ID = ?",
|
|
[
|
|
{ value = PaymentID, cfsqltype = "cf_sql_integer" },
|
|
{ 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>
|