After a payment, the customer expects an email: "we got your money". A shop doesn't run its own mail server. It calls a mail service through its API, like the supplier and the payment provider. Ours lives at http://127.0.0.1:8003: you POST JSON with "to", "subject" and "body" to /send, and it answers 202 Accepted, which means "I have it, I'll deliver it".
The email's text is built from pieces joined with +. Numbers must become text with strconv.Itoa() first, or Go won't compile the line (mismatched types string and int):
count := 3
text := "You have " + strconv.Itoa(count) + " new messages"
fmt.Println(text) // You have 3 new messages
Put the email in a helper function, so it has its own place and other code can send it too. It returns the mail service's answer, a *http.Response, so Go reports missing return until its last line is a return. Call it from the pay route only once the order is saved as paid: a declined card answers earlier, and so does a retry with the same idempotency key, so neither sends an email. The checker adds GET /check/sent, the list of emails the mail service received:
POST /orders/8/pay (Idempotency-Key: 5f1d7c9e-…) → 201 {"amount": 80, "order_id": 8, "payment_id": "pay_1"}
GET /check/sent → 200 [{"body": "Hello Eva Rossi, we received your payment of 80. Thank you!", "id": 1, "subject": "Your order 8 is paid", "to": "eva@example.com"}]
POST /orders/8/pay (the same key) → 201 {"amount": 80, "order_id": 8, "payment_id": "pay_1"}
POST /orders/9/pay → 402 {"error": "Payment declined", "reason": "card_declined"}
POST /orders/10/pay → 201 {"amount": 21, "order_id": 10, "payment_id": "pay_2"}
GET /check/sent → 200 [Eva's email, {"body": "Hello Chloe Ng, we received your payment of 21. Thank you!", "id": 2, "subject": "Your order 10 is paid", "to": "chloe@example.com"}]
Your task: add the constant mail, then write sendReceipt(id int) *http.Response: it finds the order and its customer, builds the subject and the body, turns them into JSON and POSTs them to mail + "/send", closing the body with defer and returning the mail service's answer. Call it in the pay route right after the UPDATE.
Assignment
Follow the Coding Tutorial to write the sendReceipt helper and send a receipt after each successful payment.
Hint
The order has a CustomerID; the name and the email are in the customers table. Wrap id and *order.Total in strconv.Itoa(). A function runs only when it's called: until the pay route calls the helper, GET /check/sent stays empty.
Free plan: 30 tutor messages. PRO is unlimited.
