Skip to main content

A Django app for integrating with Cryptomus payment gateway.

Project description

Django Cryptomus

Django Cryptomus is a Django package designed to enhance web applications with robust payment processing capabilities using Cryptomus services. Cryptomus provides a secure and dynamic platform for managing payment transactions through various cryptocurrencies.

About Cryptomus

Cryptomus is a platform that facilitates secure and efficient cryptocurrency payment processing solutions for businesses and developers. It allows users to create invoices, handle payment transactions, and customize payment forms and callbacks according to specific business needs.

With Django Cryptomus, developers can integrate Cryptomus functionalities seamlessly into their Django applications. This package offers dynamic configuration settings, enabling developers to tailor security measures and payment processes based on their project requirements.

Features

  • Advanced Security Capabilities: This package provides advanced features such as data encryption, access control, and detection of security threats.
  • Dynamic Configuration Settings: All settings in this package are dynamically configurable via an admin panel.
  • Ease of Use: Designed to be highly intuitive, making it easy for developers familiar with Django to integrate and customize security features.

Installation

To install via pip, simply run:

pip install django-rest-cryptomus

Then, add it to the INSTALLED_APPS list in your project settings:

# settings.py

INSTALLED_APPS = [
    ...
    'django_cryptomus',
    'rest_framework',
]

Configuration

Add the following required settings to your Django settings file (settings.py):

# Cryptomus API configuration
CRYPTOMUS_API_KEY = 'your_api_key'
CRYPTOMUS_MERCHANT = 'your_merchant'
CRYPTOMUS_BASE_URL = 'your_base_url'  # For local testing, see below

Local Testing

If you want to test the integration locally, you need to expose your local server to the internet. You can use tools like ngrok or pinggy to achieve this.

