예제 #1
0
def create_application_bank_transaction(application, active_payment_type):
    prefix = active_payment_type.payment_channel.erekins_url_prefix

    # Create new requests session with Auth keys and prepended url
    session = SessionWHeaders({'Authorization': 'ApiKey %s' % active_payment_type.payment_channel.erekins_auth_key}, url="https://%s.e-rekins.lv" % prefix)

    information = "Pieteikums nr.%i" % application.id if not application.external_invoice_code else "Rekins nr.%s" % application.external_invoice_nr
    if application.donation > 0:
        information += (" + %s" % application.competition.params.get('donation', {}).get('bank_code', 'Ziedojums - %s')) % application.donation

    bank_data = {
        "information": information,
        "integration_id": application.id,
        "amount": float(application.final_price),
        "link": active_payment_type.payment_channel.erekins_link,
    }

    transaction_obj = session.post("/api/v1/transaction/", data=json.dumps(bank_data))
    transaction_obj.raise_for_status()
    transaction = transaction_obj.json()

    Payment.objects.create(content_object=application,
                           channel=active_payment_type,
                           erekins_code=transaction.get('code'),
                           total=application.final_price,
                           status=Payment.STATUS_PENDING,
                           donation=application.donation)

    return "https://%s.e-rekins.lv/bank/%s/" % (prefix, transaction.get('code'))
예제 #2
0
def create_team_bank_transaction(team, active_payment_type):
    prefix = active_payment_type.payment_channel.erekins_url_prefix

    # Create new requests session with Auth keys and prepended url
    session = SessionWHeaders({'Authorization': 'ApiKey %s' % active_payment_type.payment_channel.erekins_auth_key}, url="https://%s.e-rekins.lv" % prefix)

    information = "Komandas %s profila apmaksa %s" % (unicode(team), team.distance.competition.get_full_name) if not team.external_invoice_code else "Rekins nr.%s" % team.external_invoice_nr

    bank_data = {
        "information": information,
        "integration_id": team.id,
        "amount": float(team.final_price),
        "link": active_payment_type.payment_channel.erekins_link,
    }

    transaction_obj = session.post("/api/v1/transaction/", data=json.dumps(bank_data))
    transaction_obj.raise_for_status()
    transaction = transaction_obj.json()

    Payment.objects.create(content_object=team,
                           channel=active_payment_type,
                           erekins_code=transaction.get('code'),
                           total=team.final_price,
                           status=Payment.STATUS_PENDING, )

    return "https://%s.e-rekins.lv/bank/%s/" % (prefix, transaction.get('code'))
예제 #3
0
def validate_payment(payment, user=False, request=None):
    prefix = payment.channel.payment_channel.erekins_url_prefix
    # Create new requests session with Auth keys and prepended url
    session = SessionWHeaders({'Authorization': 'ApiKey %s' % payment.channel.payment_channel.erekins_auth_key}, url="https://%s.e-rekins.lv" % prefix)

    transaction_obj = session.get('/api/v1/transaction/?code=%s' % urllib.quote(payment.erekins_code))

    transactions = transaction_obj.json()
    if transactions.get('meta').get('total_count') == 0:
        if payment.status != payment.STATUS_OK:
            payment.status = payment.STATUS_ID_NOT_FOUND
            payment.save()
            Log.objects.create(content_object=payment, action="TRANSACTION_VALIDATE", message="Not found transaction id in e-rekins.lv", params={'code': payment.erekins_code})
        if user:
            raise Http404
        else:
            return False

    transaction = transactions.get('objects')[0]
    status = transaction.get('status')

    payment.status = status
    payment.save()

    if status == 30:
        # Transaction is successful.
        return approve_payment(payment, user, request)
    elif status < 0:

        other_active_payments = Payment.objects.filter(content_type=payment.content_type, object_id=payment.object_id, status__gte=0).exclude(id=payment.id)
        if payment.content_type.model == 'application':
            application = payment.content_object
            # If there are no other active payments and user haven't taken invoice, then lets reset application payment status.
            if not other_active_payments and not application.external_invoice_code:
                application.payment_status = application.PAY_STATUS_NOT_PAYED
                application.save()

        Log.objects.create(content_object=payment, action="TRANSACTION_VALIDATE", message="Transaction unsuccessful. Redirecting to payment view.", params={'user': user})

        if user:
            messages.error(request, _('Transaction unsuccessful. Try again.'))
            if payment.content_type.model == 'application':
                return HttpResponseRedirect(reverse('application_pay', kwargs={'slug': payment.content_object.code}))
            elif payment.content_type.model == 'team':
                return HttpResponseRedirect(reverse('accounts:team_pay', kwargs={'pk2': payment.content_object.id}))
        else:
            return False
    else:
        Log.objects.create(content_object=payment, action="TRANSACTION_VALIDATE", message="UNKNOWN STATUS. This must be fixed.", params={'user': user})

        if user:
            messages.info(request, _('Transaction status unknown. If you paid, then wait a while until status is updated - you will receive email.'))
            if payment.content_type.model == 'application':
                return HttpResponseRedirect(reverse('application_pay', kwargs={'slug': payment.content_object.code}))
            elif payment.content_type.model == 'team':
                return HttpResponseRedirect(reverse('accounts:team_pay', kwargs={'pk2': payment.content_object.id}))
        else:
            return False
