Billing/Payment Extension

Does anyone have recommendations of a good Yii extension(s) for

performing payment transactions (using services like PayPal, Stripe, etc)?

I’m using Yii 2.0.7

I have library recommendation: http://omnipay.thephpleague.com/

Hi everyone,

I’d like to share how to integrate paypal with yii2 and what I just spent 5 hours figuring out.

First I use the most updated script provided at. Paypal changers every now and then, so if this is 1 year out maybe there’s a better version.


https://github.com/paypal/ipn-code-samples/tree/master/php

Instead of messing around with composer or git grab, I prefer to just copy and paste the class file

"PaypalIPN.php"

Next you need to change this into a component for Yii2 by doing namespace and uses.

Add the below





namespace frontend\components;


use yii\base\Exception;

use yii\base\Component;

use Yii;


class PaypalIPN extends Component 




Now you can start using the class. But next you have to check that your TLS is version 1.2. This is some sort of security encryption that paypal just upgraded to.

Open your SSH and enter.


# openssl version -a

You want to make sure your are running openssl version 1.01 or higher. Then it will able to use TLS. If you are not google ‘update openssl’.

Getting the certificate

The IPN uses Curl which need to have a Certificate. How it works I don’t know. Some private public key thing. Anyway, you’re going to have to point to the location of the cert.

So look for this line:




if ($this->use_local_certs) {



And make changers to the line just below. There is a cacert.pem file in the repository. Just copy and paste it into a folder. I’ve put mine in the web folder ( is that safe?) Then using getAlias Yii2 I’ve now cofigured it to point to it.





 curl_setopt($ch, CURLOPT_CAINFO,Yii::getAlias('@frontend/web/certs/cacert.pem'));




To test this you are going to need to use a paypal IPN simulator that comes with a developer account.

Create The Controller

Just create a controller Function like this





public function actionPaypal_ipn() {


 $ipn = new PaypalIPN();

		

		// Use the sandbox endpoint during testing.

		$ipn->useSandbox();

		

		$verified = $ipn->verifyIPN();

		if ($verified) {

			$msg = print_r($_POST,true);

			mail('myemail@paperlesscloud.wesvault.com','paypal test',$msg);		

                        // Do code needed like update DB

		}

		else {

			mail('myemail@paperlesscloud.wesvault.com','paypal failed','problem');	

			

		}

}






Use the simulator to Post variables to your script.

  1. Your script will directly echo back the variables and include some confirmation variables. (Handshake)

  2. It’ll then bounce back the keyword ‘verified’.

I hope this helps everyone!