def _page_from_link(self, link: ActionLink, element_key: str, element_class: Type[T]) -> PaypalPage[T]: """Performs an API call to get a page of elements Arguments: link {ActionLink} -- Page action link (HATEOAS) element_key {str} -- The key for the element lists inside the page json element_class {T} -- class reference of a PayPalEntity subclass Returns: PaypalPage[T] -- A page with the desired elements """ api_response = self._execute_action_link(link, None) if api_response.status_code != 200: return PaypalPage(True, api_response, 0, 0, [], []) json_response = api_response.json() total_items = json_response.get('total_items', 0) total_pages = json_response.get('total_pages', 0) elements = [ element_class.serialize_from_json(x) for x in json_response[element_key] ] links = [ ActionLink(x['href'], x['rel'], x.get('method', 'GET')) for x in json_response['links'] ] return PaypalPage(False, api_response, total_items, total_pages, elements, links)
def list_event_notifications( self, page_size: int = 10, transaction_id: str = None, event_type: str = None, date: DateRange = None) -> PaypalPage[WebhookEvent]: params = {'page_size': 10} if transaction_id != None: params['transaction_id'] = transaction_id if event_type != None: params['event_type'] = event_type if date != None and date.start_time != None: params['start_time'] = datetime.strftime(date.start, '%Y-%m-%dT%H:%M:%S') if date != None and date.end_time != None: params['end_time'] = datetime.strftime(date.end, '%Y-%m-%dT%H:%M:%S') api_response = self._session.get(self._base_url, params=params) if api_response.status_code // 100 != 2: return PaypalPage.error(api_response) return PaypalPage.full_parse_success(api_response, WebhookEvent, 'events')
def list_subscription_transactions( self, subscription_id: str, start_time: datetime, end_time: datetime) -> PaypalPage[SubscriptionTransaction]: """Calls the API to lists transactions for a subscription. Arguments: subscription_id {str} -- The subscription id start_time {datetime} -- transaction start time end_time {datetime} -- transaction end time Returns: PaypalPage[SubscriptionTransaction] -- Paged transaction info """ fmt = '%Y-%m-%dT%H:%M:%S' url = parse_url(self._base_url, subscription_id, 'transactions') params = {end_time.strftime(fmt), start_time.strftime(fmt)} api_response = self._session.get(url, params) if api_response.status_code // 100 != 2: return PaypalPage.error(api_response) return PaypalPage.full_parse_success(api_response, SubscriptionTransaction, 'transaction')
def list_plans( self, product_id: str = None, plan_ids: List[str] = [], page_size: int = 10, page: int = 1, total_required: bool = True, response_type: ResponseType = ResponseType.MINIMAL ) -> PaypalPage[Plan]: """Calls the Paypal API to list different plan details in a page Keyword Arguments: product_id {str} -- product id to query (default: {None}) plan_ids {List[str]} -- list of desired plan ids (10 supported) (default: {[]}) page_size {int} -- size of the page (default: {10}) page {int} -- desired page (default: {1}) total_required {bool} -- flag to show the total count in the response (default: {True}) response_type {ResponseType} -- response type (default: {ResponseType.MINIMAL}) Returns: PaypalPage[Plan] -- Page with plan details """ url = self._base_url params = { 'page_size': page_size, 'page': page, 'total_required': total_required } if product_id: params['product_id'] = product_id if plan_ids: params['plan_ids'] = ','.join(plan_ids) api_response = self._session.get(url, params, headers = { 'Prefer': response_type.as_header_value() }) if api_response.status_code // 100 != 2: return PaypalPage.error(api_response) return PaypalPage.full_parse_success(api_response, Plan, 'plans', response_type)
def list_invoices(self, page: int = 1, page_size: int = 10, total_required: bool = True, fields: List[str] = []) -> PaypalPage[Invoice]: """Calls the paypal API to get an invoice page. Arguments: page {int} -- current page page_size {int} -- page size total_required {bool} -- total count required fields {List[str]} -- fields to be searched Returns: PaypalPage -- The paged elements in paypal API paged response """ query_params = { 'page': page, 'page_size': page_size, 'total_required': total_required } if fields: query_params['fields'] = ','.join(fields) response = self._session.get(parse_url(self._base_url, 'invoices'), query_params) if response.status_code != 200: return PaypalPage.error(response) json_response = response.json() items = [ Invoice.serialize_from_json(x) for x in json_response.get('items', []) ] links = [ ActionLink(x['href'], x['rel'], x.get('method', 'GET')) for x in json_response.get('links', []) ] return PaypalPage.success(response, json_response.get('total_items'), json_response.get('total_pages'), items, links)
def list_plans_from_link(self, link: ActionLink, response_type: ResponseType = ResponseType.MINIMAL) -> PaypalPage[Plan]: """Calls the Paypal API to list different plan details in a page Arguments: link {ActionLink} -- Page link Keyword Arguments: response_type {ResponseType} -- response type (default: {ResponseType.MINIMAL}) Returns: PaypalPage[Plan] -- Page with plan details """ url = link.href api_response = self._session.get(url, headers = { 'Prefer': response_type.as_header_value() }) if api_response.status_code // 100 != 2: return PaypalPage.error(api_response) return PaypalPage.full_parse_success(api_response, Plan, 'plans', response_type)
def search_invoices(self, page: int, page_size: int, total_required: bool, search: InvoiceSearchRequest) -> PaypalPage[Invoice]: """Searches for and lists invoices that match search criteria. If you pass multiple criteria, the response lists invoices that match all criteria. Arguments: page {int} -- current page page_size {int} -- page size total_required {bool} -- total count required search {InvoiceSearchRequest} -- search criteria to be matched Returns: PaypalPage[Invoice] -- The paged elements in paypal API paged response """ query_params = { page: page, page_size: page_size, total_required: total_required } response = self._session.post(parse_url(self._base_url, 'search-invoices'), json.dumps(search.to_dict()), params=query_params) if response.status_code != 200: return PaypalPage.error(response) json_response = response.json() items = [ Invoice.serialize_from_json(x) for x in json_response['items'] ] links = [ ActionLink(x['href'], x['rel'], x.get('method', 'GET')) for x in json_response['links'] ] return PaypalPage.success(response, json_response.get('total_items'), json_response.get('total_pages'), items, links)
def list_products(self, page_size: int, page: int) -> PaypalPage[Product]: """Performs an API call to get a page of products Arguments: page_size {int} -- Amount of elements in the page page {int} -- current page index Returns: PaypalPage[Product] -- A page with product elements """ url = self._base_url params = { 'page_size': page_size, 'page': page, 'total_required': True } api_response = self._session.get(url, params) if api_response.status_code != 200: return PaypalPage(True, api_response, 0, 0, [], []) json_response = api_response.json() products = [ Product.serialize_from_json(x) for x in json_response['products']] links = [ActionLink(x['href'], x['rel'], x.get('method', 'GET')) for x in json_response['links']] return PaypalPage(False, api_response, json_response['total_items'], json_response['total_pages'], products, links)
def list_disputes(self, start_time: datetime, page_size: int = 2) -> PaypalPage[Dispute]: """Performs an API call to lists disputes Arguments: start_time {datetime} -- Start date Keyword Arguments: page_size {int} -- size of the page (default: {2}) Returns: PaypalPage[Dispute] -- Page with a lists of disputes """ url = self._base_url params = { 'page_size': page_size, 'start_time': start_time.strftime('%Y-%m-%dT%H:%M:%S.000Z') } api_response = self._session.get(url, params) if api_response.status_code != 200: return PaypalPage(True, api_response, 0, 0, [], []) json_response = api_response.json() items = [ Dispute.serialize_from_json(x) for x in json_response['items'] ] links = [ ActionLink(x['href'], x['rel'], x.get('method', 'GET')) for x in json_response['links'] ] return PaypalPage(False, api_response, json_response.get('total_items'), json_response.get('total_pages'), items, links)
def deactivate_plan(self, plan_id: str) -> PaypalApiResponse: """Calls the API to deactivate a plan Arguments: plan_id {str} -- The id of the plan to deactivate Returns: PaypalApiResponse -- Response with the operation status """ api_response = self._session.post(parse_url(self._base_url, plan_id, 'deactivate')) if api_response.status_code // 100 != 2: return PaypalPage.error(api_response) return PaypalApiResponse.success(api_response)
def show_plan_details(self, plan_id: str) -> PaypalApiResponse[Plan]: """Calls the paypal API to get the plan details Arguments: plan_id {str} -- The plan identifier Returns: PaypalApiResponse[Plan] -- Api response obj with the plan info. """ api_response = self._session.get(parse_url(self._base_url, plan_id)) if api_response.status_code // 100 != 2: return PaypalPage.error(api_response) return PaypalApiResponse.success(api_response, Plan.serialize_from_json(api_response.json()))
def update_pricing(self, plan_id: str, pricing_schemes: List[UpdatePricingSchemeRequest]) -> PaypalApiResponse: """Calls the API to update a plan's pricing scheme Arguments: plan_id {str} -- The id of the plan to deactivate pricing_schemes List[UpdatePricingSchemeRequest] -- The pricing scheme updates Returns: PaypalApiResponse -- Response with the operation status """ body = json.dumps([ x.to_dict() for x in pricing_schemes ]) url = parse_url(self._base_url, plan_id, 'update-pricing-schemes') api_response = self._session.post(url, body) if api_response.status_code // 100 != 2: return PaypalPage.error(api_response) return PaypalApiResponse.success(api_response)
def update_plan(self, plan_id: str, update_request: List[PatchUpdateRequest]) -> PaypalApiResponse: """Patch request to update a plan. See the docs for details Arguments: plan_id {str} -- id of the plan to be updated. update_request {PatchUpdateRequest} -- request object with update info. Returns: PaypalApiResponse -- Response obj with operation status """ url = parse_url(self._base_url, plan_id) body = json.dumps([ {'op': x.operation, 'value': x.value, 'path': x.path } for x in update_request ]) api_response = self._session.patch(url, body) if api_response.status_code // 100 != 2: return PaypalPage.error(api_response) return PaypalApiResponse.success(api_response)
def create_template(self, template: Template, *, headers: dict = dict()) -> PaypalApiResponse: """Calls the Paypal API to create a template. Arguments: template {Template} -- object with all the required info. Returns: PaypalApiResponse[Template] -- The paypal API response """ response = self._session.post(self._base_url, json.dumps(template.to_dict()), headers=headers) if response.status_code != 200: return PaypalPage.error(response) parsed_data = Template.serialize_from_json( response.json()) if response.json() else None return PaypalApiResponse.success(response, parsed_data)