This bundle allows to implement a Payment Solution working with E-transactions for your symfony project. E-transactions is a payment proposed by the following bank "Crédit Agricole". Don't hesitate to contact me to improve it ;)
Using composer :
{
"require": {
"snowbaha/etransactions-bundle": "~1.0"
}
}
Enable the bundle in the kernel:
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Snowbaha\EtransactionsBundle\SnowbahaEtransactionsBundle(),
);
}
Mandatory fields PARAMETER:
# E-Transactions (don't forget the validation of the HMAC key 'certif_test/prod' by email)
etransactions_id: ~
etransactions_site: ~
etransactions_certif_test: ~
etransactions_certif_prod: ~
Mandatory fields CONFIG :
snowbaha_etransactions:
# Credentials
identifiant: "%etransactions_id%"
site: "%etransactions_site%"
rang: "01" #change it with your RANK
# Keys
key_dev: "%etransactions_certif_test%"
key_prod: "%etransactions_certif_prod%"
# SETTING OPTIONAL
env_mode: TEST # TEST by default - Possible values : TEST / PRODUCTION
# check_signature: false # /!\ KEEP IT to false because it doesn't work and i DONT KNOW WHY (let me know if you have a solution) false by Default (if you want to check the signature of the IPN Bank
To initiate a new Transaction, you need to create an action in one of your controller and call the snowbaha.etransactions
service.
All mandatory fields are used with their default value. You can configure all the common fields of your transactions in the app/config/config.yml
file.
To see what fields are available see the PDF official doc.
init($order_id, $amount, $email_buyer, $currency = 978)
allows you to specify the id, amount, email of buyer and the currency of the transaction.setOptionnalFields(array)
allows you to specify any field.
/**
* @Route("/initiate-payment/id-{id}", name="pay_online")
* @Template()
*/
public function payOnlineAction($id)
{
// ...
$etransactions = $this->get('snowbaha.etransactions')
->init(99, 100, '[email protected]')
->setOptionnalFields(array(
'PBX_ERREUR' => 'http://www.example.com/error'
))
;
return $this->render('YOURBUNDLEBundle:Etransactions:pay_online.html.twig', array(
'paymentUrl' => $etransactions->getPaymentUrl(),
'fields' => $etransactions->getFields(),
));
}
This route will be called by the E-Transactions service to update you about the payment status. This is the only way to correctly handle payment verfication.
Service Method:
responseBankServer(Request)
is used to update the transaction status (in database)- You will get a array with :
- sucessPayment: (should be true to validate the payment)
- amount: (your pbx_total var)
- ref: (your pbx_cmb var)
- error:E; (error with
00000
if nothing) - auto:A; (autorisation)
- sign:K (signature to check)
// YOUR CONTROLLER
/**
* THIS ROUTE have to be public to allow the bank access, it is the URL you will provide to your account
* @Route("/payment/verification")
* @param Request $request
*/
public function paymentVerificationAction(Request $request)
{
// ...
$responseBank = $this->get('snowbaha.etransactions')->responseBankServer($request);
$id_order = (int)$responseBank['ref'];
$Order = $this->get('app.provider.order')->getOneByID( $id_order );
// Success
if($responseBank['sucessPayment'] === true && !is_null($Order)) :
$Order->setState("order.state.success_payment");
$Order->setPaymentError(0);
// Email notification
$this->get('app.mailing.order')->sendNewOrderNotif($Order);
elseif( !is_null($Order) ) :
// Error
$Order->setState('order.state.success_payment_error');
$Order->setPaymentError($responseBank['error']);
endif;
//...
}
This is how the template for the payOnlineAction()
may look like. You can use the paiementForm
twig function to automatically generate the form based on the fields created in the service and returned by the getFields()
function.
<html>
<i class="fa fa-refresh fa-spin margin-top margin-bottom" style="font-size: 50px"></i> {# With http://fontawesome.io/icons/ #}
<h3>Redirect to the paiement page...</h3>
<form action="{{ paymentUrl }}" method="POST" id="etransactions-form">
{{ paiementForm(fields) }}
{# If no JS, show the button to submit#}
<noscript>
<input type="submit" value="Pay">
</noscript>
</form>
</html>
<script type="text/javascript">
document.getElementById('etransactions-form').submit();
</script>
When you will get an error with the payment, you can have more information with the log : ENV.etransaction.log