Ejemplo n.º 1
0
    def post(self, request, format=None, *args, **kwargs):
        payment_id = request['object']['id']
        Payment.capture(payment_id)

        order = models.Order.objects.get(payment_id=payment_id)
        order.status = 'succeeded'
        order.save()
Ejemplo n.º 2
0
def before_payment_for_order_view(request):
    user = request.user
    user_cart = user.cart
    user_cart_price = user_cart.get_cart_price()

    payment = Payment.create(
        {
            "amount": {
                "value": str(user_cart_price),
                "currency": "RUB"
            },
            "confirmation": {
                "type": "redirect",
                "return_url": "http://127.0.0.1:8000"
            },
            "capture": True,
            "description": "Заказ №1"
        }, uuid.uuid4())

    order = models.Order(status='waiting_for_capture',
                         owner=request.user,
                         items=user_cart.items)
    order.save()

    return redirect(payment.confirmation.confirmation_url)
Ejemplo n.º 3
0
def donate_money(request):
    Configuration.account_id = '819176'
    Configuration.secret_key = 'live_Rj4b50yCpOzEirnhaHWgSJ_f-t9naP6WBSlAk8WlAss'

    #Configuration.account_id = '829811'
    #Configuration.secret_key = 'test_3z2IRsWt9h2FRrKVBy9AaGkeMHt6appEWllT9614G5k'

    # Configuration.configure('819176', 'test__QoWec5bBgd00kgqy4xnSz245AQk2faiTHjPJN7tkiQ')
    receipt = Receipt()
    receipt.customer = {"phone": "79990000000", "email": "*****@*****.**"}
    receipt.tax_system_code = 1
    receipt.items = [
        ReceiptItem({
            "description":
            "Пополнения баланса на сумму {}".format(request.POST.get('sum')),
            "quantity":
            1,
            "amount": {
                "value": request.POST.get('sum'),
                "currency": Currency.RUB
            },
            "vat_code":
            2,
            "capture":
            True
        }),
    ]

    s = {'user': str(request.user)}
    print(request.user)

    builder = PaymentRequestBuilder()
    builder.set_amount({"value": request.POST.get('sum'), "currency": Currency.RUB}) \
        .set_confirmation({"type": ConfirmationType.REDIRECT, "return_url": "https://privereda1.ru"}) \
        .set_capture(True) \
        .set_description("Заказ №72") \
        .set_metadata({"user": int(request.user.id)}) \
        .set_receipt(receipt)

    request = builder.build()
    # Можно что-то поменять, если нужно
    res = Payment.create(request)
    print(res.json())
    a = json.loads(res.json())
    print(a['confirmation']['confirmation_url'])
    var_dump.var_dump(res)
    # print(request.POST)
    # print(request.POST.get('sum'))
    return redirect(a['confirmation']['confirmation_url'])
Ejemplo n.º 4
0
    def generate_payment_yoo(
        self,
        phone: str,
        email: str,
        order_number: str,
        total_amount: int,
        products: List[models.OrderProduct],
    ) -> str:
        receipt = Receipt()
        receipt.tax_system_code = self._tax_system_code
        receipt.customer.phone = phone
        if email:
            receipt.customer.email = email

        receipt.items = [
            ReceiptItem(
                description=product.title,
                quantity=product.amount,
                amount={
                    "value": product.price,
                    "currency": Currency.RUB,
                },
                vat_code=2,  # self._tax_system_code ???
            ) for product in products
        ]

        builder = PaymentRequestBuilder()
        builder.set_amount({
            "value": total_amount,
            "currency": Currency.RUB
        }).set_confirmation({
            "type": ConfirmationType.EMBEDDED
        }).set_description("Заказ №" + order_number).set_metadata({
            "orderNumber":
            order_number
        }).set_receipt(receipt)

        # https://yookassa.ru/developers/payments/payment-process#capture-and-cancel
        builder.set_capture(True)

        request = builder.build()

        response = Payment.create(request, idempotency_key=order_number)
        return response.confirmation.confirmation_token
Ejemplo n.º 5
0
def create_invoice(chat_id):
    Configuration.account_id = get_from_env("SHOP_ID")
    Configuration.secret_key = get_from_env("PAYMENT_TOKEN")

    payment = Payment.create({
        "amount": {
            "value": "100.00",
            "currency": "RUB"
        },
        "confirmation": {
            "type": "redirect",
            "return_url": "https://www.google.com"
        },
        "capture": True,
        "description": "Заказ №1",
        "metadata": {
            "chat_id": chat_id
        }
    })

    return payment.confirmation.confirmation_url
Ejemplo n.º 6
0
def create(amount, description, data=None, renewal=None):
    """ Create a payment """

    req = {
        'amount': {
            'value': f'{amount}.00',
            'currency': 'RUB'
        },
        'description': description,
        'metadata': data,
    }

    if renewal:
        req['payment_method_id'] = renewal
    else:
        req['confirmation'] = {
            'type': 'embedded',
        }
        req['capture'] = True
        req['save_payment_method'] = True

    return Payment.create(req).id
Ejemplo n.º 7
0
def pay():
    Configuration.configure('<Идентификатор магазина>', '<Секретный ключ>')
    idempotence_key = str(uuid.uuid4())
    payment = Payment.create(
        {
            "amount": {
                "value": "2.00",
                "currency": "RUB"
            },
            "payment_method_data": {
                "type": "bank_card"
            },
            "confirmation": {
                "type": "redirect",
                "return_url": "http://http://127.0.0.1:8000/"
            },
            "description": "Заказ №72"
        }, idempotence_key)
    print('payment: ', payment)

    # get confirmation url
    confirmation_url = payment.confirmation.confirmation_url
