Beispiel #1
0
def buy_with_card():

    course_id = request.args.get('course')

    if course_id == None:
        return redirect(url_for('course.index'))

    bank_form = EnrollBankForm()
    cc_form = EnrollCreditCardForm()

    if cc_form.validate_on_submit():
        course_id = int(course_id)
        user = User(current_user)
        courses = []
        courses.append(course_id)
        with Transaction().set_context(courses=courses):
            user = User(current_user)
            if user.active_membership:
                return redirect(url_for('course.index'))
        courses = Course.search([('id', '=', course_id)], limit=1)
        if len(courses) == 1:
            course, = courses
            if cc_form.cc_membership.data == 'MONTHLY':
                service_id = course.service.id
            else:
                service_id = course.yearly_service.id
            service = Service(service_id)
        party = user.party
        today = date.today()
        subscriptions = Subscription.search([
            ('party', '=', party.id),
            ('state', '=', 'running'),
            ('is_online', '=', True),
            ('lines.service', '=', service_id),
        ])
        if len(subscriptions) >= 1:
            return redirect(url_for('course.payment'))
        else:
            enrolment, = Product.search([('is_enrolment', '=', True)], limit=1)
            payment_term, = PaymentTerm.search([()], limit=1)
            address = user.party.addresses[0]
            new_subscription = Subscription.create([{
                'party':
                user.party.id,
                'start_date':
                today,
                'invoice_start_date':
                today,
                'invoice_recurrence':
                service.consumption_recurrence,
                'enrolment':
                enrolment.id,
                'unit_price_enrolment':
                0,
                'invoice_address':
                address,
                'payment_term':
                payment_term.id,
                'is_online':
                True,
            }])
            new_subscription_line, = SubscriptionLine.create([{
                'subscription':
                new_subscription[0].id,
                'service':
                service.id,
                'quantity':
                1,
                'start_date':
                today,
                'consumption_recurrence':
                service.consumption_recurrence,
                'unit':
                1,
                'unit_price':
                service.product.list_price,
            }])
            Subscription.quote(new_subscription)
            Subscription.run(new_subscription)
            SaleSubscriptionLine.generate_consumption(date=today,
                                                      party=user.party)
            Subscription.generate_invoice(date=today, party=user.party)
            invoices = Invoice.search([('party', '=', user.party)],
                                      order=[('number', 'DESC')])
            if len(invoices) >= 1:
                for invoice in invoices:
                    if invoice.state == 'draft':
                        Invoice.post([invoice])

            current_invoice = None
            for invoice in invoices:
                if invoice.state not in ['cancel', 'paid']:
                    current_invoice = invoice
                    break

            credentials = current_app.config['QPAYPRO_CREDENTIALS']
            cc_form = CreditCardForm()

            defaults = {}
            defaults['sessionid'] = uniqid()
            defaults['orgid'] = 'k8vif92e'  #'1snn5n9w' TEST OR PRODUCTION
            defaults['merchantid'] = credentials[
                'merchantid']  #'visanetgt_qpay'
            defaults['user'] = user
            defaults['cc_form'] = cc_form

            url_test = 'https://sandbox.qpaypro.com/payment/api_v1'
            url_production = 'https://payments.qpaypro.com/checkout/api_v1'

            credit_card = str(cc_form.card_number.data)
            credit_card.replace(' ', '')

            success = True

            params = {}
            params['x_login'] = credentials['x_login']  #'visanetgt_qpay'
            params['x_private_key'] = credentials[
                'x_private_key']  # '88888888888'
            params['x_api_secret'] = credentials[
                'x_api_secret']  #'99999999999'
            params['x_product_id'] = current_invoice.lines[0].product.id  #6
            params['x_audit_number'] = random.randint(1, 999999)
            params[
                'x_fp_sequence'] = current_invoice.number  #1988679099 #INVOICE SEQUENCE NUMBER
            params[
                'x_invoice_num'] = current_invoice.id  #random.randint(1,999999) #INVOICE SEQUENCE NUMBER
            params['x_fp_timestamp'] = time()
            params['x_currency_code'] = 'GTQ'
            params[
                'x_amount'] = current_invoice.total_amount  #1.00 #invoice.total_amount
            params['x_line_item'] = current_invoice.lines[
                0].product.name  #'T-shirt Live Dreams<|>w01<|><|>1<|>1000.00<|>N'
            params['x_freight'] = 0.00
            params['x_email'] = '*****@*****.**'
            params['cc_name'] = cc_form.name.data  #'john doe'
            params['cc_number'] = credit_card  #'4111111111111111'
            params['cc_exp'] = str(cc_form.expiration_date.data)  #'01/21'
            params['cc_cvv2'] = cc_form.code.data  #'4567'
            params['cc_type'] = 'visa'
            params['x_first_name'] = user.party.name  #'john'
            params['x_last_name'] = user.party.name  #'doe'
            params['x_company'] = 'API CENTRO'  #'Company'
            params['x_address'] = user.party.city  #'711-2880 Nulla'
            params['x_city'] = user.party.city  #'Guatemala'
            params['x_state'] = user.party.city  #'Guatemala'
            params['x_country'] = user.party.country  #'Guatemala'
            params['x_zip'] = '09001'
            params['x_relay_response'] = 'none'
            params['x_relay_url'] = 'none'
            params['x_type'] = 'AUTH_ONLY'
            params['x_method'] = 'CC'
            params[
                'http_origin'] = 'https://www.apixela.net'  #'http://local.test.com'
            params['visaencuotas'] = 0
            params['device_fingerprint_id'] = defaults['sessionid']
            params['bank_form'] = bank_form
            params['cc_form'] = cc_form

            response = requests.post(url=url_production, params=params)

            res = response.raise_for_status()
            response_content = response.json()

            #response_content = {'responseCode':"100",
            #    'idTransaction':"102030"}

            response_code = response_content['responseCode']
            message = get_code(response_code)

            if response_code == "00":
                transaction_id = response_content['idTransaction']
                Invoice.card_payment_succeed([current_invoice],
                                             reference=transaction_id)
            else:
                success = False
                Subscription.draft(new_subscription)
                Subscription.cancel(new_subscription)
                Invoice.cancel([current_invoice])
            if success:
                return render_template('bienvenido.html',
                                       flash_message=message,
                                       user=user)
            else:
                return render_template(
                    'bienvenido.html',
                    user=user,
                    flash_message=message,
                )
    return render_template('enroll.html', cc_form=cc_form, bank_form=bank_form)
