CCrypto VPN public website https://vpn.ccrypto.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

111 lines
3.8 KiB

  1. import json
  2. from ipaddress import IPv4Address, IPv4Network
  3. from django.shortcuts import redirect
  4. from django.utils.translation import ugettext_lazy as _
  5. from django.core.urlresolvers import reverse
  6. from django.conf import settings as project_settings
  7. from .base import BackendBase
  8. class CoinbaseBackend(BackendBase):
  9. backend_id = 'coinbase'
  10. backend_verbose_name = _("Coinbase")
  11. backend_display_name = _("Bitcoin with CoinBase")
  12. def __init__(self, settings):
  13. self.sandbox = settings.get('SANDBOX', False)
  14. if self.sandbox:
  15. default_site = 'https://sandbox.coinbase.com/'
  16. default_base = 'https://api.sandbox.coinbase.com/'
  17. else:
  18. default_site = 'https://www.coinbase.com/'
  19. default_base = 'https://api.coinbase.com/'
  20. self.currency = settings.get('CURRENCY', 'EUR')
  21. self.key = settings.get('KEY')
  22. self.secret = settings.get('SECRET')
  23. self.base = settings.get('BASE_URL', default_base)
  24. self.site = settings.get('SITE_URL', default_site)
  25. self.callback_secret = settings.get('CALLBACK_SECRET')
  26. self.callback_source_ip = settings.get('CALLBACK_SOURCE', '54.175.255.192/27')
  27. if not self.key or not self.secret or not self.callback_secret:
  28. return
  29. from coinbase.wallet.client import Client
  30. self.client = Client(self.key, self.secret, self.base)
  31. self.backend_enabled = True
  32. def new_payment(self, payment):
  33. ROOT_URL = project_settings.ROOT_URL
  34. months = int(payment.time.days / 30)
  35. username = payment.user.username
  36. amount_str = '%.2f' % (payment.amount / 100)
  37. name = "%d months for %s" % (months, username)
  38. checkout = self.client.create_checkout(
  39. amount=amount_str,
  40. currency=self.currency,
  41. name=name,
  42. success_url=ROOT_URL + reverse('payments:view', args=(payment.id,)),
  43. cancel_url=ROOT_URL + reverse('payments:cancel', args=(payment.id,)),
  44. metadata={'payment_id': payment.id},
  45. )
  46. embed_id = checkout['embed_code']
  47. payment.backend_data['checkout_id'] = checkout['id']
  48. payment.backend_data['embed_code'] = checkout['embed_code']
  49. return redirect(self.site + 'checkouts/' + embed_id +
  50. '?custom=' + str(payment.id))
  51. def callback(self, Payment, request):
  52. if self.callback_source_ip:
  53. if ('.' in request.META['REMOTE_ADDR']) != ('.' in self.callback_source_ip):
  54. print("source IP version")
  55. print(repr(request.META.get('REMOTE_ADDR')))
  56. print(repr(self.callback_source_ip))
  57. return False # IPv6 TODO
  58. net = IPv4Network(self.callback_source_ip)
  59. if IPv4Address(request.META['REMOTE_ADDR']) not in net:
  60. print("source IP")
  61. return False
  62. secret = request.GET.get('secret')
  63. if secret != self.callback_secret:
  64. print("secret")
  65. return False
  66. data = json.loads(request.body.decode('utf-8'))
  67. order = data.get('order')
  68. if not order:
  69. # OK but we don't care
  70. print("order")
  71. return True
  72. id = order.get('custom')
  73. try:
  74. payment = Payment.objects.get(id=id)
  75. except Payment.DoesNotExist:
  76. # Wrong ID - Valid request, ignore
  77. print("wrong payment")
  78. return True
  79. button = order.get('button')
  80. if not button:
  81. # Wrong structure.
  82. print("button")
  83. return False
  84. payment.status = 'confirmed'
  85. payment.save()
  86. payment.user.vpnuser.add_paid_time(payment.time)
  87. payment.user.vpnuser.on_payment_confirmed(payment)
  88. payment.user.vpnuser.save()
  89. return True