Learn how to integrate PayPal, which is one of the most popular payment gateways.
- [Instructor] Hello. Welcome to a new section of this video course, Integrating a Payment Gateway. In this section, you'll learn how to integrate a payment gateway into your site. You'll also extend the administration site to manage your orders and export them to different formats. This section will demonstrate these topics. Integrate a payment gateway into your project. Manage payment notifications. Export orders to CSV files. Create custom views for the administration site. Generate PDF invoices dynamically.
Now we move on to the first video of this section, Integrating a Payment Gateway. A payment gateway allows you to process payments online. Using a payment gateway, you can manage customers' orders and delegate payment processing through a reliable, secure third party. This means you don't have to worry about storing credit cards in your own system. There are many payment gateway providers to choose from. We're going to integrate PayPal, which is one of the most popular payment gateways. You'll also see a few topics which will give a brief explanation about a payment gateway, creating a PayPal account, installing django-paypal, adding the payment gateway, using PayPal Sandbox.
PayPal offers several methods to integrate its gateway into your site. The standard integration consists of a Buy Now button, which you probably have seen on other websites. The button redirects buyers to PayPal to process the payment. We're going to integrate PayPal Payments Standard, including a custom Buy Now button in our site. PayPal will process the payment and send a notification to our server indicating the payment status. Creating a PayPal account. You need to have a PayPal business account to integrate the payment gateway into your site. If you don't own a PayPal account yet, sign up here.
Make sure you choose a business account and sign in to the PayPal Payments Standard solution. Fill in your details in the sign up form and complete the sign up process. PayPal will send you an email to verify your account. Installing django-paypal. django-paypal is a third-party Django application that simplifies integrating PayPal into Django projects. We're going to use it to integrate the PayPal Payments Standard solution in our shop. You can find django-paypal's documentation at this site. Install django-paypal from the shell with the command prompt by using this command.
The installation will take some time, so we need to wait until we get the successfully installed message on the command prompt. Edit the settings.py file of your project, and add paypal.standard.ipn to the INSTALLED_APPS setting. This is the application provided by django-paypal to integrate PayPal's Payments Standard with Instant Payment Notification, IPN.
We're going to handle payment notifications later. At the end, add django-paypal setting command to the settings.py file of myshop to configure django-paypal. We'll see how the PayPal command works. PAYPAL_RECEIVER_EMAIL, the email of your PayPal account. Replace my PayPal email, myshop.com, with the email you used to create your PayPal account.
PAYPAL_TEST, a boolean indicating whether the PayPal Sandbox environment should be used to process payments. The Sandbox allows you to test your PayPal integration before moving to a production environment. Open the shell and run the migrate command to sync models of django-paypal with the database. You'll get an output which is the same as on the screen. The models of django-paypal are now synced with the database. You also need to add the URL patterns of django-paypal to your project.
Edit the main.urls.py file located in the myshop directory, and add the URL pattern. Remember to place it before the shop.urls pattern to avoid wrong pattern match. Let's add the payment gateway to the checkout process. Adding the payment gateway. The checkout process should work as follows.
One, the users add items to their shopping cart. Two, users check out their shopping cart. Three, users are redirected to PayPal for payment. Four, PayPal sends payment notification to our site. Five, PayPal redirects the users back to our website. Create a new application in your project using the startapp payment command. We're going to use this application to manage the checkout process and user payments. Edit the settings.py file of your project, and add payment to the INSTALLED_APPS setting We'll add payment by typing in the code, and save it.
The payment application is now active in the project. Edit the views.py file of the orders application, and make sure to include these imports. Replace these lines of the order_create view with these selected lines. After successfully creating an order, we set the order ID in the current session using the order_id session key.
Then, we redirect the user to the payment:process URL, which we're going to create next. Edit the views.py file of the payment application, and add these code to it. In the payment_process view, we generate a custom PayPal's Buy Now button to pay an order. First, we get the current order from the order_id session key, which was set before by the order_create view. We get the order object for the given ID and build a new PayPalPaymentsForm, including these highlighted fields.
Business, the PayPal merchant account to process the payment. We use the email account defined in the PAYPAL_RECEIVER_EMAIL setting here. Amount, the amount to charge the customer. Item_name, the name of the item being sold. We use the order ID since the order may contain multiple products. Invoice, the invoice ID. This ID has to be unique for each payment. We use the order ID. Currency_code, the currency for this payment. We set this to USD for U.S. Dollar.
Use the same currency as the one set in your PayPal account. Notify_url, the URL PayPal will send IPN requests to. We use the paypal-ipn URL provided by django-paypal. The view associated to this URL handles payment notifications and stores them in the database. Return_url, the URL to redirect the user after the payment is successful. We use the URL payment:done, which we have to create next. Cancel_return, the URL to redirect the user if their payment is canceled or there was some other issue.
We use the URL payment:canceled, which we have to create next. The PayPalPaymentsForm will be rendered as a standard form with hidden fields, and the user will only see the Buy Now button. When users click the button, the form will be submitted to PayPal via POST. Let's create simple views for PayPal to redirect users when the payment has been successful or when it has been canceled for some reason. Now add these code to the same views.py file. We use the csrf_exempt decorator to avoid Django expecting a CSRF token since PayPal can redirect the user to any of these views via POST.
Create a new file inside the payment application directory, and name it urls.py. Add these code to it. These are the URLs for the payment workflow. We've included some URL patterns. Process, the view that generates the PayPal form for the Buy Now button. Done, for PayPal to redirect the user when the payment is successful. Canceled, for PayPal to redirect the user when the payment is canceled. Edit the main urls.py file of the myshop project, and include the URL patterns for the payment application.
Remember to place it before the shops.url pattern to avoid wrong pattern matching. Create the file structure inside the payment application directory like this. Go to the templates folder and open the payment folder, and name it as process.html and add these code to it.
This is the template that renders PayPalPaymentsForm and displays the Buy Now button. Now, create a new file and name it as done.html inside the payment directory. Add these code to it. This is the template for the page that the user is redirected to after a successful payment. Let's create a last file inside the payment directory and name it as canceled.html.
Now add these code to it. This is the template for the page that the user is redirected to when there is some issue processing the payment or if the user canceled the payment. Let's try the complete payment process. Open PayPal Sandbox. Open this link in your browser, and log in with your PayPal business account by giving username and password. Click the Dashboard menu item, and on the left-hand menu click the Accounts option under Sandbox. You should see the list of your Sandbox test accounts.
Initially, you'll see a business and a personal test account automatically created by PayPal. You can create a new Sandbox test account with the Create Account button. Click the personal account in the list to expand it, and then click the Profile link. You'll see information about the test account, including email and profile information. In the Funding tab, you'll see the bank account, credit card data, and PayPal credit balance. The test accounts can be used to do payments in your website using the Sandbox environment. Navigate to the Profile tab, and click the Change password link.
Create a custom password for this test account and save it. Now you'll get a password updated successfully message on your screen. Now, open the shell and start the development server with the command python manage.py runserver. Open local host in your browser, add some products to the shopping cart, and fill in the checkout form by giving your details.
When you click the Place order button, the order will be persisted to the database, and the order ID will be saved in the current session. And you'll be redirected to the payment process page. This page retrieves the order from the session and renders the PayPal form displaying a Buy Now button. You can take a look at the HTML source code to see the generated form fields. Click the Buy Now button. You'll be redirected to PayPal, and you should see the page which is shown here. Enter the buyer test account email and password, and click the Log In button.
You'll be directed to this page. Now click the Pay Now button. Finally, you'll see a confirmation page that includes your transaction ID. The page should look like this. Click the Return to email@example.com button. You'll be redirected to the URL you've specified in the return_url field of PayPalPaymentsForm. This is the URL for the payments done view. The page will look like this. The payment has been successful. However, PayPal is unable to send a payment status notification to our application since we're running our project on our local host with the IP 127.0.0.1 that not publicly accessible.
We're going to learn how to make our site accessible from the Internet and be able to receive IPN notifications. Awesome, we've successfully integrated a payment gateway. In the next video, we'll learn about payment notification.
Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Creating an online shop project
- Registering customer orders
- Managing payments and orders
- Integrating a payment gateway
- Extending the online shop
- Creating a coupon system
- Translating Python code