예제 #4
0
def create_application_invoice(application, active_payment_type, action="send"):
    bill_series = application.competition.bill_series
    prefix = active_payment_type.payment_channel.erekins_url_prefix

    # Create new requests session with Auth keys and prepended url
    session = SessionWHeaders({'Authorization': 'ApiKey %s' % active_payment_type.payment_channel.erekins_auth_key}, url="https://%s.e-rekins.lv" % prefix)

    # Find series resource_uri from e-rekins
    series_obj = session.get("/api/v1/series/?prefix=%s" % bill_series)
    if series_obj.json().get('meta').get('total_count') == 0:
        series_obj = session.post("/api/v1/series/", data=json.dumps({
            'title': bill_series,
            'reset_period': 4,
            'prefix': bill_series,
            'formatting': '- #'
        }))
        series_obj.raise_for_status()
        series_resource_uri = series_obj.headers.get('Location')
    else:
        series_resource_uri = series_obj.json().get('objects')[0].get('resource_uri')

    client_data = {
        'name': application.company_name,
        'name_short': application.company_name,
        'integration_code': str(application.id),
        'number': application.company_regnr,
        'vat': application.company_vat,
        'email': application.email,
        'office_address': {
            'address': application.company_address,
            'country': '/api/v1/country/124/',  # Always default is Latvia
        },
        'juridical_address':  {
            'address': application.company_juridical_address,
            'country': '/api/v1/country/124/',  # Always default is Latvia
        },
        'form': 1,  # Juridical
    }

    # Find or Create customer
    client_obj = session.get("/api/v1/client/?number=%s" % application.company_regnr)
    if client_obj.json().get('meta').get('total_count') == 0:
        client_obj = session.post("/api/v1/client/", data=json.dumps(client_data))
        client_obj.raise_for_status()
        client_obj = session.get(client_obj.headers.get('Location'))
        client_obj.raise_for_status()
        client_resource_uri = client_obj.json().get('resource_uri')
    else:
        client_resource_uri = client_obj.json().get('objects')[0].get('resource_uri')
        client_obj = session.put(client_resource_uri, data=json.dumps(client_data))
        client_obj.raise_for_status()

    items = []
    for participant in application.participant_set.all():
        if application.invoice_show_names:
            description = "Dalības maksa %(competition)s - %(distance)s - %(full_name)s (%(year)i)" % {
                "competition": application.competition.get_full_name,
                "distance": unicode(participant.distance),
                "full_name": participant.full_name,
                "year": participant.birthday.year
            }
        else:
            description = "Dalības maksa %(competition)s " % {
                "competition": application.competition.get_full_name,
            }

        items.append({
            "description": description,
            "vat": getattr(settings, "EREKINS_%s_DEFAULT_VAT" % active_payment_type.payment_channel.payment_channel),
            "units": "gab.",
            "amount": "1",
            "price": get_participant_fee_from_price(participant.competition, participant.price)
        })
        if participant.insurance:
            items.append({
                "description": "&nbsp;-&nbsp;Apdrošināšana %(insurance)s" % {
                    "insurance": participant.insurance,
                },
                "vat": getattr(settings, "EREKINS_%s_DEFAULT_VAT" % active_payment_type.payment_channel.payment_channel),
                "units": "gab.",
                "amount": "1",
                "price": get_insurance_fee_from_insurance(participant.competition, participant.insurance)
            })
    if application.donation > 0:
        information = application.competition.params.get('donation', {}).get('bank_code', 'Ziedojums - %s') % application.donation
        items.append({
            "description": information,
            "vat": getattr(settings, "EREKINS_%s_DEFAULT_VAT" % active_payment_type.payment_channel.payment_channel),
            "units": "gab.",
            "amount": "1",
            "price": float(application.donation)
        })
    if not application.competition.complex_payment_enddate:
        competition_date = application.competition.competition_date
    else:
        competition_date = application.competition.get_children()[0].competition_date
    competition_datetime = datetime.datetime.combine(competition_date, datetime.time())
    now = datetime.datetime.now()
    if now + datetime.timedelta(days=7) > competition_datetime:
        due_date = now
    elif now + datetime.timedelta(days=14) > competition_datetime:
        due_date = competition_datetime - datetime.timedelta(days=6)
    else:
        due_date = now + datetime.timedelta(days=7)

    invoice_data = {
        "due_date": due_date.strftime('%Y-%m-%d'),
        "client": client_resource_uri,
        "currency": "/api/v1/currency/1/",
        "invoice_creator": getattr(settings, "EREKINS_%s_DEFAULT_CREATOR" % active_payment_type.payment_channel.payment_channel),
        "series": series_resource_uri,
        "language": getattr(settings, "EREKINS_%s_DEFAULT_LANGUAGE" % active_payment_type.payment_channel.payment_channel),
        "payment_type": "Pārskaitījums",
        "kind": "2",
        "items": items,
        "status": "10",
        "activity": 'Pakalpojumu sniegšana',
        "ad_integration_code": application.id,
        "comments": "Nesaņemot apmaksu līdz norādītajam termiņam, rēķins zaudē spēku un dalībnieki starta sarakstā neparādās, kā arī netiek pielaisti pie starta.",
        "action": action,
        "invoice_external_edit_url": ("%s%s" % (settings.MY_DEFAULT_DOMAIN, reverse('manager:application_pay', kwargs={'pk': application.competition_id, 'pk2': application.id}))).replace('http://', 'https://'),
    }

    # Create invoice
    invoice_obj = session.post("/api/v1/invoice/", data=json.dumps(invoice_data))
    invoice_obj.raise_for_status()
    invoice_obj = session.get(invoice_obj.headers.get('Location'))
    invoice_obj.raise_for_status()
    invoice = invoice_obj.json()
    return invoice.get('code'), invoice.get('invoice_nr')
