def list_templates( self, page: int = 1, page_size: int = 20, fields: InvoiceListRequestField = InvoiceListRequestField.ALL ) -> PaypalApiBulkResponse[Invoice]: """ Calls the paypal API to lists merchant-created templates with associated details. The associated details include the emails, addresses, and phone numbers from the user's PayPal profile. The user can select which values to show in the business information section of their template. Keyword Arguments: page {int} -- current page index (default: {1}) page_size {int} -- page size (how many elements per page) (default: {20}) fields {InvoiceListRequestField} -- The fields to return in the response (default: {InvoiceListRequestField.NONE}) Returns: PaypalApiBulkResponse[Invoice] -- A list of invoices """ # TODO: There might be an error in the docs, the most logical response would be # a page of templates, with the desired fields. This response must be tested. params = { 'page': page, 'page_size': page_size, 'fields': fields.name.lower() } response = self._session.get(self._base_url, params) if response.status_code != 200: return PaypalApiBulkResponse.error(response) return PaypalApiBulkResponse.success( response, Invoice.serialize_from_json(response.json()))
def list_available_events(self) -> PaypalApiBulkResponse[EventType]: """Calls the paypal API to get a list of available events to which any webhook can subscribe. Returns: PaypalApiBulkResponse[EventType] -- Paypal response status with the event type list. """ api_response = self._session.get(self._base_url) if api_response.status_code // 100 != 2: return PaypalApiBulkResponse.error(api_response) return PaypalApiBulkResponse.success(api_response, [ EventType.serialize_from_json(x) for x in api_response.json().get('event_types', []) ])
def delete_profile(self, profile_id: str) -> PaypalApiResponse: """Calls the paypal Api to delete a WebExp Profile Arguments: profile_id {WebExpProfile} -- The profile id. Returns: PaypalApiResponse -- An api response """ api_response = self._session.delete( parse_url(self._base_url, profile_id)) if api_response.status_code != 204: return PaypalApiBulkResponse(True, api_response) return PaypalApiBulkResponse(False, api_response)
def list_profiles(self) -> PaypalApiBulkResponse[WebExpProfile]: """Calls the paypal Api to get a list with the latest 20 web experience profiles for a merchant or subject. Returns: PaypalApiBulkResponse[WebExpProfile] -- Latest 20 profiles """ api_response = self._session.get(self._base_url) if api_response.status_code != 200: return PaypalApiBulkResponse(True, api_response) return PaypalApiBulkResponse(False, api_response, [ WebExpProfile.serialize_from_json(x) for x in api_response.json() ])
def show_profile_details( self, profile_id: str) -> PaypalApiResponse[WebExpProfile]: """Calls the paypal Api to delete a WebExp Profile Arguments: profile_id {WebExpProfile} -- The profile id. Returns: PaypalApiResponse[WebExpProfile] -- An api response with the profile info """ api_response = self._session.get(parse_url(self._base_url, profile_id)) if api_response.status_code != 200: return PaypalApiBulkResponse(True, api_response) return PaypalApiBulkResponse( False, api_response, WebExpProfile.serialize_from_json(api_response.json()))
def _execute_basic_dispute_action( self, url: str, body: dict) -> PaypalApiBulkResponse[ActionLink]: """Executes a standard simple dispute action Arguments: url {str} -- The action URL note {str} -- Notes & comments Returns: PaypalApiBulkResponse[ActionLink] -- action links related to the dispute """ response = self._session.post(url, json.dumps(body)) if response.status_code != 200: return PaypalApiBulkResponse(True, response) return PaypalApiBulkResponse( False, response, ActionLink.serialize_from_json(response['links']))
def _process_referenced_list_response( self, api_response) -> PaypalApiResponse[ReferencedPayoutResponse]: """Process the API response for the list batch payouts calls Arguments: api_response {[type]} -- Http api response Returns: PaypalApiResponse[ReferencedPayoutResponse] -- response info """ if api_response.status_code // 100 != 2: return PaypalApiBulkResponse.error(api_response) j_data = api_response.json() return PaypalApiBulkResponse.success( api_response, ReferencedPayoutResponse.serialize_from_json(j_data) if j_data else None)
def _process_item_response( self, api_response) -> PaypalApiResponse[ReferencedPayoutsItem]: """Process the api response for a referenced payout item Arguments: api_response {[type]} -- The paypal API response Returns: PaypalApiResponse[ReferencedPayoutsItem] -- The processed item """ if api_response.status_code // 100 != 2: return PaypalApiBulkResponse.error(api_response) j_data = api_response.json() return PaypalApiBulkResponse.success( api_response, ReferencedPayoutsItem.serialize_from_json(j_data) if j_data else None)
def create_referenced_batch_payout( self, request: ReferencedPayoutRequest, request_id: str = None, partnerAttrId: str = None) -> PaypalApiBulkResponse[ActionLink]: """Creates a referenced batch payout for asynchronous, offline processing Arguments: request {ReferencedPayoutRequest} -- Request processing info Keyword Arguments: request_id {str} -- paypal request id for idempotence (default: {None}) partnerAttrId {str} -- paypal partner attribution id (default: {None}) Returns: PaypalApiBulkResponse[ActionLink] -- Batch links """ headers = dict() if request_id: headers['PayPal-Request-Id'] = request_id if partnerAttrId: headers['PayPal-Partner-Attribution-Id'] = partnerAttrId if request.execution_type.is_async: headers['Prefer'] = 'respond-async' body = request.to_dict() body.pop('_execution_type', None) api_response = self._session.post(self._base_url, json.dumps(body), headers=headers) if api_response.status_code // 100 != 2: return PaypalApiBulkResponse.error(api_response) j_data = api_response.json() links = [ ActionLink(x['href'], x['rel'], x.get('method', 'GET')) for x in j_data.get('links', []) ] return PaypalApiBulkResponse.success(api_response, links)
def list_event_subscriptions_for_webhook( self, webhook_id: str) -> PaypalApiBulkResponse[EventType]: """Calls the paypal API to list the event subscription for a Webhook Arguments: webhook_id {str} -- Webhook id Returns: PaypalApiBulkResponse[EventType] -- Paypal response status with event type list. """ api_response = self._session.get( parse_url(self._base_url, webhook_id, 'event-types')) if api_response.status_code // 100 != 2: return PaypalApiBulkResponse.error(api_response) return PaypalApiBulkResponse.success(api_response, [ EventType.serialize_from_json(x) for x in api_response.json().get('event_types', []) ])
def list_webhooks( self, anchor_type: AnchorType = AnchorType.APPLICATION ) -> PaypalApiBulkResponse[Webhook]: """Calls the paypal api to list an app webhooks Arguments: anchor_type {AnchorType} -- AnchorType filter Returns: PaypalApiBulkResponse[Webhook] -- Paypal response status with webhook objects. """ api_response = self._session.get( self._base_url, params={'anchor_type': anchor_type.name}) if api_response.status_code // 100 != 2: return PaypalApiBulkResponse.error(api_response) return PaypalApiBulkResponse.success(api_response, [ Webhook.serialize_from_json(x) for x in api_response.json().get('webhooks', []) ])
def _execute_evidence_multipart_request( self, url: str, json_part: dict, files: MIMEApplication) -> PaypalApiBulkResponse[ActionLink]: """Calls the API to provide evidence on a multipart request Arguments: dispute_id {str} -- dispute identifier json_part {dict} -- Json part of the multipart request files {MIMEApplication} -- files to be appended Keyword Arguments: return_addr {PaypalPortableAddress} -- [description] (default: {None}) Returns: PaypalApiBulkResponse[ActionLink] -- [description] """ multipart = MIMEMultipart('related') text = 'input={};type=application/json'.format(json.dumps(json_part)) json_part = MIMEText(text, 'json') # json_part = MIMEApplication(json_part,'json') multipart.attach(json_part) for f in files: multipart.attach(f) body = multipart.as_string() headers = dict(multipart.items()) response = self._session.post(url, body, headers=headers) if response.status_code != 200: return PaypalApiBulkResponse(True, response) return PaypalApiBulkResponse( False, response, ActionLink.serialize_from_json(response['links']))