Skip to content

Commit

Permalink
adding escrow release funds
Browse files Browse the repository at this point in the history
  • Loading branch information
chauchausoup committed Oct 18, 2024
1 parent 185e52f commit 1d4e95b
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
46 changes: 46 additions & 0 deletions internal/escrow/escrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,49 @@ func DepositEscrowHandler(w http.ResponseWriter, r *http.Request) {
"escrow_id": escrowID,
})
}

func ReleaseEscrowHandler(w http.ResponseWriter, r *http.Request) {
claims, ok := r.Context().Value("user").(*middleware.Claims)
if !ok || claims.Role != "buyer" {
log.Printf("[ERROR] Unauthorized access attempt by userID %d with role %s", claims.UserID, claims.Role)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}

vars := mux.Vars(r)
transactionIDStr := vars["id"]
transactionID, err := strconv.Atoi(transactionIDStr)
if err != nil {
http.Error(w, "Invalid transaction ID", http.StatusBadRequest)
return
}

var transaction models.Transaction
err = db.DB.Get(&transaction, "SELECT * FROM transactions WHERE transaction_id = $1", transactionID)
if err != nil {
http.Error(w, "Transaction not found", http.StatusNotFound)
return
}

if transaction.Status != "in_progress" && transaction.Status != "pending" {
http.Error(w, "Cannot release funds for this transaction", http.StatusBadRequest)
return
}

_, err = db.DB.Exec("UPDATE escrow_accounts SET status = 'released' WHERE transaction_id = $1", transactionID)
if err != nil {
http.Error(w, "Failed to release funds from escrow", http.StatusInternalServerError)
return
}

_, err = db.DB.Exec("UPDATE transactions SET status = 'completed', updated_at = NOW() WHERE transaction_id = $1", transactionID)
if err != nil {
http.Error(w, "Failed to update transaction status", http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{
"message": "Funds successfully released to the seller",
})
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func main() {
api.HandleFunc("/transactions/{id}/confirm", transactions.ConfirmDeliveryHandler).Methods("PUT")

api.HandleFunc("/escrow/{id}/deposit", escrow.DepositEscrowHandler).Methods("POST")
api.HandleFunc("/escrow/{id}/release", escrow.ReleaseEscrowHandler).Methods("PUT")

// Setup CORS
c := cors.New(cors.Options{
Expand Down
56 changes: 56 additions & 0 deletions swagger/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,62 @@ paths:
security:
- BearerAuth: []

/api/escrow/{id}/release:
put:
summary: Release funds from escrow to the seller
description: This endpoint releases the escrowed funds to the seller. Only admin or system can perform this operation.
tags:
- escrow
parameters:
- name: id
in: path
required: true
description: The ID of the transaction to release funds for
schema:
type: integer
responses:
'200':
description: Funds successfully released to the seller
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: "Funds successfully released to the seller"
'400':
description: Bad request - Invalid transaction status or other validation error
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: "Cannot release funds for this transaction"
'404':
description: Not Found - Transaction not found
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: "Transaction not found"
'500':
description: Server error - An error occurred while processing the request
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: "Failed to release funds from escrow"
security:
- BearerAuth: []

/api/upload:
post:
Expand Down

0 comments on commit 1d4e95b

Please sign in to comment.