How to validate payment through PayPal using Magento REST API ?

validate paypal payment

In this digital age to maintain your business growth, Mobile application for your eCommerce becomes the main key point in short mCommerce. Data researched by Coupofy suggests that between 2013 and 2016 mobile shopping will grow 42% compared to regular eCommerce’s 13%, and mobile users will spend a total of $600 billion in 2018, a 300% growth from 2014’s $200 billion. Business owners are focusing on launching their mobile app to make their exists in this digital age. So the development of mCommerce is in the pick point now. As the mCommerce Mobile application development growing rapidly there are a lot of issues are in the queue related to design, performance, UI, payment gateway, validate PayPal payment etc. In this article, we are going to discuss how to validate PayPal payment through Magento REST API interface?

So what is the actual problem?

We are developing an android app for Magento base eCommerce site. We are using REST API. Payment and order related things integration is challenging for us, since we need more secure and careful module development. We are doing an android app so there is a situation where we have complete the payment process using PayPal express mobile SDK. In return, we get transaction id, according to my understanding we have to save with the created order. In the site, we configured PayPal successfully. So the transaction id passed to Magento, where we need to validate PayPal payment. Now at the end, we want an API from Magento side to save the transaction and complete the checkout process.

In fact lot of people have the same problem:

Solution

We have gone through the answer posted in the various community like StackExchange, Magento forum etc. After a little bit research, we have to use the following approach and quite suited well to our requirement.
1. Created a custom payment Method: In the box, Magento rest api does not support place order without the payment method. We have created a custom payment module with a custom method. For More detail how to create a payment module check here. Here is the code sample for our method:

/**
 * Copyright © 2016 iPragmatech. All rights reserved.
 */
namespace Ipragmatech\Paymentapi\Model;

/**
 * Pay In Store payment method model
 */
class Ippaypal extends \Magento\Payment\Model\Method\AbstractMethod
{

    /**
     * Payment code
     *
     * @var string
     */
    protected $_code = 'ippaypal';

    /**
     * Availability option
     *
     * @var bool
     */
    protected $_isOffline = true;
}

2. Enable only for the rest API: The above method enables only for the rest api and not supported on the web app for the payment. Make a new rest api endpoint to receive the transaction id and validate it from PayPal. After successful validation move to the next step.Check out the documentation to validate PayPal payment.
3. Saved transaction id with order: Once all validation are passed save the transaction id return by the PayPal with the order. A transaction is a proof that payment has been done. In Magento transactions are created when the invoice is created, although Magento creates the transaction by itself you don’t need to worry about that but in case you will have to create the transaction manually, here is the code snippet that will create a transaction:

public function createTransaction($order = null, $paymentData = array())
    {
        try {
            //get payment object from order object
            $payment = $order->getPayment();
            $payment->setLastTransId($paymentData['id']);
            $payment->setTransactionId($paymentData['id']);
            $payment->setAdditionalInformation(
                [\Magento\Sales\Model\Order\Payment\Transaction::RAW_DETAILS => (array) $paymentData]
            );
            $formatedPrice = $order->getBaseCurrency()->formatTxt(
                $order->getGrandTotal()
            );
 
            $message = __('The authorized amount is %1.', $formatedPrice);
            //get the object of builder class
            $trans = $this->_transactionBuilder;
            $transaction = $trans->setPayment($payment)
            ->setOrder($order)
            ->setTransactionId($paymentData['id'])
            ->setAdditionalInformation(
                [\Magento\Sales\Model\Order\Payment\Transaction::RAW_DETAILS => (array) $paymentData]
            )
            ->setFailSafe(true)
            //build method creates the transaction and returns the object
            ->build(\Magento\Sales\Model\Order\Payment\Transaction::TYPE_CAPTURE);
 
            $payment->addTransactionCommentsToOrder(
                $transaction,
                $message
            );
            $payment->setParentTransactionId(null);
            $payment->save();
            $order->save();
 
            return  $transaction->save()->getTransactionId();
        } catch (Exception $e) {
            //log errors here
        }
    }

Click here more detail regarding the transaction.

Conclusion

After all, we have used this approach to validate payment because this is well suited to our requirement. If you have a better idea please fell free to share in the comment. This might help a lot to us as well others.

Reference

How to validate payment through PayPal using Magento REST API ? was last modified: December 29th, 2016 by Manish Kumar