Beispiel #2
0
def payment():
    #FIX WHEN CREDIT CARD IS ACTIVE
    return redirect(url_for('course.pay_with_bank'))
    user = User(current_user)
    party = user.party
    today = date.today()
    courses = []
    subscriptions = Subscription.search([('is_online', '=', True),
                                         ('party', '=', party.id),
                                         ('state', '=', 'running')])
    if len(subscriptions) < 1:
        return render_template(
            'bienvenido.html',
            user=user,
            flash_message=
            "Something wrong. Sorry the inconvenient. We will call you later.")
    else:
        for subscription in subscriptions:
            for line in subscription.lines:
                courses.append(line.service.id)
    with Transaction().set_context(courses=courses):
        user = User(current_user)
        if user.active_membership:
            return redirect(url_for('course.index'))

    ip_address = request.remote_addr
    if not ip_address or ip_address == '127.0.0.1':
        ip_address = '186.189.195.217'
    if ipaddress.ip_address(ip_address).is_private:
        ip_address = '186.189.195.217'
    access_token = 'cda5ddffc1e6b7'
    handler = ipinfo.getHandler(access_token)

    details = handler.getDetails(ip_address)
    country_code = details.country
    country_code = str(country_code)
    lcase_country_code = country_code.lower()
    currency_id = 16

    for currency in data:
        if str(currency['code']) == country_code:
            currency_id = currency['id']
            break
    currency = Currency(currency_id)

    with Transaction().set_context(currency_web=currency_id):
        invoices = Invoice.search([
            ('party', '=', party.id),
            ('state', '=', 'posted'),
        ])

    if len(subscriptions) >= 1 and len(invoices) >= 1:
        credentials = current_app.config['QPAYPRO_CREDENTIALS']
        cc_form = CreditCardForm()

        defaults = {}
        defaults['sessionid'] = uniqid()
        defaults['orgid'] = 'k8vif92e'  #'1snn5n9w' TEST OR PRODUCTION
        defaults['merchantid'] = credentials['merchantid']  #'visanetgt_qpay'
        defaults['user'] = user
        defaults['cc_form'] = cc_form
        defaults['invoices'] = invoices
        defaults['subscriptions'] = subscriptions
        defaults['currency'] = currency

        url_test = 'https://sandbox.qpaypro.com/payment/api_v1'
        url_production = 'https://payments.qpaypro.com/checkout/api_v1'

        if cc_form.validate_on_submit():
            credit_card = str(cc_form.card_number.data)
            credit_card.replace(' ', '')
            #invoice, = invoices
            success = True
            for invoice in invoices:
                params = {}
                params['x_login'] = credentials['x_login']  #'visanetgt_qpay'
                params['x_private_key'] = credentials[
                    'x_private_key']  # '88888888888'
                params['x_api_secret'] = credentials[
                    'x_api_secret']  #'99999999999'
                params['x_product_id'] = invoice.lines[0].product.id  #6
                params['x_audit_number'] = random.randint(1, 999999)
                params[
                    'x_fp_sequence'] = invoice.number  #1988679099 #INVOICE SEQUENCE NUMBER
                params[
                    'x_invoice_num'] = invoice.id  #random.randint(1,999999) #INVOICE SEQUENCE NUMBER
                params['x_fp_timestamp'] = time()
                params['x_currency_code'] = 'GTQ'
                params[
                    'x_amount'] = invoice.total_amount  #1.00 #invoice.total_amount
                params['x_line_item'] = invoice.lines[
                    0].product.name  #'T-shirt Live Dreams<|>w01<|><|>1<|>1000.00<|>N'
                params['x_freight'] = 0.00
                params['x_email'] = '*****@*****.**'
                params['cc_name'] = cc_form.name.data  #'john doe'
                params['cc_number'] = credit_card  #'4111111111111111'
                params['cc_exp'] = str(cc_form.expiration_date.data)  #'01/21'
                params['cc_cvv2'] = cc_form.code.data  #'4567'
                params['cc_type'] = 'visa'
                params['x_first_name'] = user.party.name  #'john'
                params['x_last_name'] = user.party.name  #'doe'
                params['x_company'] = 'API CENTRO'  #'Company'
                params['x_address'] = user.party.city  #'711-2880 Nulla'
                params['x_city'] = user.party.city  #'Guatemala'
                params['x_state'] = user.party.city  #'Guatemala'
                params['x_country'] = user.party.country  #'Guatemala'
                params['x_zip'] = '09001'
                params['x_relay_response'] = 'none'
                params['x_relay_url'] = 'none'
                params['x_type'] = 'AUTH_ONLY'
                params['x_method'] = 'CC'
                params[
                    'http_origin'] = 'https://www.apixela.net'  #'http://local.test.com'
                params['visaencuotas'] = 0
                params['device_fingerprint_id'] = defaults['sessionid']

                response = requests.post(url=url_production, params=params)

                res = response.raise_for_status()
                response_content = response.json()

                #response_content = {'responseCode':"100",
                #    'idTransaction':"102030"}

                response_code = response_content['responseCode']
                message = get_code(response_code)

                if response_code == "00":
                    transaction_id = response_content['idTransaction']
                    Invoice.card_payment_succeed([invoice],
                                                 reference=transaction_id)
                else:
                    success = False
                    break
            if success:
                return render_template('bienvenido.html',
                                       flash_message=message,
                                       user=user)
            else:
                return render_template(
                    'bienvenido.html',
                    user=user,
                    flash_message=
                    "Something wrong. Sorry the inconvenient. We will call you later."
                )
        return render_template('course/pay_enroll.html', **defaults)
    elif len(subscriptions) >= 1 and user.party.receivable != 0:
        return render_template(
            'bienvenido.html',
            user=user,
            flash_message=
            "Something wrong. Sorry the inconvenient. We will call you later.")
    else:
        return redirect(url_for('course.index'))