├── main ├── __init__.py ├── migrations │ ├── __init__.py │ └── 0001_initial.py ├── tests.py ├── apps.py ├── admin.py ├── urls.py ├── models.py ├── templates │ ├── error.html │ └── home.html └── views.py ├── myproject ├── __init__.py ├── urls.py ├── asgi.py ├── wsgi.py └── settings.py ├── .gitattributes ├── .env.example ├── requirements.txt ├── README.md ├── manage.py └── .gitignore /main/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /myproject/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /main/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * linguist-language=Python 2 | -------------------------------------------------------------------------------- /main/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /main/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class MainConfig(AppConfig): 5 | name = 'main' 6 | -------------------------------------------------------------------------------- /.env.example: -------------------------------------------------------------------------------- 1 | 2 | DEBUG=True 3 | TEMPLATE_DEBUG=True 4 | BASE_URL=http://127.0.0.1:8000/ 5 | IDPAY_API_KEY=YOUR-IDPAY-API-KEY 6 | IDPAY_SANDBOX=True -------------------------------------------------------------------------------- /main/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from .models import Main 3 | 4 | # Register your models here. 5 | 6 | admin.site.register(Main) 7 | -------------------------------------------------------------------------------- /myproject/urls.py: -------------------------------------------------------------------------------- 1 | 2 | from django.contrib import admin 3 | from django.urls import path , include 4 | 5 | urlpatterns = [ 6 | path('admin/', admin.site.urls), 7 | path('', include('main.urls')), 8 | ] 9 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | appdirs==1.4.4 2 | asgiref~=3.2.10 3 | attrs==19.3.0 4 | cached-property==1.5.1 5 | certifi==2020.4.5.1 6 | chardet==3.0.4 7 | defusedxml==0.6.0 8 | Django>=3.0.7 9 | idna==2.9 10 | idpay==0.0.1 11 | isodate==0.6.0 12 | lxml==4.6.5 13 | python-decouple==3.1 14 | pytz==2020.1 15 | requests==2.23.0 16 | requests-toolbelt==0.9.1 17 | six==1.14.0 18 | sqlparse==0.3.1 19 | urllib3==1.26.5 20 | -------------------------------------------------------------------------------- /myproject/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for myproject project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /myproject/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for myproject project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /main/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path , include 2 | from . import views 3 | 4 | urlpatterns = [ 5 | 6 | path('', views.home, name='home'), 7 | path('payment', views.payment_start, name='payment_start'), 8 | path('payment/return', views.payment_return, name='payment_return'), 9 | path('payment/check/', views.payment_check, name='payment_check'), 10 | path('requirement', views.requirement, name='requirement'), 11 | path('about-me', views.about_me, name='about_me'), 12 | ] -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # IDPay on Django 2 | 3 | ### Requirements 4 | 5 | - Django v3.0.3 6 | - idpay v0.0.1 7 | 8 | ### Deployment 9 | 10 | ```bash 11 | git clone git@github.com:idpay/python-django.git 12 | cd python-django 13 | # Create virtualenv named build 14 | virtualenv -p python3 build 15 | source build/bin/activate 16 | # Set your api-key 17 | cp .env.example .env 18 | vi .env 19 | # Install requirements, migration and run 20 | pip install -r requirements.txt 21 | python manage.py migrate 22 | python manage.py runserver 23 | ``` 24 | -------------------------------------------------------------------------------- /main/models.py: -------------------------------------------------------------------------------- 1 | from __future__ import unicode_literals 2 | from django.db import models 3 | 4 | # Create your models here. 5 | 6 | 7 | class Main(models.Model): 8 | 9 | order_id = models.TextField() 10 | payment_id = models.TextField() 11 | amount = models.IntegerField() 12 | date = models.TextField(default='-') 13 | 14 | card_number = models.TextField(default="****") 15 | idpay_track_id = models.IntegerField(default=0000) 16 | bank_track_id = models.TextField(default=0000) 17 | 18 | status = models.IntegerField(default=0) 19 | 20 | def __str__(self): 21 | return str(self.pk) + " | " + self.order_id + " | " + str(self.status) -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') 9 | try: 10 | from django.core.management import execute_from_command_line 11 | except ImportError as exc: 12 | raise ImportError( 13 | "Couldn't import Django. Are you sure it's installed and " 14 | "available on your PYTHONPATH environment variable? Did you " 15 | "forget to activate a virtual environment?" 16 | ) from exc 17 | execute_from_command_line(sys.argv) 18 | 19 | 20 | if __name__ == '__main__': 21 | main() 22 | -------------------------------------------------------------------------------- /main/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.0.3 on 2020-02-04 13:33 2 | 3 | from django.db import migrations, models 4 | import uuid 5 | 6 | 7 | class Migration(migrations.Migration): 8 | 9 | initial = True 10 | 11 | dependencies = [ 12 | ] 13 | 14 | operations = [ 15 | migrations.CreateModel( 16 | name='Main', 17 | fields=[ 18 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 19 | ('order_id', models.UUIDField(default=uuid.uuid1, unique=True)), 20 | ('payment_id', models.TextField()), 21 | ('amount', models.IntegerField()), 22 | ('date', models.TextField(default='-')), 23 | ('card_number', models.TextField(default='****')), 24 | ('idpay_track_id', models.IntegerField(default=0)), 25 | ('bank_track_id', models.TextField(default=0)), 26 | ('status', models.IntegerField(default=0)), 27 | ], 28 | ), 29 | ] 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io 2 | 3 | ### OSX ### 4 | .DS_Store 5 | .AppleDouble 6 | .LSOverride 7 | 8 | # Icon must end with two \r 9 | Icon 10 | 11 | 12 | # Thumbnails 13 | ._* 14 | 15 | # Files that might appear on external disk 16 | .Spotlight-V100 17 | .Trashes 18 | 19 | # Directories potentially created on remote AFP share 20 | .AppleDB 21 | .AppleDesktop 22 | Network Trash Folder 23 | Temporary Items 24 | .apdisk 25 | 26 | 27 | ### Python ### 28 | # Byte-compiled / optimized / DLL files 29 | __pycache__/ 30 | *.py[cod] 31 | .venv/ 32 | 33 | # C extensions 34 | *.so 35 | 36 | # Distribution / packaging 37 | .Python 38 | env/ 39 | build/ 40 | develop-eggs/ 41 | dist/ 42 | downloads/ 43 | eggs/ 44 | lib/ 45 | lib64/ 46 | parts/ 47 | sdist/ 48 | var/ 49 | *.egg-info/ 50 | .installed.cfg 51 | *.egg 52 | 53 | # PyInstaller 54 | # Usually these files are written by a python script from a template 55 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 56 | *.manifest 57 | *.spec 58 | 59 | # Installer logs 60 | pip-log.txt 61 | pip-delete-this-directory.txt 62 | 63 | # Unit test / coverage reports 64 | htmlcov/ 65 | .tox/ 66 | .coverage 67 | .cache 68 | nosetests.xml 69 | coverage.xml 70 | 71 | # Translations 72 | *.mo 73 | *.pot 74 | 75 | # Sphinx documentation 76 | docs/_build/ 77 | 78 | # PyBuilder 79 | target/ 80 | 81 | 82 | ### Django ### 83 | *.log 84 | *.pot 85 | *.pyc 86 | __pycache__/ 87 | local_settings.py 88 | 89 | .env 90 | db.sqlite3 91 | 92 | # Editor directories and files 93 | .idea/ 94 | .vscode/ 95 | -------------------------------------------------------------------------------- /main/templates/error.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | IDPay 13 | 14 | 15 | 16 | 17 | 31 | 32 |



