ydaniels / django-cryptocurrency-payment

Simple and flexible app for accepting cryptocurrency payment with no much hassle
MIT License
36 stars 12 forks source link

local variable 'newkey' referenced before assignment #4

Open Michael-Lyon opened 3 years ago

Michael-Lyon commented 3 years ago

Latest version as at April 2021 Django 3 Python 3.9.1 Parrot Security

Description

I tried to implement the create_payment method and I kept getting "local variable 'newkey' referenced before assignment" error message.

What I Did

payment = create_new_payment(crypto='BITCOIN', #Cryptocurrency from your backend settings
                fiat_amount=10, #Amount of actual item in fiat
                fiat_currency='USD', #Fiat currency used to convert to crypto amount
                payment_title="tESTING",  #Title associated with payment
                payment_description="tESTING", #Description associated with payment
                related_object=None, #Generic linked object for this payment -> crypto_payments = GenericRelation(CryptoCurrencyPayment)
                user=request.user, #User of this payment for non-anonymous payment
                parent_payment=None, #Obvious
                address_index=None,# Use a particular address index for this payment
                reuse_address=None)
Michael-Lyon commented 3 years ago

Please i would love to get a response as soon as possible.

ydaniels commented 3 years ago

Hi can you post a tracebrack

Michael-Lyon commented 3 years ago

Traceback (most recent call last): File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request) File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/michaellyon/cryptSite/core/views.py", line 8, in home payment = create_new_payment(crypto='BITCOIN', #Cryptocurrency from your backend settings File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/cryptocurrency_payment/models.py", line 82, in create_new_payment address = backend_obj.generate_new_address(index=address_generated_count) File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/merchant_wallet/backends/btc.py", line 81, in generate_new_address address = btc.pubkey_to_address(btc.bip32_descend(self.public_key, [0, index])) File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/bitcoin/deterministic.py", line 198, in bip32_descend key = bip32_ckd(key, p) File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/bitcoin/deterministic.py", line 132, in bip32_ckd return bip32_serialize(raw_bip32_ckd(bip32_deserialize(data), i)) File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/bitcoin/deterministic.py", line 96, in raw_bip32_ckd return (vbytes, depth + 1, fingerprint, i, I[32:], newkey)

Exception Type: UnboundLocalError at / Exception Value: local variable 'newkey' referenced before assignment

ydaniels commented 3 years ago

Looks like the master public key you put in the django settings file is incorrect, make sure its your wallet extended public key starting with 'xpub' . You may need to convert your master public key to xpub format if your wallet uses another format.

Michael-Lyon commented 3 years ago

Ok. Thank you let me try that now.

Michael-Lyon commented 3 years ago

i have tried what you said about the xpub key. I think it worked, but I'm getting a constraint error.

Traceback (most recent call last): File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute return Database.Cursor.execute(self, query, params)

The above exception (UNIQUE constraint failed: cryptocurrency_payment_cryptocurrencypayment.id) was the direct cause of the following exception: File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request) File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, *callback_kwargs) File "/home/michaellyon/cryptSite/core/views.py", line 19, in home payment.save(True) File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/django/db/models/base.py", line 726, in save self.save_base(using=using, force_insert=force_insert, File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/django/db/models/base.py", line 763, in save_base updated = self._save_table( File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/django/db/models/base.py", line 868, in _save_table results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw) File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/django/db/models/base.py", line 906, in _do_insert return manager._insert( File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method return getattr(self.get_queryset(), name)(args, **kwargs) File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/django/db/models/query.py", line 1270, in _insert return query.get_compiler(using=using).execute_sql(returning_fields) File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1410, in execute_sql cursor.execute(sql, params) File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute return super().execute(sql, params) File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers return executor(sql, params, many, context) File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/django/db/utils.py", line 90, in exit raise dj_exc_value.with_traceback(traceback) from exc_value File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/home/michaellyon/cryptSite/env/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute return Database.Cursor.execute(self, query, params)

Exception Type: IntegrityError at / Exception Value: UNIQUE constraint failed: cryptocurrency_payment_cryptocurrencypayment.id

ydaniels commented 3 years ago

Check your migrations for issues or login to your django admin and see if there is a duplicate for cryptocurrencypayment model id field. You can also clear your migrations and restart again if you don't mind losing your current data.

Michael-Lyon commented 3 years ago

Thanks. Let me try this

Michael-Lyon commented 3 years ago

So the issue has been resolved. It returns the btc worth of the fait amount and an address.

Please how can I verify that a payment has been made so that I can update the status. and can I reduce the time for the payment to minutes please how can I generate a qr code scanner to make it easier to display on my page?

Thank you.

ydaniels commented 3 years ago

You can call this method cryptocurrency_payment.tasks.update_payment_status . It is going to automatically check and update the payment status.

Actually the best thing is to setup a per minute or 5-20 mins periodic task for these methods

cryptocurrency_payment.tasks.update_payment_status
cryptocurrency_payment.tasks.cancel_unpaid_payment
cryptocurrency_payment.tasks.refresh_payment_prices

Updating payment status, cancelling unpaid payment and refreshing prices is done automatically, you can then use signals to perform different action based on different payment status.

You can access or embed the payment page in your view visit

{your-base-url}/payment/{id-of-generated-payment}

It contains the qr code and every necessary information for payment. Another option is to use google js api for qr code. Check the config on the doc for more adjustment

Michael-Lyon commented 3 years ago

Hi. I hope you are having a good day. Thank you for all your response. I happen to have another question. I tried to retrieve all transactions made by a user, I want to loop through the data to be able to show the transaction that was cancelled, paid or just created. I want to be able to display these information on a table

ydaniels commented 3 years ago

Yea you can loop user.crypto_payments.all() and access field like status check model for more fields https://github.com/ydaniels/django-cryptocurrency-payment/blob/3a98453dea40b6570a233dab218f55a8e27c1c6c/cryptocurrency_payment/models.py#L104

Michael-Lyon commented 3 years ago

Thanks let me try that now

Michael-Lyon commented 3 years ago

I'm sorry i have been trying to look at what you said I should try but I do not seem to understand. is crypto_payments.all() a method and how to I access it with the user?

ydaniels commented 3 years ago

crypto_payments is a reverse relation to the user object, anywhere you have a user object you can access crypto_payments, its there already. For example you can access it directly from django request object in your view

for payment in request.user.crypto_payments.all():
     print(payment.status)
Michael-Lyon commented 3 years ago

Hi. Please one more question. Can I use the cryptocurrency payment to receive payments from other coins? Like usdt and please how can I set it up?

SixteenAngels commented 2 years ago

You should make a video