Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/phcorp/kptivepaymentsipsbundle

forked from KptiveStudio/KptivePaymentSipsBundle since project was abandoned. Contributions welcomed!
https://github.com/phcorp/kptivepaymentsipsbundle

atos jmspaymentcorebundle payment sips symfony-bundle

Last synced: 26 days ago
JSON representation

forked from KptiveStudio/KptivePaymentSipsBundle since project was abandoned. Contributions welcomed!

Awesome Lists containing this project

README

        

KptivePaymentSipsBundle
=======================

[![Build Status](https://secure.travis-ci.org/phcorp/KptivePaymentSipsBundle.svg?branch=master)](http://travis-ci.org/phcorp/KptivePaymentSipsBundle)
[![SensioLabs Insight](https://img.shields.io/sensiolabs/i/88a429c7-37d7-4b1d-8280-8d1d0ee73e03.svg)](https://insight.sensiolabs.com/projects/88a429c7-37d7-4b1d-8280-8d1d0ee73e03)

The `KptivePaymentSipsBundle` provides access to the Atos Worldline SIPS payment solution through
the [JMSPaymentCoreBundle](https://github.com/schmittjoh/JMSPaymentCoreBundle).

The following payment services are powered by Atos SIPS:
- Merc@net (BNP Parisbas)
- Cyberplus (Banque Populaire)
- Elys Net (HSBC)
- Scellius (La Banque Postale)
- SogenActif (Société Générale)
- Webaffaires (Crédit du Nord)
- Sherlocks (LCL)
- Citelis (Crédit Mutuel)
- ...

This means that this bundle should work out of the box with any of them.

Installation
------------

### Step 1

Run:

``` bash
$ php composer.phar require kptive/payment-sips-bundle
```

Or add the following to your `composer.json` before updating your vendors:

``` js
{
"require": {
"kptive/payment-sips-bundle": "*@dev"
}
}
```

### Step 2

Register the bundle in your `AppKernel` class.
You will also have to register the `JMSPaymentCoreBundle` and
[configure it](http://jmsyst.com/bundles/JMSPaymentCoreBundle/master/configuration).

``` php
id;
}

public function getAmount()
{
return $this->amount;
}

public function getPaymentInstruction()
{
return $this->paymentInstruction;
}

public function setPaymentInstruction(PaymentInstruction $instruction)
{
$this->paymentInstruction = $instruction;

return $this;
}

public function getPayedAt()
{
return $this->payedAt;
}

public function setPayedAt($payedAt)
{
$this->payedAt = $payedAt;

return $this;
}

```

Create a controller with a `details` action.
This is where your customer can review their order and confirm it.

``` php
get('request');
$em = $this->get('doctrine')->getEntityManager();
$router = $this->get('router');
$ppc = $this->get('payment.plugin_controller');

$confirm = new \StdClass();

$form = $this->createFormBuilder($confirm)
->add('save', 'submit', array('label' => 'confirmer'))
->getForm();

if ('POST' === $request->getMethod()) {
$form->handleRequest($request);

if ($form->isValid()) {
$instruction = new PaymentInstruction($order->getAmount(), 978, 'sips');

$ppc->createPaymentInstruction($instruction);

$order->setPaymentInstruction($instruction);
$em->persist($order);
$em->flush($order);

return new RedirectResponse($router->generate('payment_gateway', array(
'id' => $order->getId(),
)));
}
}

return array(
'order' => $order,
'form' => $form->createView()
);
}
}
```

As you can see in the previous action, when the user confirms their order, we
create a new `PaymentInstruction` (see the
[JMSPaymentCoreBundle documentation](http://jmsyst.com/bundles/JMSPaymentCoreBundle/master/model)
for more information on how it works).
They are then redirected to the `payment_gateway` route.
This is where we'll make a call to the SIPS API so that we can display the SIPS
credit card choice form.

Let's implement the corresponding action:

``` php
/**
* @Route("/gateway/{id}", name="payment_gateway")
* @Template()
*/
public function sipsGatewayAction(Order $order)
{
$client = $this->get('kptive_payment_sips.client');

$config = array(
'amount' => $order->getAmount() * 100,
'order_id' => $order->getId(),
);

$sips = $client->request($config);

return array('sips' => $sips);
}
```

And in the corresponding view, display the form to the user:

``` jinja
{# src/Acme/PaymentBundle/Resources/views/Checkout/sipsGateway.html.twig #}

{{ sips|raw }}
```

When the user has completed the payment workflow on the SIPS platform, they will
be redirected to the `normal_return_url` you configured earlier in the bundle
config section.

Let's implement the action :

``` php
/**
* @Route("/complete", name="payment_complete")
* @Template()
*/
public function completeAction(Request $request)
{
$data = $request->request->get('DATA');
$em = $this->get('doctrine')->getEntityManager();
$client = $this->get('kptive_payment_sips.client');

$response = $client->handleResponseData($data);
$order = $em->getRepository('KsPaymentBundle:Order')->find($response['order_id']);
$instruction = $order->getPaymentInstruction();

$result = $this->get('kptive_payment_sips.return_handler')->handle($instruction, $response);

return array('order' => $order);
}
```

For now, we didn't do anything with the Order, we just handled the bank response
and marked the payment as valid.

The JMSPaymentCoreBundle will trigger a `payment.state_change` event.
So we will listen to this event and do everything useful we want in a `PaymentListener`:

``` php
entityManager = $entityManager;
}

public function onPaymentStateChange(PaymentStateChangeEvent $event)
{
if (PaymentInterface::STATE_DEPOSITED === $event->getNewState()) {
$order = $this
->entityManager
->getRepository('AcmePaymentBundle:Order')
->findOneBy(array('paymentInstruction' => $event->getPaymentInstruction()));

$order->setPayedAt(new \DateTime());

// Do various things with the Order here
// ...

$this->entityManager->persist($order);
$this->entityManager->flush();
}
}
}
```

Register it as a service:

``` xml




```

And voilà!

If your customer doesn't click on the "Back" button on the bank platform,
a request will be automatically issued to the configured `automatic_response_url`.

You can use the same URL as the `normal_return_url` or implement your own.

**Warning**: those examples don't take security into account. Don't forget to
check the ownership of the order!

Credits
-------

* Hubert Moutot

A great thank you to Johannes M Schmitt for his awesome JMSPayementCoreBundle.
Thanks to https://github.com/Kitano/KitanoPaymentSipsBundle for the inspiration.

License
-------

KptivePaymentSipsBundle is released under the MIT License.
See the bundled LICENSE file for details.