Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

verify transaction on views.py #37

Open
wants to merge 1 commit into
base: futuria
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions portal/apps/core/templates/article/paywall/pw_auth_wo_credits.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,24 @@
Toastify.success('La transacción fue confirmada! Gracias por tu compra.', { duration: -1 });

if (hasReachedSufficientCommitment) {
$.ajax({
beforeSend: function (xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", Cookies.get('csrftoken'));
},
method: "POST",
url: "/usuarios/comprar-articulo/",
data: {
article_id: '{{ article.id }}',
user_id: '{{ user.id }}',
}
});
try {
$.ajax({
beforeSend: function (xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", Cookies.get('csrftoken'));
},
method: "POST",
url: "/usuarios/comprar-articulo/",
data: {
article_id: '{{ article.id }}',
user_id: '{{ user.id }}',
public_key: provider.publicKey.toString(),
signature,
transaction: serializedTransaction.toString('base64'),
}
});
} catch {
Toastify.error('Ocurrió un error al procesar la transacción.');
}

clearInterval(interval);
location.reload();
Expand Down
13 changes: 11 additions & 2 deletions portal/apps/thedaily/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
from .exceptions import UpdateCrmEx
from .tasks import send_notification, notify_digital, notify_paper

import solana
from hashlib import sha256

standard_library.install_aliases()
to_response = render_response('thedaily/templates/')
Expand Down Expand Up @@ -1601,10 +1603,17 @@ def telephone_subscription_msg(user, preferred_time):
@never_cache
@to_response
def buy_single_article(request):
# TODO: validate the transaction
# TODO: test the transaction validation
article_id = request.POST.get('article_id')
user_id = request.POST.get('user_id')
if request.method == 'POST' and article_id and user_id:
public_key = request.POST.get('public_key')
transaction = request.POST.get('transaction')
signature = request.POST.get('signature')
if request.method == 'POST' and article_id and user_id and public_key and transaction and signature:
hash = sha256(transaction).hexdigest()
valid = solana.signature.verify_signature(public_key, hash, signature)
if not valid:
return HttpResponse('Invalid signature', status=400)
user = get_object_or_404(User, id=user_id)
article = get_object_or_404(Article, id=article_id)
user.subscriber.articles_bought.add(article)
Expand Down
3 changes: 2 additions & 1 deletion portal/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ pywebpush
django>1.11,<2
future
factory-boy
w3storage
w3storage
solana