33 | 34 | 35 | 36 |
37 |
38 |
39 |
40 | 41 | 46 | 47 |
48 |
49 |
50 |
51 | 52 | 53 | 54 |
55 |
56 |
57 |
58 | 59 |
60 | {% csrf_token %} 61 | 62 | 63 | 64 | 65 |
66 | 67 |
68 |
69 |
70 |
71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /myproject/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for myproject project. 3 | 4 | Generated by 'django-admin startproject' using Django 3.0.1. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.0/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/3.0/ref/settings/ 11 | """ 12 | 13 | import os 14 | 15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = '!sd7*#s!f@*2%zs!4f2emzv%%&(4@vvm-tvj24_9$elf-61zis' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'main', 41 | ] 42 | 43 | MIDDLEWARE = [ 44 | 'django.middleware.security.SecurityMiddleware', 45 | 'django.contrib.sessions.middleware.SessionMiddleware', 46 | 'django.middleware.common.CommonMiddleware', 47 | 'django.middleware.csrf.CsrfViewMiddleware', 48 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 49 | 'django.contrib.messages.middleware.MessageMiddleware', 50 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 51 | ] 52 | 53 | ROOT_URLCONF = 'myproject.urls' 54 | 55 | TEMPLATES = [ 56 | { 57 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 58 | 'DIRS': [], 59 | 'APP_DIRS': True, 60 | 'OPTIONS': { 61 | 'context_processors': [ 62 | 'django.template.context_processors.debug', 63 | 'django.template.context_processors.request', 64 | 'django.contrib.auth.context_processors.auth', 65 | 'django.contrib.messages.context_processors.messages', 66 | ], 67 | }, 68 | }, 69 | ] 70 | 71 | WSGI_APPLICATION = 'myproject.wsgi.application' 72 | 73 | 74 | # Database 75 | # https://docs.djangoproject.com/en/3.0/ref/settings/#databases 76 | 77 | DATABASES = { 78 | 'default': { 79 | 'ENGINE': 'django.db.backends.sqlite3', 80 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 81 | } 82 | } 83 | 84 | 85 | # Password validation 86 | # https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators 87 | 88 | AUTH_PASSWORD_VALIDATORS = [ 89 | { 90 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 91 | }, 92 | { 93 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 94 | }, 95 | { 96 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 97 | }, 98 | { 99 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 100 | }, 101 | ] 102 | 103 | 104 | # Internationalization 105 | # https://docs.djangoproject.com/en/3.0/topics/i18n/ 106 | 107 | LANGUAGE_CODE = 'en-us' 108 | 109 | TIME_ZONE = 'UTC' 110 | 111 | USE_I18N = True 112 | 113 | USE_L10N = True 114 | 115 | USE_TZ = True 116 | 117 | 118 | # Static files (CSS, JavaScript, Images) 119 | # https://docs.djangoproject.com/en/3.0/howto/static-files/ 120 | 121 | STATIC_URL = '/static/' 122 | -------------------------------------------------------------------------------- /main/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render, get_object_or_404, redirect 2 | from .models import Main 3 | from django.views.decorators.csrf import csrf_exempt 4 | from decouple import config 5 | from idpay.api import IDPayAPI 6 | 7 | import requests 8 | import json 9 | import uuid 10 | 11 | 12 | def payment_init(): 13 | base_url = config('BASE_URL', default='http://127.0.0.1', cast=str) 14 | api_key = config('IDPAY_API_KEY', default='', cast=str) 15 | sandbox = config('IDPAY_SANDBOX', default=True, cast=bool) 16 | 17 | return IDPayAPI(api_key, base_url, sandbox) 18 | 19 | def home(request): 20 | payments = Main.objects.all() 21 | return render(request, 'home.html', {'payments': payments}) 22 | 23 | def payment_start(request): 24 | if request.method == 'POST': 25 | 26 | order_id = uuid.uuid1() 27 | amount = request.POST.get('amount') 28 | 29 | payer = { 30 | 'name': request.POST.get('name'), 31 | 'phone': request.POST.get('phone'), 32 | 'mail': request.POST.get('mail'), 33 | 'desc': request.POST.get('desc'), 34 | } 35 | 36 | 37 | record = Main(order_id=order_id, amount=int(amount)) 38 | record.save() 39 | 40 | idpay_payment = payment_init() 41 | result = idpay_payment.payment(str(order_id), amount, 'payment/return', payer) 42 | 43 | if 'id' in result: 44 | record.status = 1 45 | record.payment_id = result['id'] 46 | record.save() 47 | 48 | return redirect(result['link']) 49 | 50 | else: 51 | txt = result['message'] 52 | else: 53 | txt = "Bad Request" 54 | 55 | return render(request, 'error.html', {'txt': txt}) 56 | 57 | 58 | @csrf_exempt 59 | def payment_return(request): 60 | if request.method == 'POST': 61 | 62 | pid = request.POST.get('id') 63 | status = request.POST.get('status') 64 | pidtrack = request.POST.get('track_id') 65 | order_id = request.POST.get('order_id') 66 | amount = request.POST.get('amount') 67 | card = request.POST.get('card_no') 68 | date = request.POST.get('date') 69 | 70 | if Main.objects.filter(order_id=order_id, payment_id=pid, amount=amount, status=1).count() == 1: 71 | 72 | idpay_payment = payment_init() 73 | 74 | payment = Main.objects.get(payment_id=pid, amount=amount) 75 | payment.status = status 76 | payment.date = str(date) 77 | payment.card_number = card 78 | payment.idpay_track_id = pidtrack 79 | payment.save() 80 | 81 | if str(status) == '10': 82 | result = idpay_payment.verify(pid, payment.order_id) 83 | 84 | if 'status' in result: 85 | 86 | payment.status = result['status'] 87 | payment.bank_track_id = result['payment']['track_id'] 88 | payment.save() 89 | 90 | return render(request, 'error.html', {'txt': result['message']}) 91 | 92 | else: 93 | txt = result['message'] 94 | 95 | else: 96 | txt = "Error Code : " + str(status) + " | " + "Description : " + idpay_payment.get_status(status) 97 | 98 | else: 99 | txt = "Order Not Found" 100 | 101 | else: 102 | txt = "Bad Request" 103 | 104 | return render(request, 'error.html', {'txt': txt}) 105 | 106 | 107 | def payment_check(request, pk): 108 | 109 | payment = Main.objects.get(pk=pk) 110 | 111 | idpay_payment = payment_init() 112 | result = idpay_payment.inquiry(payment.payment_id, payment.order_id) 113 | 114 | if 'status' in result: 115 | 116 | payment.status = result['status'] 117 | payment.idpay_track_id = result['track_id'] 118 | payment.bank_track_id = result['payment']['track_id'] 119 | payment.card_number = result['payment']['card_no'] 120 | payment.date = str(result['date']) 121 | payment.save() 122 | 123 | return render(request, 'error.html', {'txt': result['message']}) 124 | 125 | 126 | def requirement(request): 127 | txt = "pip install idpay" 128 | 129 | return render(request, 'error.html', {'txt': txt}) 130 | 131 | 132 | def about_me(request): 133 | txt = 'IDPay' 134 | 135 | return render(request, 'error.html', {'txt': txt}) 136 | -------------------------------------------------------------------------------- /main/templates/home.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | IDPay 13 | 14 | 15 | 16 | 17 | 31 | 32 |



33 | 34 |
35 |
36 |
37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | {% for payment in payments %} 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | {% endfor %} 69 | 70 | 71 |
#Order IDPayment IDAmountCard NumberIDPay Track IDBank Track IDStatusCheck
{{payment.pk}}{{payment.order_id}}{{payment.payment_id}}{{payment.amount}}{{payment.card_number }}{{payment.idpay_track_id}}{{payment.bank_track_id}}{{payment.status}}Check
72 | 73 | 74 |
75 |
76 |
77 | 78 |

79 | 80 |
81 |
82 |
83 |
84 | 85 |
86 | {% csrf_token %} 87 | 88 | 89 |

90 | 91 | 92 |

93 | 94 | 95 |

96 | 97 | 98 |

99 | 100 | 101 |

102 | 103 | 104 | 105 |
106 | 107 |
108 |
109 |
110 |
111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | --------------------------------------------------------------------------------