RestBucks on .Net; paying the order

Previous posts

This time I am going to show how did I implement the use case for payment.

As I shown in my previous post about canceling the order, the first thing to modify is our domain class Order;

public class Order
{
    //more properties and methods


    public virtual Payment     Payment { get; private set; }
    public virtual OrderStatus Status  { get; private set; }

    public virtual void Pay(string cardNumber, string cardOwner)
    {
        if (Status != OrderStatus.Unpaid)
        {
            string message = string.Format("The order can not be paid because it is {0}.",
                                           Status.ToString().ToLower());
            throw new InvalidOrderOperationException(message);
        }
        Status = OrderStatus.Paid;
        Payment = new Payment
                  {
                      CardOwner = cardOwner, 
                      CreditCardNumber = cardNumber
                  };
    }
}

And the next step is to add another operation to our REST end point:

[WebInvoke(UriTemplate = "{orderId}/payment", Method = "POST")]
public HttpResponseMessage Pay(int orderId, PaymentRepresentation paymentArgs)
{
    var order = orderRepository.GetById(orderId);
    if (order == null) return Responses.NotFound();
    order.Pay(paymentArgs.CardNumber, paymentArgs.CardOwner);
    return Responses.Ok();
}

It is a POST to http://restbuckson.net/order/{id}/payment. As usual you can see the tests here.

After you paid an order the order will remain in the “Paid” status for few minutes until the barista prepares it. In my next post I will show how I wrote my barista-bot ;).


blog comments powered by Disqus
  • Categories

  • Archives