48. In payment links

We will now configure the payment links used in the Mercado Pago API.

To track when a payment is successfully made, we will use webhooks. Webhooks capture information about a specific event and store it in a link, from which we can extract the data.

Visit the site below:

Now, copy this link:

Paste it into the link variable within the finish_order function.

views.py\finish_order
    else :
            #? make payment
            items_ordered = OrderedItem.objects.filter(order=order)
            link = "https://webhook.site/4db7b2f6-e5ab-45e0-bb21-d8ca592c9fe5"
            create_payment(items_ordered, link)
            return redirect("store")
    else :
        return redirect("store")

Now, open the ecommerce project in an incognito tab and log in to the Mercado Pago page using the buyer account you created earlier.

In the ecommerce project, add items to your cart and complete the checkout. Click on the link printed in the terminal, and open it in the incognito tab where you are logged in with your buyer Mercado Pago account.

Note: You could test the payment by paying directly using the credit from your account. However, we also want to test unsuccessful cases.

Click on the 'Alter' button to change the payment method to credit card payment.

Now, in a regular tab (not incognito), log in with the account used to create the integration in Mercado Pago, and select the "Testing Cards" option.

We will test cases where the purchase was successful and failed using these testing cards. Here is the URL in case you are unable to find it:

Note: Being logged into your testing account is only necessary during the testing phase. In the production phase, you will implement your account data into the code, and you won’t need to remain logged in for the system to work.

Inside the Mercado Pago page for testing cards, this content will appear:

Mercado Pago allows us to determine the payment status of a credit card by using specific names: APRO (approval), OTHER (rejection), and CONT (pending).

First, paste the card number, security code, and expiration date of the Mastercard example (or any other of the three shown) into the forms and set the name to APRO to test a successful payment.

Important Note: During testing, use your own generated test card details. Using the exact ones shown in the image may lead to errors.

After clicking "Continue," paste the CPF number beside the APRO section on the Mercado Pago site and click "Submit."

Click on "Continue," set the payment method to "Cash" (payment in 1x), then click "Pay." You will be redirected to the following page:

Now, click on "Return to Site," and this page will appear:

When you click on the "Return to Site" button, you will be redirected to the webhook link we added earlier, which will capture the information of the purchase. Click on "View" in the webhook site, and it will redirect you to the corresponding page.

In the "Query Strings" section, you can view all the information the webhook obtained from the payment process. Note that in this case, it indicates that the payment was approved.

To test an unsuccessful payment, repeat the same procedures but use the credit card name OTHER instead of APRO and its respective CPF number. Ensure you perform this procedure in the same tab where you are logged in as your buyer testing account.

You will be redirected to the following page:

When you click on "Return to Site" and then enter the webhook site, the information about the rejected payment will also be displayed.

We will use two important parameters provided by the webhook: preference_id (the ID of the user's payment preference) and status (indicating whether the purchase was approved or rejected).

On our website, we will create a new URL view to display this information to the user (obtained from the webhook) instead of just showing a blank page.

To accomplish this, our view functions need to know the redirect link and the payment information. Therefore, we will modify the create_payment function inside api_mercadopago.py to return both the link and the preference_id.

api_mercadopago.py
# Create a preference
    preference_response = sdk.preference().create(preference_data) #? creating request with various informations about the payment
    payment_link = preference_response["response"]["init_point"]
    payment_id = preference_response["response"]["id"] #? obtaining the payment id to treat it
    return payment_link, payment_id

Inside our finish_order view function, we will receive the payment_link and preference_id variables and then redirect the user to the payment_link.

views.py\finish_order
            link = "https://webhook.site/4db7b2f6-e5ab-45e0-bb21-d8ca592c9fe5"
            payment_link, payment_id= create_payment(items_ordered, link)
            return redirect(payment_link)

Since we need to store payment information (which includes several attributes) and associate it with the order, we will create a new model/class for it inside the models.py file.

models.py
class Payment(models.Model) :
    payment_id = models.CharField(max_length=400)
    order = models.ForeignKey(Order, blank=True, null=True, on_delete=models.SET_NULL)
    aproved = models.BooleanField(default=False) 

Remember to register the new Payment class in the admin.py file so it can be managed from the admin page.

admin.py
# Register your models here.
admin.site.register([Categoric, Client, Type, Product, ItemStock, Order, OrderedItem, Adres, Banner, Color, Payment])

Lastly, since we added a new class to our database (modifying it directly), we need to migrate the changes.

Stop the website if it’s running and then execute the following commands in the terminal:

python manage.py makemigrations
python manage.py migrate

Last updated