def query(request: HttpRequest, transaction_id: str) -> HttpResponse: """ Retries the status of the given transaction from netaxept. """ logger.info('netaxept-query', transaction_id=transaction_id) payment_gateway, gateway_config = get_payment_gateway('netaxept') netaxept_config = gateway_to_netaxept_config(gateway_config) query_response = netaxept_protocol.query(config=netaxept_config, transaction_id=transaction_id) return TemplateResponse(request, 'netaxept/query_result.html', {'query_response': query_response})
def register_payment(payment: Payment) -> str: """ - Registers the payment with netaxept. - Stores the newly created netaxept transaction_id in the Payment. - Records a Transaction representing the registration. :param payment: A payment to register. :return: The newly created netaxept transaction_id :raises NetaxeptException: If the payment was already registered or the registration fails """ logger.info('netaxept-actions-register', payment_id=payment.id) if payment.token != '': # The payment was already registered. if payment.is_authorized: raise PaymentAlreadyRegisteredAndAuthorized() else: # If payment was registered but not yet authorized we re-register it so that a later authorize can succeeed. # Otherwise when the user gets to the netaxept terminal page he sees a 'payment already processed' error. logger.info('netaxept-regegister-payment', payment_id=payment.id) _payment_gateway, gateway_config = get_payment_gateway(payment.gateway) netaxept_config = gateway_to_netaxept_config(gateway_config) try: register_response = netaxept_protocol.register( config=netaxept_config, order_number=payment.id, amount=payment.total, language='en', customer_email=payment.customer_email) except NetaxeptProtocolError as exception: Transaction.objects.create(payment=payment, kind=TransactionKind.REGISTER, token='', is_success=False, amount=payment.total, error=exception.error, gateway_response=exception.raw_response) raise NetaxeptException(exception.error) with transaction.atomic(): payment.token = register_response.transaction_id payment.save() Transaction.objects.create( payment=payment, kind=TransactionKind.REGISTER, token=register_response.transaction_id, is_success=True, amount=payment.total, error=None, gateway_response=register_response.raw_response) return register_response.transaction_id
def register_and_goto_terminal(request: HttpRequest, payment_id: int) -> HttpResponse: """ Register the payment with netaxept, and take the user to the terminal page for payment authorization. """ logger.info('netaxept-register-and-goto-terminal', payment_id=payment_id) payment = get_object_or_404(Payment, id=payment_id) transaction_id = actions.register_payment(payment) payment_gateway, gateway_config = get_payment_gateway(payment.gateway) netaxept_config = gateway_to_netaxept_config(gateway_config) return redirect(netaxept_protocol.get_payment_terminal_url(config=netaxept_config, transaction_id=transaction_id))
def payment_intents_manual_flow(request: HttpRequest, payment_id: int) -> HttpResponse: payment = get_object_or_404(Payment, id=payment_id) payment_gateway, gateway_config = get_payment_gateway(payment.gateway) connection_params = gateway_config.connection_params stripe_public_key = connection_params['public_key'] confirm_payment_endpoint = reverse('stripe_payment_intents_confirm_payment', args=[payment_id]) return TemplateResponse( request, 'stripe/payment_intents_manual_flow.html', { 'stripe_public_key': stripe_public_key, 'confirm_payment_endpoint': confirm_payment_endpoint})
def test_dummy_payment_form(kind, charge_status, payment_dummy): payment = payment_dummy data = {"charge_status": charge_status} payment_gateway, gateway_config = get_payment_gateway(payment.gateway) payment_info = create_payment_information(payment) form = payment_gateway.create_form( data=data, payment_information=payment_info, connection_params=gateway_config.connection_params, ) assert form.is_valid() gateway_process_payment(payment=payment, payment_token=form.get_payment_token()) payment.refresh_from_db() assert payment.transactions.last().kind == kind
def payment_intents_confirm_payment(request, payment_id): # XXX: Update the payment with the info payment = get_object_or_404(Payment, id=payment_id) payment_gateway, gateway_config = get_payment_gateway(payment.gateway) connection_params = gateway_config.connection_params stripe_public_key = connection_params['public_key'] import stripe stripe.api_key = stripe_public_key data = request.data try: if 'payment_method_id' in data: # Create the PaymentIntent intent = stripe.PaymentIntent.create( payment_method=data['payment_method_id'], amount=1099, currency='chf', confirmation_method='manual', confirm=True, ) elif 'payment_intent_id' in data: intent = stripe.PaymentIntent.confirm(data['payment_intent_id']) except stripe.error.CardError as e: # Display error on client return JsonResponse({'error': e.user_message}) if intent.status == 'requires_action' and intent.next_action.type == 'use_stripe_sdk': # Tell the client to handle the action return JsonResponse({ 'requires_action': True, 'payment_intent_client_secret': intent.client_secret}) elif intent.status == 'succeeded': # The payment didn’t need any additional actions and completed! # Handle post-payment fulfillment return JsonResponse({'success': True}) else: # Invalid status return JsonResponse({'error': 'Invalid PaymentIntent status'}, status=500)
def elements_token(request: HttpRequest, payment_id: int) -> HttpResponse: payment = get_object_or_404(Payment, id=payment_id) payment_gateway, gateway_config = get_payment_gateway(payment.gateway) connection_params = gateway_config.connection_params if request.method == 'GET': return TemplateResponse( request, 'stripe/elements_token.html', {'stripe_public_key': connection_params['public_key']}) elif request.method == 'POST': stripe_token = request.POST.get('stripeToken') if stripe_token is None: return HttpResponse('missing stripe token') try: logger.info('stripe authorize', payment=payment) gateway_authorize(payment=payment, payment_token=stripe_token) except Exception as exc: logger.error('stripe authorize', exc_info=exc) return HttpResponse('Error authorizing {}: {}'.format(payment_id, exc)) else: return redirect('view_payment', payment_id=payment.pk)
def register_payment(payment: Payment) -> str: """ This part of the process is unique to netaxept so it cannot be implemented inside the payment generic SPI. This implies that a programmer who wants to use the netaxept gateway will have to know to call this specific function. This function: - Registers the payment with netaxept - Stores the newly created netaxept transaction_id in the Payment (the transaction_id represents the payment on the netaxept side, and needs to be passed to netaxept for further operations like capturing, refunding, etc) - Create a Transaction object representing the registration for auditing purposes. :param payment: A payment to register :return: The the newly created netaxept transaction id :raises NetaxeptException: If the payment was already registered or the registration fails """ logger.info('netaxept-actions-register', payment_id=payment.id) if payment.token != '': # The payment was already registered. if payment.is_authorized: raise PaymentAlreadyRegisteredAndAuthorized() else: # If payment was registered but not yet authorized we re-register it so that a later authorize can succeeed: # Otherwise when the user gets to the netaxept terminal page he sees a 'payment already processed' error. logger.info('netaxept-regegister-payment', payment_id=payment.id) _payment_gateway, gateway_config = get_payment_gateway(payment.gateway) netaxept_config = gateway_to_netaxept_config(gateway_config) try: register_response = netaxept_protocol.register( config=netaxept_config, order_number=payment.id, amount=payment.total, language='en', customer_email=payment.customer_email) except NetaxeptProtocolError as exception: Transaction.objects.create( payment=payment, kind=TransactionKind.REGISTER, token='', is_success=False, amount=payment.total, error=exception.error, gateway_response=exception.raw_response) raise NetaxeptException(exception.error) with transaction.atomic(): payment.token = register_response.transaction_id payment.save() Transaction.objects.create( payment=payment, kind=TransactionKind.REGISTER, token=register_response.transaction_id, is_success=True, amount=payment.total, error=None, gateway_response=register_response.raw_response) return register_response.transaction_id