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
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)
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})
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
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
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)
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)
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)
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
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})
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))
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, ))
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())}
def get_queryset(self): wallet = Wallet.for_pub_hash(self.kwargs.get('pub_hash')) return wallet.charges.all().order_by('-date_created', 'id')
def get_balance(request, pub): wallet = Wallet.for_pub_key(pub) balance = wallet.update_balance_from_blockchain() return JsonResponse({"balance": wallet.balance})