Skip to content

Commit

Permalink
Integrate paypal
Browse files Browse the repository at this point in the history
  • Loading branch information
ouardisoft committed Mar 14, 2012
1 parent 04ed4d9 commit 4bd38db
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 2 deletions.
2 changes: 1 addition & 1 deletion OSPaymentBundle.php
Expand Up @@ -6,4 +6,4 @@

class OSPaymentBundle extends Bundle
{
}
}
10 changes: 10 additions & 0 deletions PaymentFactory.php
Expand Up @@ -11,6 +11,12 @@ class PaymentFactory
{

private $plugin;
private $container;

function __construct($container)
{
$this->container = $container;
}

public function __call($name, $arguments)
{
Expand All @@ -23,6 +29,10 @@ public function getPlugin($plugin = null)
$this->setPlugin($plugin);
}

if (method_exists($this->plugin, 'setContainer')) {
$this->plugin->setContainer($this->container);
}

return $this->plugin;
}

Expand Down
253 changes: 253 additions & 0 deletions Plugins/Paypal.php
@@ -0,0 +1,253 @@
<?php

namespace OS\PaymentBundle\Plugins;

use Symfony\Component\HttpFoundation\RedirectResponse,
Symfony\Component\DependencyInjection\Container;

/**
* @author ouardisoft
*/
class Paypal
{

CONST URI_PAYPAL_SANDBOX = 'https://www.sandbox.paypal.com/fr/cgi-bin/webscr';
CONST URI_PAYPAL_DEFAULT = 'https://www.paypal.com/fr/cgi-bin/webscr';

private $uri;
private $url;
private $options;
private $redirect;
private $env = 'default';
private $results;

/**
*
* @var Container
*/
private $container;
private $resultCURL;

/**
*
* @return type
*/
public function getUri()
{
return $this->uri;
}

/**
*
* @param type $uri
*/
public function setUri($uri)
{
$this->uri = $uri;
}

/**
*
* @return type
*/
public function getUrl()
{
return $this->url;
}

/**
*
* @param type $url
*/
public function setUrl($url)
{
$this->url = $url;
}

/**
*
* @return type
*/
public function getEnv()
{
return $this->env;
}

/**
*
* @param type $env
*/
public function setEnv($env)
{
$this->env = $env;
}

/**
*
* @return type
*/
public function getOptions()
{
return $this->options;
}

/**
*
* @param type $options
*/
public function setOptions($options)
{
if (array_key_exists('env', $options)) {
$this->setEnv($options['env']);

unset($options['env']);
}

switch ($this->getEnv()) {
case 'default': $this->setUri(static::URI_PAYPAL_DEFAULT);break;
case 'sandbox': $this->setUri(static::URI_PAYPAL_SANDBOX);break;
}

$this->options = $options;

return $this;
}

/**
*
* @return type
*/
public function getRedirect()
{
return $this->redirect;
}

/**
*
* @param type $redirect
*/
public function setRedirect($redirect)
{
$this->redirect = $redirect;
}

/**
*
* @return type
*/
public function getResults()
{
return $this->results;
}

/**
*
* @param type $results
*/
public function setResults($results)
{
$this->results = $results;
}

/**
*
*/
public function execute($options)
{
$this->setOptions($options);

$this->call();

return $this;
}
/**
*
*/
public function getQueryStringFromOptions()
{
return http_build_query($this->getOptions());
}

/**
*
*/
public function call()
{
$url = sprintf('%s?%s', $this->getUri(), $this->getQueryStringFromOptions());

$response = new RedirectResponse($url);

$this->setRedirect($response);
}

/**
*
*/
public function getTestOptions()
{
return array();
}

public function validate()
{
$request = $this->getContainer()->get('request')->request;

$request->set('cmd', '_notify-validate');
$parameters = http_build_query($request->all());

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $this->getUrl());
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$this->resultCURL = curl_exec($ch);

curl_close($ch);

return $this;
}

/**
*
* @return Container
*/
public function getContainer()
{
return $this->container;
}

/**
*
* @param Container $container
*/
public function setContainer($container)
{
$this->container = $container;

return $this;
}

/**
*
* @return type
*/
public function getResultCURL()
{
return $this->resultCURL;
}

/**
*
* @param type $resultCURL
*/
public function setResultCURL($resultCURL)
{
$this->resultCURL = $resultCURL;
}

}
2 changes: 1 addition & 1 deletion Resources/config/services.yml
Expand Up @@ -4,4 +4,4 @@ parameters:
services:
payment.factory:
class: %payment.factory.class%
arguments: []
arguments: ['@service_container']
43 changes: 43 additions & 0 deletions Resources/doc/paypal.md
@@ -0,0 +1,43 @@
How to use:

Check result

// check request
$payment = $this->get('payment.factory');
$r = $payment->setPlugin('Paypal')
->setOptions(array('env' => 'sandbox'))
->validate()
->getResultCURL();

var_dump($r);


Redirect to paypal

/**
* Send request
*/
$payment = $this->get('payment.factory');
$payment->execute(array(
'plugin' => 'Paypal',
'options' => array(
'env' => 'sandbox',
'cmd' => '_xclick',
'business' => YOUR_BUSINESS,
'currency_code' => 'EUR',
'item_name' => 'Chemise',
'amount' => 1000,
'shipping' => 600,
'tax' => 200,
'invoice' => 40009,
'custom' => 7856,
'no_note' => '1',
'notify_url' => '',
'return' => '',
'cancel_return' => '',
'bn' => ''
)));

$r = $payment->redirect();

return $r;

0 comments on commit 4bd38db

Please sign in to comment.