Exemple #1
0
def send_purchase_notification(payment_id):
    """
    Send a potential client who filled their data but not complete the buy-in face of a refused credit card
    :params payment_id: id of payment
    """
    payment = django_pagarme_facade.find_payment(payment_id)
    last_notification = payment.notifications.order_by('-creation').values(
        'status', 'creation').first()
    payment_profile = django_pagarme_facade.get_user_payment_profile(
        payment.user_id)
    payment_config_items = payment.items.all()
    purchased_items = [{
        "product_name": item.name,
        "quantity": '1',
        "price": str(item.price / 100),
    } for item in payment_config_items]

    purchase = {
        'created_at': last_notification['creation'].isoformat(),
        'transaction_id': payment.transaction_id,
        'name': payment_profile.name,
        'email': payment_profile.email,
        'phone': str(payment_profile.phone),
        'total_price': total_price(payment_config_items),
        'line_items': purchased_items,
        'payment_method': PAYMENT_METHOD_DCT[payment.payment_method],
        'financial_status': STATUS_DCT[last_notification['status']],
        'billet_url': payment.boleto_url,
        'billet_barcode': payment.boleto_barcode,
    }
    return requests.post(settings.HOTZAPP_API_URL, json=purchase).status_code
Exemple #2
0
def test_logged_user_payment_billing_address_data(resp_with_user, logged_user):
    profile: UserPaymentProfile = facade.get_user_payment_profile(logged_user)
    assert profile.to_billing_address_dict() == {
        'street': STREET,
        'complementary': COMPLEMENTARY,
        'street_number': STREET_NUMBER,
        'neighborhood': NEIGHBORHOOD,
        'city': CITY,
        'state': STATE,
        'zipcode': ZIPCODE,
        'country': ADDRESS_COUNTRY,
    }
Exemple #3
0
def create_subscription_and_activate_services(payment: PagarmePayment) -> Subscription:
    subscription = memberkit_facade.create_new_subscription(payment, 'Criação como resposta de pagamento no Pagarme')
    phone = None
    try:
        phone = pagarme_facade.get_user_payment_profile(subscription.subscriber)
    except pagarme_facade.UserPaymentProfileDoesNotExist:
        phone = None
    return activate_subscription_on_all_services(
        subscription,
        observation='Ativados serviços no Memberkit, Discourse e Active Campaign',
        phone=phone
    )
Exemple #4
0
def test_logged_user_payment_customer_data(resp_with_user, logged_user):
    profile: UserPaymentProfile = facade.get_user_payment_profile(logged_user)
    assert profile.to_customer_dict() == {
        'external_id': str(logged_user.id),
        'type': CUSTOMER_TYPE,
        'country': COSTUMER_COUNTRY,
        'documents': {
            'number': DOCUMENT_NUMBER,
            'type': DOCUMENT_TYPE,
        },
        'name': logged_user.first_name,
        'email': logged_user.email,
        'phone': PHONE.replace('+', ''),
    }
Exemple #5
0
def pagarme(request, slug):
    payment_item = facade.get_payment_item(slug)
    if not facade.is_payment_config_item_available(payment_item, request):
        return redirect(
            reverse('django_pagarme:unavailable', kwargs={'slug': slug}))
    open_modal = request.GET.get('open_modal', '').lower() == 'true'
    review_informations = not (request.GET.get('review_informations',
                                               '').lower() == 'false')
    customer_qs_data = {
        k: request.GET.get(k, '')
        for k in ['name', 'email', 'phone']
    }
    customer_qs_data = {k: v for k, v in customer_qs_data.items() if v}
    user = request.user
    address = None
    if user.is_authenticated:
        user_data = {
            'external_id': user.id,
            'name': user.first_name,
            'email': user.email
        }
        try:
            payment_profile = facade.get_user_payment_profile(user)
        except facade.UserPaymentProfileDoesNotExist:
            customer = ChainMap(customer_qs_data, user_data)
        else:
            customer = ChainMap(customer_qs_data,
                                payment_profile.to_customer_dict(), user_data)
            address = payment_profile.to_billing_address_dict()
    else:
        customer = customer_qs_data
    ctx = {
        'payment_item': payment_item,
        'open_modal': open_modal,
        'review_informations': review_informations,
        'customer': customer,
        'slug': slug,
        'address': address
    }
    suffix = slug.replace('-', '_')
    templates = [
        f'django_pagarme/pagarme_{suffix}.html', 'django_pagarme/pagarme.html'
    ]

    return render(request, templates, ctx)
