def pay(request, order): """ Create the payment and redirect to PayPal or back to the order with an error message """ paypal_fee = PayOrderByPaypal.calculate_processing_fee(order) order.processing_fee += paypal_fee order.save() payment = paypalrestsdk.Payment({ "intent": "sale", "payer": { "payment_method": "paypal" }, "redirect_urls": { "return_url": request.build_absolute_uri().split('?')[0] + '?status=success', "cancel_url": request.build_absolute_uri().split('?')[0] + '?status=failed', }, "transactions": [ { "amount": { "total": str(currency_round_up(order.left_to_pay)), "currency": CURRENCY[1], }, "description": _("Payment for {event} with variable symbol: {vs}".format( event=order.event, vs=order.variable_symbol)) }, ] }) try: paypal_payment = payment.create() except paypalrestsdk.exceptions.UnauthorizedAccess: payment.error = 'Client Authentication failed' paypal_payment = False if paypal_payment: request.session['paypal_payment_id'] = payment['id'] try: return redirect(PayOrderByPaypal.get_paypal_url(payment)) except Exception as e: logger.error(str(e)) logger.error( "Payment for order(pk={order}) couldn't be created! Error: {err}". format(order=order.pk, err=payment.error)) messages.error( request, _('Something went wrong, try again later, or try different payment method.' )) order.processing_fee -= paypal_fee order.save() return redirect('konfera_payments:payment_options', order_uuid=str(order.uuid))
def currency_code(value): return '%s %s' % (currency_round_up(value), CURRENCY[1])
def currency(value): return '%s %s' % (currency_round_up(value), CURRENCY[0])
def get_paypal_price(order): """ Calculate the total price for order when paid by paypal """ additional_charge = order.left_to_pay * Decimal( settings.PAYPAL_ADDITIONAL_CHARGE) / Decimal('100') return currency_round_up(order.left_to_pay + additional_charge)