예제 #5
0
def create_team_invoice(team, active_payment_type, action="send"):
    bill_series = team.distance.competition.bill_series
    prefix = active_payment_type.payment_channel.erekins_url_prefix

    # Create new requests session with Auth keys and prepended url
    session = SessionWHeaders({'Authorization': 'ApiKey %s' % active_payment_type.payment_channel.erekins_auth_key}, url="https://%s.e-rekins.lv" % prefix)

    # Find series resource_uri from e-rekins
    series_obj = session.get("/api/v1/series/?prefix=%s" % bill_series)
    if series_obj.json().get('meta').get('total_count') == 0:
        series_obj = session.post("/api/v1/series/", data=json.dumps({
            'title': bill_series,
            'reset_period': 4,
            'prefix': bill_series,
            'formatting': '- #'
        }))
        series_obj.raise_for_status()
        series_resource_uri = series_obj.headers.get('Location')
    else:
        series_resource_uri = series_obj.json().get('objects')[0].get('resource_uri')

    client_data = {
        'name': team.company_name,
        'name_short': team.company_name,
        'integration_code': "team_%s" % str(team.id),
        'number': team.company_regnr,
        'vat': team.company_vat,
        'email': team.email,
        'office_address': {
            'address': team.company_address,
            'country': '/api/v1/country/124/',  # Always default is Latvia
        },
        'juridical_address':  {
            'address': team.company_juridical_address,
            'country': '/api/v1/country/124/',  # Always default is Latvia
        },
        'form': 1,  # Juridical
    }

    # Find or Create customer
    client_obj = session.get("/api/v1/client/?number=%s" % team.company_regnr)
    if client_obj.json().get('meta').get('total_count') == 0:
        client_obj = session.post("/api/v1/client/", data=json.dumps(client_data))
        client_obj.raise_for_status()
        client_obj = session.get(client_obj.headers.get('Location'))
        client_obj.raise_for_status()
        client_resource_uri = client_obj.json().get('resource_uri')
    else:
        client_resource_uri = client_obj.json().get('objects')[0].get('resource_uri')
        client_obj = session.put(client_resource_uri, data=json.dumps(client_data))
        client_obj.raise_for_status()

    items = [{
            "description": "Komandas %s profila apmaksa" % unicode(team),
            "vat": getattr(settings, "EREKINS_%s_DEFAULT_VAT" % active_payment_type.payment_channel.payment_channel),
            "units": "gab.",
            "amount": "1",
            "price": float(team.final_price)
        }, ]

    due_date = datetime.datetime.now() + datetime.timedelta(days=7)
    invoice_data = {
        "due_date": due_date.strftime('%Y-%m-%d'),
        "client": client_resource_uri,
        "currency": "/api/v1/currency/1/",
        "invoice_creator": getattr(settings, "EREKINS_%s_DEFAULT_CREATOR" % active_payment_type.payment_channel.payment_channel),
        "series": series_resource_uri,
        "language": getattr(settings, "EREKINS_%s_DEFAULT_LANGUAGE" % active_payment_type.payment_channel.payment_channel),
        "payment_type": "Pārskaitījums",
        "kind": "2",
        "items": items,
        "status": "10",
        "activity": 'Pakalpojumu sniegšana',
        "ad_integration_code": "team_%i" % team.id,
        "action": action,
    }

    # Create invoice
    invoice_obj = session.post("/api/v1/invoice/", data=json.dumps(invoice_data))
    invoice_obj.raise_for_status()
    invoice_obj = session.get(invoice_obj.headers.get('Location'))
    invoice_obj.raise_for_status()
    invoice = invoice_obj.json()
    return invoice.get('code'), invoice.get('invoice_nr')