Verified Commit 23755415 authored by Sli's avatar Sli
Browse files

server: implements buying refound and improve/simplify refilling cancellation

parent 7eebe73f
......@@ -140,9 +140,7 @@ message RefillingReply {
}
message RefoundBuyingRequest {
uint64 transaction_id = 1;
uint64 counter_id = 3;
string device_uuid = 4;
uint64 buying_id = 1;
}
message RefoundBuyingReply {
......@@ -151,22 +149,19 @@ message RefoundBuyingReply {
INTERNAL_SERVER_ERROR = 1;
MISSING_TRANSACTION = 2;
MISSING_COUNTER = 3;
MISSING_DEVICE_UUID = 4;
TRANSACTION_NOT_FOUND = 5;
COUNTER_NOT_FOUND = 6;
TRANSACTION_NOT_FOUND = 3;
ALREADY_REFOUNDED = 4;
}
Status status = 1;
google.protobuf.Timestamp now = 2;
repeated string customer_id = 3; // Multiple customer could be refounded at the same time since multi-payment exist
repeated Money customer_balance = 4; //So we can have multiple new balances...
repeated string customer_ids = 3; // Multiple customer could be refounded at the same time since multi-payment exist
repeated Money customer_balances = 4; //So we can have multiple new balances...
}
message CancelRefillingRequest {
uint64 refilling_id = 1;
string device_uuid = 4;
}
message CancelRefillingReply {
......@@ -175,9 +170,9 @@ message CancelRefillingReply {
INTERNAL_SERVER_ERROR = 1;
MISSING_TRANSACTION = 2;
MISSING_DEVICE_UUID = 4;
TRANSACTION_NOT_FOUND = 5;
TRANSACTION_NOT_FOUND = 3;
ALREADY_CANCELLED = 4;
}
Status status = 1;
......
......@@ -238,10 +238,46 @@ class PaymentServicer(com_pb2_grpc.PaymentProtocolServicer):
)
def RefoundBuying(self, request, context):
"""Missing associated documentation comment in .proto file"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details("Method not implemented!")
raise NotImplementedError("Method not implemented!")
"""
Refound users involved in a buying
"""
# Check missing fields
if not request.buying_id:
return com_pb2.RefoundBuyingReply(
now=pb_now(), status=com_pb2.RefoundBuyingReply.MISSING_TRANSACTION
)
# Check buying validity
buying = db.query(models.Buying).get(request.buying_id)
if buying is None:
return com_pb2.RefoundBuyingReply(
now=pb_now(), status=com_pb2.RefoundBuyingReply.TRANSACTION_NOT_FOUND
)
if buying.refounded:
return com_pb2.RefoundBuyingReply(
now=pb_now(), status=com_pb2.RefoundBuyingReply.ALREADY_REFOUNDED
)
customer_ids = []
customer_balances = []
# Refound users
for payment in buying.payments:
customer = payment.customer
customer.balance += payment.amount
customer_ids.append(customer.id)
customer_balances.append(decimal_to_pb_money(customer.balance))
db.add(customer)
buying.refounded = True
db.add(buying)
db.commit()
return com_pb2.RefoundBuyingReply(
now=pb_now(),
status=com_pb2.RefoundBuyingReply.SUCCESS,
customer_ids=customer_ids,
customer_balances=customer_balances,
)
def CancelRefilling(self, request, context):
"""
......@@ -251,10 +287,6 @@ class PaymentServicer(com_pb2_grpc.PaymentProtocolServicer):
return com_pb2.CancelRefillingReply(
now=pb_now(), status=com_pb2.CancelRefillingReply.MISSING_TRANSACTION
)
if not request.device_uuid:
return com_pb2.CancelRefillingReply(
now=pb_now(), status=com_pb2.CancelRefillingReply.MISSING_DEVICE_UUID
)
refilling = db.query(models.Refilling).get(request.refilling_id)
if refilling is None:
......@@ -262,6 +294,11 @@ class PaymentServicer(com_pb2_grpc.PaymentProtocolServicer):
now=pb_now(), status=com_pb2.CancelRefillingReply.TRANSACTION_NOT_FOUND
)
if refilling.cancelled:
return com_pb2.CancelRefillingReply(
now=pb_now(), status=com_pb2.CancelRefillingReply.ALREADY_CANCELLED
)
refilling.cancelled = True
customer = refilling.customer
customer.balance -= refilling.amount
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment