# Take a payment

The person approves the amount and the payee; any other amount or payee is refused.

| | |
|---|---|
| Level | 2: A person whose identity is verified approves with a passkey. |
| Class | `irreversible`: Cannot be undone. The app creates the approval for that exact request, and it works once. |

## What the test proves

- the person approves the amount and the payee, then 201
- another amount, another payee or a second payment needs another approval
- an approval to pay is never kept by the agent

## Python

```python
routes.append(route("POST", "/payees/{payee}/payments", "payments:send", cls="irreversible", level=2,
                    resource="payee:{payee}", amount_field="/amount", summary="Pay someone",
                    body={"amount": "a decimal string, e.g. 40.00", "currency": "e.g. EUR"}))
templates["payments:send"] = "Pay {amount.max} {amount.currency} to {scope}"


@app.post("/payees/<payee>/payments")
def pay(payee):
    # The payee, the amount and the currency are the ones the person approved.
    payment = request.json
    return {"paid": payment["amount"], "currency": payment["currency"], "to": payee}, 201
```

## JavaScript

```js
routes.push(route("POST", "/payees/{payee}/payments", "payments:send", {
  class: "irreversible", level: 2, resource: "payee:{payee}", amountField: "/amount", summary: "Pay someone",
  body: { amount: "a decimal string, e.g. 40.00", currency: "e.g. EUR" },
}));
templates["payments:send"] = "Pay {amount.max} {amount.currency} to {scope}";

api.post("/payees/:payee/payments", (req, res) => {
  // The payee, the amount and the currency are the ones the person approved.
  const { amount, currency } = req.body;
  res.status(201).json({ paid: amount, currency, to: req.params.payee });
});
```

## Go

```go
routes = append(routes, allowedby.Route{Method: "POST", Path: "/payees/{payee}/payments",
	Capability: "payments:send", Class: allowedby.Irreversible, Level: 2, Resource: "payee:{payee}",
	AmountField: "/amount", Summary: "Pay someone",
	Body: map[string]string{"amount": "a decimal string, e.g. 40.00", "currency": "e.g. EUR"}})
templates["payments:send"] = allowedby.Text("Pay {amount.max} {amount.currency} to {scope}")

mux.HandleFunc("POST /payees/{payee}/payments", func(w http.ResponseWriter, r *http.Request) {
	// The payee, the amount and the currency are the ones the person approved.
	var payment struct{ Amount, Currency string }
	if err := json.NewDecoder(r.Body).Decode(&payment); err != nil {
		writeJSON(w, http.StatusBadRequest, map[string]string{"error": "amount, currency: strings"})
		return
	}
	writeJSON(w, http.StatusCreated, map[string]string{
		"paid": payment.Amount, "currency": payment.Currency, "to": r.PathValue("payee")})
})
```

## Java

```java
@Bean
AllowedByCustomizer pay() {
    return aby -> aby
            .route(Route.post("/payees/{payee}/payments").capability("payments:send")
                    .actionClass(ActionClass.IRREVERSIBLE).level(2).resource("payee:{payee}")
                    .amountField("/amount").summary("Pay someone")
                    .body(Map.of("amount", "a decimal string, e.g. 40.00", "currency", "e.g. EUR")))
            .template("payments:send", "Pay {amount.max} {amount.currency} to {scope}");
}

@PostMapping("/payees/{payee}/payments")
@ResponseStatus(HttpStatus.CREATED)
Map<String, String> pay(@PathVariable String payee, @RequestBody Payment payment) {
    // The payee, the amount and the currency are the ones the person approved.
    return Map.of("paid", payment.amount(), "currency", payment.currency(), "to", payee);
}
```

## C#

```dotnet
routes.Add(Route("POST", "/payees/{payee}/payments", "payments:send", cls: "irreversible", level: 2,
    resource: "payee:{payee}", amountField: "/amount", summary: "Pay someone",
    body: new Dictionary<string, string> { ["amount"] = "a decimal string, e.g. 40.00", ["currency"] = "e.g. EUR" }));
templates["payments:send"] = "Pay {amount.max} {amount.currency} to {scope}";

app.MapPost("/payees/{payee}/payments", (string payee, Payment payment) =>
    // The payee, the amount and the currency are the ones the person approved.
    Results.Json(new { paid = payment.Amount, currency = payment.Currency, to = payee }, statusCode: 201));
```

## PHP

```php
$routes[] = route('POST', '/payees/{payee}/payments', 'payments:send', class: 'irreversible', level: 2,
    resource: 'payee:{payee}', amountField: '/amount', summary: 'Pay someone',
    body: ['amount' => 'a decimal string, e.g. 40.00', 'currency' => 'e.g. EUR']);
$templates['payments:send'] = 'Pay {amount.max} {amount.currency} to {scope}';

$app->post('/payees/{payee}/payments', function (Request $request, Response $response, array $args) use ($json) {
    // The payee, the amount and the currency are the ones the person approved.
    $payment = json_decode((string) $request->getBody(), true);

    return $json($response, ['paid' => $payment['amount'], 'currency' => $payment['currency'], 'to' => $args['payee']], 201);
});
```

## Ruby

```ruby
ROUTES << AllowedBy.route(
  "POST", "/payees/{payee}/payments", "payments:send",
  class: "irreversible", level: 2, resource: "payee:{payee}", amount_field: "/amount", summary: "Pay someone",
  body: { amount: "a decimal string, e.g. 40.00", currency: "e.g. EUR" }
)
TEMPLATES["payments:send"] = "Pay {amount.max} {amount.currency} to {scope}"

post "/payees/:payee/payments" do
  # The payee, the amount and the currency are the ones the person approved.
  payment = JSON.parse(request.body.read)
  [201, { paid: payment["amount"], currency: payment["currency"], to: params["payee"] }.to_json]
end
```

## Rust

```rust
let aby = aby
    .route(
        Route::post("/payees/{payee}/payments")
            .capability("payments:send")
            .class(Class::Irreversible)
            .level(2)
            .resource("payee:{payee}")
            .amount_field("/amount")
            .summary("Pay someone")
            .body_field("amount", "a decimal string, e.g. 40.00")
            .body_field("currency", "e.g. EUR"),
    )
    .template("payments:send", "Pay {amount.max} {amount.currency} to {scope}");
let app = app.route("/payees/{payee}/payments", post(pay));

async fn pay(Path(payee): Path<String>, Json(payment): Json<Payment>) -> (StatusCode, Json<Value>) {
    // The payee, the amount and the currency are the ones the person approved.
    (StatusCode::CREATED, Json(json!({"paid": payment.amount, "currency": payment.currency, "to": payee})))
}
```
