This repository has been archived by the owner on May 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bundle based on OmnipayStripe and JMSPaymentCoreBundle
- Loading branch information
0 parents
commit b4c8a4a
Showing
10 changed files
with
460 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Ruudk\Payment\StripeBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
|
||
class Configuration implements ConfigurationInterface | ||
{ | ||
/** | ||
* @return TreeBuilder | ||
*/ | ||
public function getConfigTreeBuilder() | ||
{ | ||
$treeBuilder = new TreeBuilder(); | ||
$rootNode = $treeBuilder->root('ruudk_payment_stripe'); | ||
|
||
$methods = array('checkout'); | ||
|
||
$rootNode | ||
->children() | ||
->scalarNode('api_key') | ||
->isRequired() | ||
->cannotBeEmpty() | ||
->end() | ||
->booleanNode('logger') | ||
->defaultTrue() | ||
->end() | ||
->end() | ||
|
||
->fixXmlConfig('method') | ||
->children() | ||
->arrayNode('methods') | ||
->defaultValue($methods) | ||
->prototype('scalar') | ||
->validate() | ||
->ifNotInArray($methods) | ||
->thenInvalid('%s is not a valid method.') | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
; | ||
|
||
return $treeBuilder; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Ruudk\Payment\StripeBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Loader; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
|
||
class RuudkPaymentStripeExtension extends Extension | ||
{ | ||
/** | ||
* @param array $configs | ||
* @param ContainerBuilder $container | ||
*/ | ||
public function load(array $configs, ContainerBuilder $container) | ||
{ | ||
$configuration = new Configuration(); | ||
$config = $this->processConfiguration($configuration, $configs); | ||
|
||
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); | ||
$loader->load('services.xml'); | ||
|
||
$container->setParameter('ruudk_payment_stripe.api_key', $config['api_key']); | ||
|
||
foreach($config['methods'] AS $method) { | ||
$this->addFormType($container, $method); | ||
} | ||
|
||
/** | ||
* When logging is disabled, remove logger and setLogger calls | ||
*/ | ||
if(false === $config['logger']) { | ||
$container->getDefinition('ruudk_payment_stripe.controller.notification')->removeMethodCall('setLogger'); | ||
$container->getDefinition('ruudk_payment_stripe.plugin.credit_card')->removeMethodCall('setLogger'); | ||
$container->removeDefinition('monolog.logger.ruudk_payment_stripe'); | ||
} | ||
} | ||
|
||
protected function addFormType(ContainerBuilder $container, $method) | ||
{ | ||
$stripeMethod = 'stripe_' . $method; | ||
|
||
$definition = new Definition(); | ||
if($container->hasParameter(sprintf('ruudk_payment_stripe.form.%s_type.class', $method))) { | ||
$definition->setClass(sprintf('%%ruudk_payment_stripe.form.%s_type.class%%', $method)); | ||
} else { | ||
$definition->setClass('%ruudk_payment_stripe.form.stripe_type.class%'); | ||
} | ||
$definition->addArgument($stripeMethod); | ||
|
||
$definition->addTag('payment.method_form_type'); | ||
$definition->addTag('form.type', array( | ||
'alias' => $stripeMethod | ||
)); | ||
|
||
$container->setDefinition( | ||
sprintf('ruudk_payment_stripe.form.%s_type', $method), | ||
$definition | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Ruudk\Payment\StripeBundle\Form; | ||
|
||
use Symfony\Component\Form\FormBuilderInterface; | ||
|
||
class CheckoutType extends StripeType | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
$builder->add('token', 'hidden', array( | ||
'required' => false | ||
)); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Ruudk\Payment\StripeBundle\Form; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
|
||
class StripeType extends AbstractType | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $name; | ||
|
||
/** | ||
* @param string $name | ||
*/ | ||
public function __construct($name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014 Ruud Kamphuis | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
|
||
namespace Ruudk\Payment\StripeBundle\Plugin; | ||
|
||
use JMS\Payment\CoreBundle\Model\FinancialTransactionInterface; | ||
use JMS\Payment\CoreBundle\Plugin\AbstractPlugin; | ||
use JMS\Payment\CoreBundle\Plugin\Exception\FinancialException; | ||
use JMS\Payment\CoreBundle\Plugin\PluginInterface; | ||
use Omnipay\Stripe\Gateway; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class CheckoutPlugin extends AbstractPlugin | ||
{ | ||
/** | ||
* @var \Omnipay\Stripe\Gateway | ||
*/ | ||
protected $gateway; | ||
|
||
/** | ||
* @var \Psr\Log\LoggerInterface | ||
*/ | ||
protected $logger; | ||
|
||
public function __construct(Gateway $gateway) | ||
{ | ||
$this->gateway = $gateway; | ||
} | ||
|
||
/** | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function setLogger(LoggerInterface $logger = null) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
|
||
public function processes($name) | ||
{ | ||
return $name == 'stripe_checkout'; | ||
} | ||
|
||
public function approveAndDeposit(FinancialTransactionInterface $transaction, $retry) | ||
{ | ||
$parameters = $this->getPurchaseParameters($transaction); | ||
|
||
$response = $this->gateway->purchase($parameters)->send(); | ||
|
||
if($this->logger) { | ||
$this->logger->info(json_encode($response->getRequest()->getData())); | ||
$this->logger->info(json_encode($response->getData())); | ||
} | ||
|
||
if($response->isSuccessful()) { | ||
$transaction->setReferenceNumber($response->getTransactionReference()); | ||
|
||
$data = $response->getData(); | ||
|
||
$transaction->setProcessedAmount($data['amount'] / 100); | ||
$transaction->setResponseCode(PluginInterface::RESPONSE_CODE_SUCCESS); | ||
$transaction->setReasonCode(PluginInterface::REASON_CODE_SUCCESS); | ||
|
||
if($this->logger) { | ||
$this->logger->info(sprintf( | ||
'Payment is successful for transaction "%s".', | ||
$response->getTransactionReference() | ||
)); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if($this->logger) { | ||
$this->logger->info(sprintf( | ||
'Payment failed for transaction "%s" with message: %s.', | ||
$response->getTransactionReference(), | ||
$response->getMessage() | ||
)); | ||
} | ||
|
||
$data = $response->getData(); | ||
switch($data['error']['type']) { | ||
case "api_error": | ||
$ex = new FinancialException($response->getMessage()); | ||
$ex->addProperty('error', $data['error']); | ||
$ex->setFinancialTransaction($transaction); | ||
|
||
$transaction->setResponseCode('FAILED'); | ||
$transaction->setReasonCode($response->getMessage()); | ||
$transaction->setState(FinancialTransactionInterface::STATE_FAILED); | ||
|
||
break; | ||
|
||
case "card_error": | ||
$ex = new FinancialException($response->getMessage()); | ||
$ex->addProperty('error', $data['error']); | ||
$ex->setFinancialTransaction($transaction); | ||
|
||
$transaction->setResponseCode('FAILED'); | ||
$transaction->setReasonCode($response->getMessage()); | ||
$transaction->setState(FinancialTransactionInterface::STATE_FAILED); | ||
|
||
break; | ||
|
||
default: | ||
$ex = new FinancialException($response->getMessage()); | ||
$ex->addProperty('error', $data['error']); | ||
$ex->setFinancialTransaction($transaction); | ||
|
||
$transaction->setResponseCode('FAILED'); | ||
$transaction->setReasonCode($response->getMessage()); | ||
$transaction->setState(FinancialTransactionInterface::STATE_FAILED); | ||
|
||
break; | ||
} | ||
|
||
throw $ex; | ||
} | ||
|
||
/** | ||
* @param FinancialTransactionInterface $transaction | ||
* @return array | ||
*/ | ||
protected function getPurchaseParameters(FinancialTransactionInterface $transaction) | ||
{ | ||
/** | ||
* @var \JMS\Payment\CoreBundle\Model\PaymentInterface $payment | ||
*/ | ||
$payment = $transaction->getPayment(); | ||
|
||
/** | ||
* @var \JMS\Payment\CoreBundle\Model\PaymentInstructionInterface $paymentInstruction | ||
*/ | ||
$paymentInstruction = $payment->getPaymentInstruction(); | ||
|
||
/** | ||
* @var \JMS\Payment\CoreBundle\Model\ExtendedDataInterface $data | ||
*/ | ||
$data = $transaction->getExtendedData(); | ||
|
||
$transaction->setTrackingId($payment->getId()); | ||
|
||
$parameters = array( | ||
'amount' => $payment->getTargetAmount(), | ||
'currency' => $paymentInstruction->getCurrency(), | ||
'description' => $data->get('description'), | ||
'token' => $data->get('token'), | ||
); | ||
|
||
return $parameters; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
RuudkPaymentStripeBundle | ||
======================== | ||
|
||
A Symfony2 Bundle that provides access to the Stripe API. Based on JMSPaymentCoreBundle. | ||
|
||
## Installation | ||
|
||
### Step1: Require the package with Composer | ||
|
||
```` | ||
php composer.phar require ruudk/payment-stripe-bundle | ||
```` | ||
|
||
### Step2: Enable the bundle | ||
|
||
Enable the bundle in the kernel: | ||
|
||
``` php | ||
<?php | ||
// app/AppKernel.php | ||
|
||
public function registerBundles() | ||
{ | ||
$bundles = array( | ||
// ... | ||
|
||
new Ruudk\Payment\StripeBundle\RuudkPaymentStripeBundle(), | ||
); | ||
} | ||
``` | ||
|
||
### Step3: Configure | ||
|
||
Add the following to your config.yml: | ||
```yaml | ||
ruudk_payment_stripe: | ||
api_key: Your API key | ||
logger: true/false # Default true | ||
methods: | ||
- checkout | ||
``` | ||
Make sure you set the `description` in the `predefined_data` for every payment method you enable: | ||
````php | ||
$form = $this->getFormFactory()->create('jms_choose_payment_method', null, array( | ||
'amount' => $order->getAmount(), | ||
'currency' => 'EUR', | ||
'predefined_data' => array( | ||
'stripe_checkout' => array( | ||
'description' => 'My product', | ||
), | ||
), | ||
)); | ||
```` | ||
|
||
See [JMSPaymentCoreBundle documentation](http://jmsyst.com/bundles/JMSPaymentCoreBundle/master/usage) for more info. |
Oops, something went wrong.