Using ngrok

  1. Install ngrok from ngrok.com.

  2. Run the following command to start ngrok and expose your local server:

    ngrok http 8000
    
  3. Use the URL provided by ngrok (e.g., https://your-subdomain.ngrok.io) as your CRYPTOMUS_BASE_URL.

Using pinggy

  1. Use the following command to start pinggy and expose your local server:

    ssh -p 443 -R0:localhost:8000 -L4300:localhost:4300 qr@a.pinggy.io
    
  2. Use the URL provided by pinggy as your CRYPTOMUS_BASE_URL.

Usage

Include the django_cryptomus URLs in your project's urls.py:

from django.urls import path, include

urlpatterns = [
    ...
    path('payment/', include("django_cryptomus.urls", namespace="django_cryptomus")),
]

Custom Form Integration

You have the flexibility to create your own custom HTML form and use it instead of the default form provided by Django Cryptomus. To integrate your custom form, follow these steps:

  1. Create your custom HTML form with the desired fields and styling. You can name this file custom_payment_form.html.

  2. In your Django project, specify the path to your custom form in the settings:

#settings.py

CRYPTOMUS_CUSTOM_PAYMENT_FORM = 'templates_path/custom_payment_form.html'

JSON Example for Payment Data

In addition to using an HTML form, you can also provide payment data in JSON format. This can be useful for API integrations or other programmatic uses. Here is an example of the required fields in JSON format:

{
    "amount": "100.00",
    "currency": "USD",
    "order_id": "123456",
    "additional_data": {
        "network": "BTC",
        "is_payment_multiple": true,
        "lifetime": 3600,
        "to_currency": "BTC",
        "subtract": 0,
        "accuracy_payment_percent": 0,
        "additional_data": "Additional information for merchant",
        "currencies": ["BTC", "ETH", "LTC"],
        "except_currencies": [],
        "course_source": "Binance",
        "from_referral_code": "your_referral_code",
        "discount_percent": 5,
        "is_refresh": false
    }
}

To use JSON instead of a form, simply send a POST request to the appropriate endpoint with the JSON data. This approach allows for more flexibility and can easily be integrated into various systems and applications.

Additional Data

The additional_data field in JSON is used to send additional data and advanced settings in the payment transaction request. This field is optional, meaning you can include it or omit it based on your requirements.

To get detailed information about each parameter within additional_data and how to use them, please refer to the official Cryptomus documentation:

You can find comprehensive details about each parameter in the additional_data field, enabling you to utilize them effectively for your needs.

Customizing Cryptomus Callback View and Using CryptoMusPayment Model

If you want to customize the behavior of the callback view provided by Django Cryptomus and use the CryptoMusPayment model to manage payment records, follow these steps:

  1. Subclass the CryptomusCallbackView class and import the CryptoMusPayment model into your Django project:
# views.py

from rest_framework import status
from rest_framework.response import Response
from django_cryptomus.views import CryptomusCallbackView
from django_cryptomus.models import CryptoMusPayment

class CustomCryptomusCallbackView(CryptomusCallbackView):
    def post(self, request, *args, **kwargs):
        data = request.data
        if data['status'] == 'paid':
            cryptomus_payment = CryptoMusPayment.objects.filter(order_id=data['order_id'])
            
            cryptomus_payment.update(
                payment_amount_usd=data['payment_amount_usd'],
                payer_currency=data['payer_currency'],
                from_address=data['from'],
                status=data['status'],
                txid=data['txid'],
                payment_data=data
            )
            
            return Response({'message': 'Payment status updated successfully.'}, status=status.HTTP_200_OK)
        else:
            return Response({'error': 'Invalid payment status.'}, status=status.HTTP_400_BAD_REQUEST)

Specify the path to your custom callback view class in the settings:

# settings.py

CRYPTOMUS_CALLBACK_VIEW_CLASS = 'yourapp.views.CustomCryptomusCallbackView'

Customizing Success View

To customize the success page of Cryptomus payment, you can create a custom view class and specify its path using the CRYPTOMUS_SUCCESS_VIEW_CLASS variable in your Django project settings.

Example Custom Success View

# views.py

from django_cryptomus.views import SuccessCryptomusView
from django.http import HttpResponse
from django.shortcuts import render

class CustomSuccessCryptomusView(SuccessCryptomusView):

    def get(self, request):
        """
        Handle GET requests to render the success page.

        Renders a success page template specified in

Setting Custom Success View To set the custom success view, you need to specify the path to your custom view class in the settings.py file of your Django project:

# settings.py

CRYPTOMUS_SUCCESS_VIEW_CLASS = 'yourapp.views.CustomCryptomusSuccessView'

Customizing Success Template

To customize the success page template for Cryptomus payment, you can create a custom HTML template and specify its path using the CRYPTOMUS_PAYMENT_SUCCESS_HTML variable in your Django project settings.

Example Custom Success Template

<!-- success_page.html -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Payment Success</title>
    <!-- Add your custom CSS stylesheets or scripts here -->
</head>
<body>
    <div class="container">
        <h1>Payment Successful!</h1>
        <p>Thank you for your payment.</p>
        <!-- Add additional content or dynamic data here -->
    </div>
</body>
</html>

Setting Custom Success Template To set the custom success template, you need to specify the path to your custom HTML template in the settings.py file of your Django proje

# settings.py

CRYPTOMUS_PAYMENT_SUCCESS_HTML = 'cryptopay/success_page.html'

Then, you can utilize the dynamic configuration settings provided by this package to tailor all aspects of your site's security.

Contributing

We welcome contributions and feedback from the community. To report issues, make suggestions, or submit pull requests, please visit our GitHub repository.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-rest-cryptomus-0.2.7.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_rest_cryptomus-0.2.7-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file django-rest-cryptomus-0.2.7.tar.gz.

File metadata

  • Download URL: django-rest-cryptomus-0.2.7.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.6

File hashes

Hashes for django-rest-cryptomus-0.2.7.tar.gz
Algorithm Hash digest
SHA256 bb14d0f555676910524d37f6be836a4cbce6e2c68c0ee571d95d51b840539307
MD5 9c9b60e6d370870060a9bdd41d110362
BLAKE2b-256 2acdb337811e1ff12327e6fb18593f63fef341698103aa1de4ad81db8faecd45

See more details on using hashes here.

File details

Details for the file django_rest_cryptomus-0.2.7-py3-none-any.whl.

File metadata

File hashes

Hashes for django_rest_cryptomus-0.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 f895c21f56ffa7146b71201c61b9b076aebdf41cede5743cb913f92d3e6ad740
MD5 5a010331fa772ccc4059e946851c34da
BLAKE2b-256 a5d12a89f7acf970a116a13da84212ab84c096c5362d94c11359554b17a60771

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page