Ejemplo n.º 8
0
def payment(request, order_id):
    order = get_object_or_404(Order, pk=order_id)
    if order.user.id != request.user.id:
        """ This is not the user's order, someone tries to hack us """
        return HttpResponseForbidden()

    Configuration.account_id = KASSA_ACCOUNT_ID
    Configuration.secret_key = KASSA_SECRET_KEY

    items = []
    for item in order.items.all():
        items.append({
            'description': str(item.product),
            'quantity': item.quantity,
            'amount': {
                'value':
                str(item.cost.quantize(Decimal('1'),
                                       rounding=ROUND_HALF_EVEN)),
                'currency':
                'RUB'
            },
            'vat_code': 1
        })
    if order.delivery_price > 0:
        items.append({
            'description': "Доставка",
            'quantity': 1,
            'amount': {
                'value':
                str(
                    order.delivery_price.quantize(Decimal('1'),
                                                  rounding=ROUND_HALF_EVEN)),
                'currency':
                'RUB'
            },
            'vat_code': 1
        })

    return_url = 'https://{}{}'.format(Site.objects.get_current().domain,
                                       reverse('shop:order', args=[order.id]))

    payment_details = {
        'amount': {
            'value':
            str(order.total.quantize(Decimal('1'), rounding=ROUND_HALF_EVEN)),
            'currency':
            'RUB'
        },
        'receipt': {
            'customer': {
                'phone': order.phone,
                'full_name': order.name
            },
            'tax_system_code': 2,
            'items': items
        },
        'confirmation': {
            'type': 'redirect',
            'return_url': return_url
        },
        'metadata': {
            'order_id': order.id,
            'user_id': order.user.id
        },
        'capture': True,
        'description': 'Заказ №{}'.format(order.id)
    }
    if order.email:
        payment_details['receipt']['customer']['email'] = order.email
    if order.payment == order.PAYMENT_CREDIT:
        payment_details['payment_method_data'] = {'type': 'installments'}

    payment = Payment.create(payment_details, str(uuid.uuid4()))
    return HttpResponseRedirect(payment.confirmation.confirmation_url)
Ejemplo n.º 9
0
def pay_request(order):
    from .models import PaymentObj
    delivery_price = order.delivery_price
    is_need_pack = order.is_need_pack
    print('delivery_price', delivery_price)
    order_total_price = order.total_price

    pack_price = 0
    if is_need_pack:
        pack_price = 300
    amount = order_total_price + delivery_price
    print('amount', amount)
    # payment_type = request.data.get('pay_type')

    Configuration.account_id = settings.YA_SHOP_ID
    Configuration.secret_key = settings.YA_API
    pay_id = uuid.uuid4()
    items = []

    for item in order.items.all():

        if is_need_pack:
            items.append({
                "description": 'Упаковка',
                "quantity": 1,
                "amount": {
                    "value": pack_price,
                    "currency": "RUB"
                },
                "vat_code": "2",
                "payment_mode": "full_prepayment",
                "payment_subject": "commodity"
            })

        if delivery_price > 0:
            items.append({
                "description": 'Доставка',
                "quantity": 1,
                "amount": {
                    "value": delivery_price,
                    "currency": "RUB"
                },
                "vat_code": "2",
                "payment_mode": "full_prepayment",
                "payment_subject": "commodity"
            })

        print('item price',item.price / item.quantity if not order.promo_code else
                        int((item.price / item.quantity) - ((item.price / item.quantity) * order.promo_code.discount / 100)))
        items.append({
                    "description": item.item_type.item.name,
                    "quantity": item.quantity,
                    "amount": {
                        "value": item.price / item.quantity if not order.promo_code else
                        int((item.price / item.quantity) - ((item.price / item.quantity) * order.promo_code.discount / 100)),
                        "currency": "RUB"
                    },
                    "vat_code": "2",
                    "payment_mode": "full_prepayment",
                    "payment_subject": "commodity"
                })

        # vat_code
        # 1        Без        НДС
        # 2        НДС        по        ставке        0 %
        # 3        НДС        по        ставке        10 %
        # 4        НДС        чека        по        ставке        20 %
        # 5        НДС        чека        по        расчетной        ставке        10 / 110
        # 6        НДС        чека        по        расчетной        ставке        20 / 120
    print(items)
    payment = Payment.create({
        "amount": {
            "value": amount,
            "currency": "RUB"
        },
        "receipt": {
            "customer": {
                "full_name": order.fio,
                "phone": order.phone
            },
            "items": items
        },
        # "payment_method": {
        #     "type": payment_type,
        # },
        "confirmation": {
            "type": "redirect",
            # "return_url": f'{settings.HOST}/payment_complete?pay_id={pay_id}'
            "return_url": f'{settings.HOST}'
        },
        "capture": True,
        "description": f'Оплата заказа ID {order.id}'
    }, pay_id)

    print(payment.id)
    ya_id = payment.id
    print(payment.confirmation.confirmation_url)



    new_payment = PaymentObj.objects.create(pay_id=pay_id,
                                            ya_id = ya_id,
                                            order=order,
                                            amount=amount,
                                            status='Не оплачен')

    if order.client:
        new_payment.client = order.client
    else:
        new_payment.guest = order.guest
    new_payment.save()
    return payment.confirmation.confirmation_url