request_messenger.py (6063B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\store\client\payment\purchase\messenger\request_messenger.py 2 import logging 3 from six.moves import http_client as httplib 4 from ipaddress.proto import to_eve_public 5 from publicGateway.grpc.exceptions import GenericException 6 from stackless_response_router.exceptions import TimeoutException 7 from store.client.const import LOG_ENTRY_NAME 8 from store.client.payment.purchase.errors import InsufficientFunds, PurchaseFailed 9 from store.client.store_price import StorePrice 10 from store.client.offers.offer_identifier import StoreOfferIdentifier 11 from store.client.payment.purchase.taxed_price_information import TaxedPriceInformation 12 from store.client.payment.token.errors import RedirectRequired 13 from store.client.payment.token.quick_pay_token import QuickPayTokenIdentifier 14 from eveProto.generated.eve_public.payment.purchase.purchase_pb2 import Order 15 from eveProto.generated.eve_public.payment.purchase.api_pb2 import TokenRequest, TokenResponse, CostRequest, CostResponse 16 from eveProto.generated.eve_public.plex.vault.api.requests_pb2 import PurchaseRequest, PurchaseResponse 17 _TIMEOUT_SECONDS = 20 18 logger = logging.getLogger(__name__) 19 20 class PublicPaymentPurchaseRequestMessenger(object): 21 22 def __init__(self, public_gateway): 23 self.public_gateway = public_gateway 24 25 def purchase_with_token_request(self, offer_identifier, quantity, cost_information, token_identifier, ip_address): 26 request = TokenRequest(order=Order(offer=StoreOfferIdentifier.to_proto(offer_identifier), quantity=quantity, cost=TaxedPriceInformation.to_proto(cost_information)), token=QuickPayTokenIdentifier.to_proto(token_identifier), ip_address=to_eve_public(ip_address)) 27 info_log = '{log_entry} - Purchase {count} x offer {offer_id} with token {token_id} and IP address {ip} '.format(log_entry=LOG_ENTRY_NAME, count=quantity, offer_id=offer_identifier, token_id=token_identifier, ip=ip_address) 28 try: 29 self._blocking_request(request, TokenResponse) 30 info_log += 'Response: purchase successful. ' 31 except GenericException as exc: 32 status_code = exc.response_primitive.status_code 33 if status_code == httplib.TEMPORARY_REDIRECT: 34 info_log += 'Purchase failed, redirect to website required. ' 35 raise RedirectRequired 36 if status_code == httplib.BAD_REQUEST: 37 info_log += 'Purchase failed: %s' % exc.response_primitive.status_message 38 raise PurchaseFailed(exc.response_primitive.status_message) 39 if status_code == httplib.INTERNAL_SERVER_ERROR: 40 info_log += 'Purchase failed, internal server error. ' 41 raise PurchaseFailed('InternalServerError') 42 info_log += 'ERROR (status_code={status_code})'.format(status_code=status_code) 43 logger.exception('Unexpected exception when purchasing with token: %s', exc) 44 raise 45 except TimeoutException as exc: 46 info_log += 'TIMED OUT' 47 logger.exception('Timed out when purchasing with token: %s', exc) 48 raise 49 finally: 50 logger.info(info_log) 51 52 def purchase_with_plex_request(self, offer_identifier, quantity): 53 request = PurchaseRequest(offer=StoreOfferIdentifier.to_proto(offer_identifier), quantity=quantity, not_gift=True) 54 info_log = '{log_entry} - Purchase offer {qty} x {offer_id} with PLEX '.format(log_entry=LOG_ENTRY_NAME, qty=quantity, offer_id=offer_identifier) 55 try: 56 self._blocking_request(request, PurchaseResponse) 57 info_log += 'Response: purchase successful. ' 58 except GenericException as exc: 59 status_code = exc.response_primitive.status_code 60 if status_code == httplib.PAYMENT_REQUIRED: 61 info_log += 'Purchase failed, insufficient funds. ' 62 raise InsufficientFunds 63 info_log += 'ERROR (status_code={status_code})'.format(status_code=status_code) 64 logger.exception('Unexpected exception when purchasing with PLEX: %s', exc) 65 raise 66 except TimeoutException as exc: 67 info_log += 'TIMED OUT' 68 logger.exception('Timed out when purchasing with PLEX: %s', exc) 69 raise 70 finally: 71 logger.info(info_log) 72 73 def get_taxed_cost_request(self, price, token_identifier): 74 request = CostRequest(catalog_amount_in_cents=int(price.currency_price_in_cents) if price.currency_price_in_cents else 0, currency=price.currency_code, token=QuickPayTokenIdentifier.to_proto(token_identifier)) 75 info_log = '{log_entry} - Get taxed cost request for price {price} and token {token_id} '.format(log_entry=LOG_ENTRY_NAME, price=price, token_id=token_identifier) 76 try: 77 response_primitive, response_payload = self._blocking_request(request, CostResponse) 78 info_log += 'Response: taxed cost returned: {tax}. '.format(tax=response_payload.cost) 79 return TaxedPriceInformation.build_from_proto(response_payload.cost) 80 except GenericException as exc: 81 status_code = exc.response_primitive.status_code 82 info_log += 'ERROR (status_code={status_code})'.format(status_code=status_code) 83 logger.exception('Unexpected exception when purchasing with token: %s', exc) 84 raise 85 except TimeoutException as exc: 86 info_log += 'TIMED OUT' 87 logger.exception('Timed out when purchasing with token: %s', exc) 88 raise 89 finally: 90 logger.info(info_log) 91 92 def _blocking_request(self, request, response_class): 93 request_primitive, response_channel = self.public_gateway.send_user_request(request, response_class, _TIMEOUT_SECONDS) 94 response_primitive, payload = response_channel.receive() 95 if response_primitive.status_code != 200: 96 raise GenericException(request_primitive, response_primitive) 97 return (response_primitive, payload)