Add submitCash.cfm endpoint for cash payments
Creates Payment record with PaymentPaidInCash and links to order Sets PaymentStatus = 'pending' and StatusID = 1 (submitted)
This commit is contained in:
parent
5912784772
commit
175fdfb2b9
1 changed files with 110 additions and 0 deletions
110
api/orders/submitCash.cfm
Normal file
110
api/orders/submitCash.cfm
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
<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 cash amount --->
|
||||
<cfset PaymentUUID = createUUID()>
|
||||
<cfset qInsertPayment = queryExecute(
|
||||
"INSERT INTO Payments (
|
||||
UUID,
|
||||
PaymentPaidInCash,
|
||||
PaymentSentByUserID,
|
||||
PaymentOrderID,
|
||||
PaymentTip,
|
||||
CreatedOn
|
||||
) VALUES (?, ?, ?, ?, ?, NOW())",
|
||||
[
|
||||
{ value = PaymentUUID, cfsqltype = "cf_sql_varchar" },
|
||||
{ value = CashAmount, cfsqltype = "cf_sql_decimal" },
|
||||
{ value = qOrder.UserID, cfsqltype = "cf_sql_integer" },
|
||||
{ value = OrderID, cfsqltype = "cf_sql_integer" },
|
||||
{ value = Tip, cfsqltype = "cf_sql_decimal" }
|
||||
],
|
||||
{ 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',
|
||||
Tip = ?,
|
||||
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, "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>
|
||||
Reference in a new issue