Exemple #6
0
def contact_info(request, slug):
    payment_item = facade.get_payment_item(slug)
    if not facade.is_payment_config_item_available(payment_item, request):
        return redirect(
            reverse('django_pagarme:unavailable', kwargs={'slug': slug}))
    if request.method == 'GET':
        user = request.user
        if user.is_authenticated:
            try:
                payment_profile = facade.get_user_payment_profile(user.id)
            except facade.UserPaymentProfileDoesNotExist:
                form = facade.ContactForm({
                    'name': user.first_name,
                    'email': user.email
                })
            else:
                form = facade.ContactForm({
                    'name': payment_profile.name,
                    'email': payment_profile.email,
                    'phone': payment_profile.phone
                })
        else:
            form = facade.ContactForm()
        ctx = {'contact_form': form, 'slug': slug}
        return render(request, 'django_pagarme/contact_form.html', ctx)

    dct = {key: request.POST[key] for key in 'name phone email'.split()}
    dct['payment_item_slug'] = slug
    try:
        dct = facade.validate_and_inform_contact_info(user=request.user, **dct)
    except facade.InvalidContactData as e:
        ctx = {'contact_form': e.contact_form, 'slug': slug}
        resp = render(request,
                      'django_pagarme/contact_form_errors.html',
                      ctx,
                      status=400)
        return resp
    else:
        path = reverse('django_pagarme:pagarme', kwargs={'slug': slug})
        dct['open_modal'] = 'true'
        query_string = urlencode(dct)
        return redirect(f'{path}?{query_string}')
Exemple #7
0
def pagarme(request, slug):
    open_modal = request.GET.get('open_modal', '').lower() == 'true'
    review_informations = not (request.GET.get('review_informations',
                                               '').lower() == 'false')
    customer_qs_data = {
        k: request.GET.get(k, '')
        for k in ['name', 'email', 'phone']
    }
    customer_qs_data = {k: v for k, v in customer_qs_data.items() if v}
    user = request.user
    address = None
    if user.is_authenticated:
        user_data = {
            'external_id': user.id,
            'name': user.first_name,
            'email': user.email
        }
        try:
            payment_profile = facade.get_user_payment_profile(user)
        except facade.UserPaymentProfileDoesNotExist:
            customer = ChainMap(customer_qs_data, user_data)
        else:
            customer = ChainMap(customer_qs_data,
                                payment_profile.to_customer_dict(), user_data)
            address = payment_profile.to_billing_address_dict()
    else:
        customer = customer_qs_data
    ctx = {
        'payment_item': facade.get_payment_item(slug),
        'open_modal': open_modal,
        'review_informations': review_informations,
        'customer': customer,
        'slug': slug,
        'address': address
    }
    return render(request, 'django_pagarme/pagarme.html', ctx)
def test_refused_payment_user_profile_exists(refused_resp, user):
    assert facade.get_user_payment_profile(user) is not None
Exemple #9
0
def test_logged_user_payment_saved(resp_with_user, logged_user):
    assert facade.get_user_payment_profile(logged_user) is not None
Exemple #10
0
def test_no_user_payment_recording(resp_no_user, logged_user):
    with pytest.raises(facade.UserPaymentProfileDoesNotExist):
        facade.get_user_payment_profile(logged_user)
def test_logged_user_card_id(resp_with_user, logged_user):
    profile: UserPaymentProfile = facade.get_user_payment_profile(logged_user)
    assert profile.card_id == CARD_ID