Last active
August 29, 2015 14:10
-
-
Save shinesoftware/b2c685fef06537de4801 to your computer and use it in GitHub Desktop.
Paypal Adaptive Payment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Zend Framework (http://framework.zend.com/) | |
* | |
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository | |
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) | |
* @license http://framework.zend.com/license/new-bsd New BSD License | |
*/ | |
namespace Paypal\Controller; | |
use Base\Service\SettingsService; | |
use Zend\Mvc\Controller\AbstractActionController; | |
use Zend\View\Model\ViewModel; | |
use Base\Service\SettingsServiceInterface; | |
class IndexController extends AbstractActionController | |
{ | |
protected $settings; | |
protected $translator; | |
/** | |
* preDispatch event of the page | |
* | |
* (non-PHPdoc) | |
* @see Zend\Mvc\Controller.AbstractActionController::onDispatch() | |
*/ | |
public function onDispatch(\Zend\Mvc\MvcEvent $e){ | |
$this->translator = $e->getApplication()->getServiceManager()->get('translator'); | |
return parent::onDispatch( $e ); | |
} | |
public function __construct(SettingsServiceInterface $settings) | |
{ | |
$this->settings = $settings; | |
} | |
public function indexAction () | |
{ | |
$link = null; | |
$username = $this->settings->getValueByParameter('paypal', 'username'); | |
$password = $this->settings->getValueByParameter('paypal', 'password'); | |
$signature = $this->settings->getValueByParameter('paypal', 'signature'); | |
$appid = $this->settings->getValueByParameter('paypal', 'appid'); | |
$mode = $this->settings->getValueByParameter('paypal', 'mode'); | |
$log = $this->settings->getValueByParameter('paypal', 'log'); | |
$currency = $this->settings->getValueByParameter('paypal', 'currency'); | |
$baseUrl = $this->settings->getValueByParameter('paypal', 'baseurl'); | |
$success = "$baseUrl/paypal/success"; | |
$cancel = "$baseUrl/paypal/failure"; | |
$ipn = "$baseUrl/response/ipn"; | |
if(empty($username) || empty($password) || empty($signature) || empty($appid) || empty($currency) || empty($baseUrl)) | |
throw new \Exception('Paypal is not set yet! Go to the administration page and then Settings > Paypal'); | |
$config = array('acct1.UserName' => $username, | |
'acct1.Password' => $password, | |
'acct1.Signature' => $signature, | |
'acct1.AppId' => $appid, | |
'mode' => !empty($mode) ? $mode : "sandbox", | |
'http.ConnectionTimeOut' => 30, | |
'log.LogEnabled' => (bool)$log, | |
'log.FileName' => LOG_PATH . 'PayPal.log', | |
'log.LogLevel' => 'FINE', | |
'validation.level' => 'log'); | |
$service = new \PayPal\Service\AdaptivePaymentsService($config); | |
$payer = new \PayPal\Api\Payer(); | |
$payer->setPaymentMethod("paypal"); | |
$requestEnvelope = new \PayPal\Types\Common\RequestEnvelope("en_US"); | |
$receivers = array(); | |
$receivers[0] = new \PayPal\Types\AP\Receiver(); | |
$receivers[0]->email = "user1@mydomain.com"; // A receiver's email address | |
$receivers[0]->amount = "1.00"; // Amount to be credited to the receiver's account | |
$receivers[0]->primary = true; | |
$receivers[1] = new \PayPal\Types\AP\Receiver(); | |
$receivers[1]->email = "user2@mydomain.com"; // A receiver's email address | |
$receivers[1]->amount = "1.00"; // Amount to be credited to the receiver's account | |
$receiverList = new \PayPal\Types\AP\ReceiverList($receivers); | |
$payRequest = new \PayPal\Types\AP\PayRequest($requestEnvelope, "PAY", $cancel, $currency, $receiverList, $success); | |
$payRequest->ipnNotificationUrl = $ipn; | |
try { | |
$response = $service->Pay($payRequest); | |
} catch(\Exception $ex) { | |
var_dump($ex->getMessage()); | |
die; | |
} | |
if ($response->responseEnvelope->ack == "Success"){ | |
$link = "<a href='https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_ap-payment&paykey=" . $response->payKey . "'>Pay</a>"; | |
}else{ | |
foreach ($response->error as $error){ | |
var_dump($error->message); | |
} | |
} | |
die($link); | |
} | |
public function successAction(){ | |
$request = $this->getRequest(); | |
} | |
public function failuresAction(){ | |
$request = $this->getRequest(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment