Example #1
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data()

        pub_hash = self.kwargs.get('pub_hash')
        context['wallet'] = Wallet.for_pub_hash(pub_hash)

        return context
Example #2
0
    def form_valid(self, form):
        amount = form.cleaned_data.get('amount')
        pub_hash = form.cleaned_data.get('pub_hash')

        wallet = Wallet.for_pub_hash(pub_hash)
        self.pub_hash = pub_hash
        
        return super().form_valid(form)
Example #3
0
def api_paypal_pay(request, pub_hash):
    wallet = Wallet.for_pub_hash(pub_hash)
    amount = to_ap(Decimal(request.data.get('amount')))

    # Generate Paypal URL
    charge = Charge.create_with_paypal(wallet, amount)

    return Response({'url': charge.paypal_approval_url})
Example #4
0
    def get_queryset(self):
        wallet = Wallet.for_pub_hash(self.kwargs.get('pub_hash'))
        user = wallet.user
        orders = user.orders.order_by('-date_created', 'id')
        for order in orders.blockchain_update_required():
            order.update_status_from_blockchain()

        return orders
Example #5
0
 def clean_pub_hash(self):
     pub_hash = self.cleaned_data['pub_hash']
     try:
         wallet = Wallet.for_pub_hash(pub_hash)
     except Wallet.DoesNotExist:
         raise forms.ValidationError(
             'Wallet does not exist',
         )
     return pub_hash
Example #6
0
    def form_valid(self, form):
        amount = form.cleaned_data.get('amount')
        pub_hash = form.cleaned_data.get('pub_hash')

        wallet = Wallet.for_pub_hash(pub_hash)
        wallet.create_cash_charge(to_ap(amount))

        self.pub_hash = pub_hash
        
        return super().form_valid(form)
Example #7
0
    def form_valid(self, form):
        amount = form.cleaned_data.get('amount')
        pub_hash = self.kwargs.get('pub_hash')

        amount_ap = to_ap(amount)

        wallet = Wallet.for_pub_hash(pub_hash)
        self.pub_hash = pub_hash

        self.payout = wallet.create_payout(amount_ap)
        
        return super().form_valid(form)
Example #8
0
def api_order_create(request):
    items = request.data.get('items')
    shop_data = request.data.get('shop')
    shop = get_object_or_404(Shop, pk=shop_data.get('id'))
    user_pubkey = request.data.get('pubkey')

    wallet = Wallet.for_pub_key(user_pubkey)
    user = wallet.user

    # TODO Make transaction out of this entire function.
    order = Order.objects.create(
        shop=shop,
        user=user,
        wallet=wallet,
        status=Order.STATUS.pending,
        total_amount=0,
    )

    total_amount = 0

    for item in items:
        product_id = item.get('id')
        count = item.get('count')

        if not count:
            continue

        product = Product.objects.get(pk=product_id, shops__in=[shop])

        amount = count * product.price
        line_item = LineItem(
            product_ref=product,
            product_price=product.price,
            product_name=product.name,
            product_description=product.description,
            product_picture=product.picture,
            product_icon=product.icon,
            order=order,
            count=count,
            total_amount=amount,
        )

        line_item.save()
        total_amount += product.price * count

    order.total_amount = total_amount
    order.save()

    order.tx = blockchain.get_transfer_tx(wallet.pub_key, shop.pub_key,
                                          total_amount)

    return Response(OrderSerializer(order).data)
Example #9
0
    def update_or_create(self, id=None, owner=None, festival=None, defaults=None):
        shop, created = Shop.objects.update_or_create(
            id=id,
            owner=owner,
            festival=festival,
            defaults=defaults
        )

        if created:
            shop.wallet = Wallet.create(owner, precharge=False)
            shop.save()

        return shop, created
Example #10
0
def api_voucher_submit(request, pub_hash):
    wallet = Wallet.for_pub_hash(pub_hash)
    code = request.data.get('code')

    print('CODE', code)

    try:
        charge = Charge.create_with_voucher(wallet, code)
    except InvalidVoucherException as e:
        resp = Response({'msg': 'Voucher not found.'},
                        status=status.HTTP_404_NOT_FOUND)
        return resp

    return Response({'id': charge.id, 'amount': charge.amount})
Example #11
0
def api_wristband_activate(request, token):
    wristband = Wristband.objects.filter(token=token).first()
    if wristband:
        return Response({'err': 'wristband already activated'})

    user = User.objects.create(
        email='wristband_{}@cryptofest.net'.format(token))

    wallet = Wallet.create(user)

    wristband = Wristband.objects.create(
        token=token,
        wallet=wallet,
        status=Wristband.STATUS.active,
    )

    return Response(serialize_wristband(wristband))
Example #12
0
def signup(request):
    pub_key = request.data.get('pub_key')

    try:
        wallet = Wallet.for_pub_key(pub_key)
    except Wallet.DoesNotExist:
        pub_hash = pub_key[-6:]
        user = User.objects.create(email='{}@cryptofest.net'.format(pub_hash))

        wallet = Wallet.objects.create(
            pub_key=pub_key,
            balance=0,
            user=user,
        )

        wallet.transfer_aeter(int(0.01 * 10e18))

    return Response(dict(id=wallet.user.id, ))
Example #13
0
 def get_initial(self):
     pub_hash = self.kwargs.get('pub_hash')
     wallet = Wallet.for_pub_hash(pub_hash)
     return {'amount': from_ap(wallet.maximum_payout())}
Example #14
0
 def get_queryset(self):
     wallet = Wallet.for_pub_hash(self.kwargs.get('pub_hash'))
     return wallet.charges.all().order_by('-date_created', 'id')
Example #15
0
def get_balance(request, pub):
    wallet = Wallet.for_pub_key(pub)
    balance = wallet.update_balance_from_blockchain()
    return JsonResponse({"balance": wallet.balance})