Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Integrate paypal

  • Loading branch information...
commit 4bd38db5baaac778d9aa288baceca16a06178a86 1 parent 04ed4d9
ouardisoft authored
View
2  OSPaymentBundle.php
@@ -6,4 +6,4 @@
class OSPaymentBundle extends Bundle
{
-}
+}
View
10 PaymentFactory.php
@@ -11,6 +11,12 @@ class PaymentFactory
{
private $plugin;
+ private $container;
+
+ function __construct($container)
+ {
+ $this->container = $container;
+ }
public function __call($name, $arguments)
{
@@ -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;
}
View
253 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;
+ }
+
+}
View
2  Resources/config/services.yml
@@ -4,4 +4,4 @@ parameters:
services:
payment.factory:
class: %payment.factory.class%
- arguments: []
+ arguments: ['@service_container']
View
43 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;
Please sign in to comment.
Something went wrong with that request. Please try again.