Package: django-payment-manager
Django Payment AppThis is a Django payment application that allows you to easily integrate with PayPal to accept payments. This app is
designed to be flexible, allowing you to collect payments for any type of service or product.InstallationAdd the following code inside the<head>tag of yourbase.htmltemplate:<head><!-- other tags --><title>My title</title>{% block scriptHead %}{% endblock %}<!-- this line must be added --></head>To use the application, a model must be created with at least the following field :Create a model in your Django application that includes at least the following fields:fromdjango.dbimportmodelsfrompayment.utilsimportget_timestamp,generate_random_idclassInfoToPassToPaymentApplication(models.Model):id_request=models.CharField(max_length=255,null=True,blank=True)amount=models.DecimalField(max_digits=10,decimal_places=2,null=True,blank=True)currency=models.CharField(max_length=3,null=True,blank=True)# Add any other fields you wantdefsave(self,*args,**kwargs):ifself.id_requestisNone:self.id_request=f"{get_timestamp()}{generate_random_id()}"# You must create this function or use another method to generate a unique idreturnsuper().save(*args,**kwargs)defget_id_request(self):returnself.id_requestdefget_amount_to_pay(self):returnself.amountdefget_currency(self):returnself.currencydefset_paid_status(self,status:bool):self.is_paid=statusdefget_name(self):# return the name of the service or productpassdefget_img_url(self):# return the url of the image of the service or productpassdefget_user_name(self):# return the name of the user who is going to paypassdefget_user_email(self):# return the email of the user who is going to paypassThis model should represent the information that you want to pass to the payment application. This can include details
about the service or product you're selling, as well as any other necessary information.UsageWith your model created, you can now integrate the payment app with your existing application. Add the following
settings to your Django project's settings.py file, adjusting the values as needed:# settings.pyINSTALLED_APPS=[# other apps'payment',]# settings.py# payment settingsPAYMENT_PAYPAL_ENVIRONMENT='sandbox'# or 'production'PAYMENT_PAYPAL_CLIENT_ID='your_paypal_client_id'PAYMENT_PAYPAL_CLIENT_SECRET='your_paypal_client_secret'# fees# if you want to apply the default PayPal fee, set the following to True or add the fee to your modelPAYMENT_APPLY_PAYPAL_FEES=False# (2.90% + 0.35$ per transaction, if card 2.10% + 0.35$ as of 2023-05-06) default is FalsePAYMENT_SPLIT_PAYPAL_FEES=False# split the fees between you and the user (if PAYMENT_APPLY_PAYPAL_FEES = True)  default  is False# if you want to apply your own percentage fees (can't have both)PAYMENT_FEES=0.03ifnotPAYMENT_APPLY_PAYPAL_FEESelse0.00# default is 0.00# basicPAYMENT_BASE_TEMPLATE='base_templates/base.html'# or your own base template pathPAYMENT_WEBSITE_NAME='My Website'# or your website namePAYMENT_MODEL='your_app_name.InfoToPassToPaymentApplication'# Replace with your app and model namePAYMENT_REDIRECT_SUCCESS_URL='your_app_name:success_view_name'# Replace with your app and success view name# Additional to the default email settings# if the following are not defined, the default templates are appliedDEFAULT_FROM_EMAIL="webmaster@localhost"# the default from email that you're usingDEFAULT_EMAIL_MESSAGE=None# leave as None if you want to use the default message in the templateDEFAULT_EMAIL_PAYMENT_SUCCESS_TEMPLATE='template/emails/payment_success.html'# an example of nameDEFAULT_EMAIL_MESSAGE is the default message that you want to send to the user, leave empty if you want to
add one directly in the template or if you want to use the default one in the template which is :link=request.build_absolute_uri(payment_info.get_absolute_url())message=DEFAULT_EMAIL_MESSAGEorf"Thank you for your payment, it's been received and your booking is now confirmed"\f"You can view it by clicking <a href='{link}'>here</a> "\f"We're excited to have you on board! Your order # is{order_id}."The following objects are passed to the email template :context={'first_name':first_name,'company':PAYMENT_WEBSITE_NAME,"order_id":order_id,"payment_info":payment_info,'message':message,'current_year':datetime.datetime.now().year,}payment_info is an object that contains the following attributes :fromdjango.contrib.contenttypes.fieldsimportGenericForeignKeyfromdjango.contrib.contenttypes.modelsimportContentTypefromdjango.dbimportmodelsclassPaymentInfo:order_id=models.CharField(max_length=255,unique=True)reference_id=models.CharField(max_length=255)amount=models.DecimalField(max_digits=10,decimal_places=2)fee=models.DecimalField(max_digits=10,decimal_places=2,null=True,blank=True)currency=models.CharField(max_length=3)status=models.CharField(max_length=20)linked_object_type=models.ForeignKey(ContentType,on_delete=models.CASCADE,null=True,blank=True)linked_object_id=models.PositiveIntegerField(null=True,blank=True)linked_object=GenericForeignKey('linked_object_type','linked_object_id')# meta datacreated_at=models.DateTimeField(auto_now_add=True)updated_at=models.DateTimeField(auto_now=True)defget_order_id(self):returnself.order_iddefget_reference_id(self):returnself.reference_iddefget_amount(self):returnself.amountdefget_currency(self):returnself.currencydefget_status(self):returnself.statusdefget_linked_object(self):returnself.linked_objectdefget_created_at(self):returnself.created_atdefget_updated_at(self):returnself.updated_atdefget_total_amount(self):returnself.amount+self.feedefget_fee(self):returnself.feedefget_absolute_url(self):# implemented in the payment apppassYou can customize the message in the email by not using the object message and directly put your message in the
template or by adding a changing the default value of the message in the settings.The attribute in the payment_info object can be used to display the information in your template.Follow the instructions in the app's documentation to set up the required views.Once everything is set up, you can start accepting payments through PayPal.