def create_agreement(self, plan_id, name): """ Create billing agreement. On success returns approval_url and token """ # PayPal does not support immediate start of agreement # That's why we need to increase start date by small amount of time start_date = datetime.datetime.utcnow() + datetime.timedelta(minutes=1) # PayPal does not fully support ISO 8601 format formatted_date = start_date.strftime('%Y-%m-%dT%H:%M:%SZ') agreement = paypal.BillingAgreement({ 'name': name, 'description': 'Agreement for {}'.format(name), 'start_date': formatted_date, 'payer': {'payment_method': 'paypal'}, 'plan': {'id': plan_id} }) try: if agreement.create(): approval_url = self._find_approval_url(agreement.links) # PayPal does not return agreement ID until it is approved # That's why we need to extract token in order to identify it with agreement in DB token = self._find_token(approval_url) return approval_url, token else: raise PayPalError(agreement.error) except paypal.exceptions.ConnectionError as e: raise PayPalError(e)
def create_billing_agreement(name, plan_id, return_url, cancel_url, notify_url=None): start_date = timezone.now() + relativedelta(seconds=10) override_merchant_preferences = dict(cancel_url=cancel_url, return_url=return_url) if notify_url: override_merchant_preferences['notify_url'] = notify_url agreement = paypalrestsdk.BillingAgreement( dict(name=name, description=name, start_date=start_date.strftime(PAYPAL_API_DATE_FORMAT), plan=dict(id=plan_id), payer=dict(payment_method='paypal'), override_merchant_preferences=dict(cancel_url=cancel_url, return_url=return_url)), api) if not agreement.create(): raise Error(agreement.error) return agreement
def test_get_transaction(self): date_format = '%Y-%m-%d' transactions = paypalrestsdk.BillingAgreement( dict(id='I-6PK9NDKY8HBW'), api=api).search_transactions( start_date=(timezone.now() + relativedelta(days=-1)).strftime(date_format), end_date=(timezone.now() + relativedelta(days=1)).strftime(date_format), api=api) if not transactions.success(): raise Exception(transactions.error) logger.info("Transactions: %s", transactions)
def get_transactions(agreement_id, start=None, end=None): date_format = '%Y-%m-%d' if start is None: start = timezone.now() + relativedelta(days=-1) if end is None: end = timezone.now() transactions = paypalrestsdk.BillingAgreement( dict(id=agreement_id), api=api).search_transactions(start_date=start.strftime(date_format), end_date=end.strftime(date_format), api=api) if not transactions.success(): raise Error(transactions.error) else: return transactions.agreement_transaction_list