def handle_payment(self, order_number, total_incl_tax, **kwargs): """ Complete payment with PayPal - this calls the 'DoExpressCheckout' method to capture the money from the initial transaction. """ try: payer_id = self.request.POST['payer_id'] token = self.request.POST['token'] except KeyError: raise PaymentError("Unable to determine PayPal transaction details") try: txn = confirm_transaction(payer_id, token, amount=self.txn.amount, currency=self.txn.currency) except PayPalError: raise UnableToTakePayment() if not txn.is_successful: raise UnableToTakePayment() # Record payment source and event source_type, is_created = SourceType.objects.get_or_create(name='PayPal') source = Source(source_type=source_type, currency=txn.currency, amount_allocated=txn.amount, amount_debited=txn.amount) self.add_payment_source(source) self.add_payment_event('Settled', txn.amount)
def handle_payment(self, order_number, total, **kwargs): """ Complete payment with PayPal - this calls the 'DoExpressCheckout' method to capture the money from the initial transaction. """ try: confirm_txn = confirm_transaction( kwargs['payer_id'], kwargs['token'], kwargs['txn'].amount, kwargs['txn'].currency) except PayPalError: raise UnableToTakePayment() if not confirm_txn.is_successful: raise UnableToTakePayment() # Record payment source and event source_type, is_created = SourceType.objects.get_or_create( name='PayPal') source = Source(source_type=source_type, currency=confirm_txn.currency, amount_allocated=confirm_txn.amount, amount_debited=confirm_txn.amount, amount_fee=confirm_txn.fee) self.add_payment_source(source) self.add_payment_event('Settled', confirm_txn.amount, reference=confirm_txn.correlation_id, fee=confirm_txn.fee)
def handle_payment(self, order_number, total, **kwargs): """ Complete payment with PayPal - this calls the 'DoExpressCheckout' method to capture the money from the initial transaction. """ try: if int(kwargs['txn'].value('BILLINGAGREEMENTACCEPTEDSTATUS')) == 1: utc_today = timezone.now().utcnow().date() start_date = utc_today.strftime('%Y-%m-%dT%H:%M:%SZ') desc = '' billing_period = None billing_frequency = None basket = Basket.objects.filter(owner=self.request.user, status=Basket.FROZEN).order_by('-date_created')[0] for index, line in enumerate(basket.all_lines()): product = line.product recurring_profile = get_recurring_profile(product) if recurring_profile is not None: billing_period = recurring_profile.get('billing_period') billing_frequency = recurring_profile.get('billing_frequency') desc = recurring_profile.get('billing_description') total_periods = recurring_profile.get('total_periods') confirm_txn = create_recurring_payment( kwargs['payer_id'], kwargs['token'], kwargs['txn'].amount, kwargs['txn'].currency, start_date, desc, billing_period, billing_frequency, total_periods ) else: confirm_txn = confirm_transaction( kwargs['payer_id'], kwargs['token'], kwargs['txn'].amount, kwargs['txn'].currency) except PayPalError: raise UnableToTakePayment() if not confirm_txn.is_successful: raise UnableToTakePayment() # Record payment source and event source_type, is_created = SourceType.objects.get_or_create( name='PayPal') source = Source(source_type=source_type, currency=confirm_txn.currency, amount_allocated=confirm_txn.amount, amount_debited=confirm_txn.amount) self.add_payment_source(source) self.add_payment_event('Settled', confirm_txn.amount, reference=confirm_txn.correlation_id)
def handle_payment(self, order_number, total, **kwargs): """ Complete payment with PayPal - this calls the 'DoExpressCheckout' method to capture the money from the initial transaction. """ try: confirm_txn = confirm_transaction( kwargs['payer_id'], kwargs['token'], kwargs['txn'].amount, kwargs['txn'].currency) except PayPalError: raise UnableToTakePayment() if not confirm_txn.is_successful: raise UnableToTakePayment() # Record payment source and event source_type, is_created = SourceType.objects.get_or_create( name='PayPal') source = Source(source_type=source_type, currency=confirm_txn.currency, amount_allocated=confirm_txn.amount, amount_debited=confirm_txn.amount) self.add_payment_source(source) self.add_payment_event('Settled', confirm_txn.amount, reference=confirm_txn.correlation_id)
def get(self, request, *args, **kwargs): """ Complete payment with PayPal - this calls the 'DoExpressCheckout' method to capture the money from the initial transaction. """ def handle_paypal_error(order_number, amount, correlation_id, code=None): error_msg = _("A problem occurred while processing payment for " "this order - no payment has been taken. Please " "contact customer services if this problem persists") if code: error_msg += ' [Code: %s]' % code messages.error(self.request, error_msg) # set order status order = Order.objects.get(number=order_number) order.set_status("Cancelled") order.save() self.add_payment_event('Failure', amount, reference=correlation_id) # set redirect url self._redirect_url = reverse('customer:order', kwargs={"order_number": order.number}) order = Order.objects.get(number=kwargs["order_number"]) order_number = kwargs["order_number"] currency = kwargs["currency"] amount = kwargs["amount"] token = kwargs["token"] payer_id = kwargs["payer_id"] # add payment source source_type, is_created = SourceType.objects.get_or_create( name='PayPal') source = Source(source_type=source_type, currency=currency, amount_allocated=amount, amount_debited=amount) self.add_payment_source(source) try: confirm_txn = confirm_transaction(payer_id, token, amount, currency) except PayPalError as e: # 10486 error should be redirect to paypal if e.message['code'] == '10486': if getattr(settings, 'PAYPAL_SANDBOX_MODE', True): url = 'https://www.sandbox.paypal.com/webscr' else: url = 'https://www.paypal.com/webscr' params = ( ('cmd', '_express-checkout'), ('token', token), ) url = '%s?%s' % (url, urlencode(params)) # we need to redirect to paypal so do so self._redirect_url = url else: handle_paypal_error(order_number, amount, e.message["correlation_id"], code=e.message["code"]) else: if not confirm_txn.is_successful: # irgend ein anderer Grund wieso es nicht geklappt hat handle_paypal_error(order_number, amount, confirm_txn.correlation_id) else: # everythings seems ok: Record payment source and event self.add_payment_event('Settled', confirm_txn.amount, reference=confirm_txn.correlation_id) # finally (and in any case) save payment detail self.save_payment_details(order) return super(HandlePaymentView, self).get(request, *args, **kwargs)