Skip to content
This repository was archived by the owner on Feb 10, 2024. It is now read-only.

Commit edf16d9

Browse files
authored
Merge pull request #1 from kiakahaDZ/main
Merge
2 parents f9e12bc + 7cdf9db commit edf16d9

5 files changed

Lines changed: 279 additions & 2 deletions

File tree

README.md

Lines changed: 146 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,146 @@
1-
# epay-gateway-symfony
2-
Chargily ePay Gateway (Symfony Package)
1+
# chargily-epay-symfony
2+
Symfony Plugin for Chargily ePay Gateway
3+
4+
![Chargily ePay Gateway](https://raw.githubusercontent.com/Chargily/epay-gateway-php/main/assets/banner-1544x500.png "Chargily ePay Gateway")
5+
6+
# Installation
7+
1. Via Composer (Recomended)
8+
```bash
9+
composer require chargily/epay-symfony
10+
```
11+
12+
2. Register the bundle, add this line at the end of the file config/bundles.php
13+
```bash
14+
\Chargily\SymfonyBundle\ChargilySymfonyBundle::class => ['all' => true],
15+
```
16+
17+
3. Import the services, Add the follow line in config/services.yml
18+
```bash
19+
imports:
20+
- { resource: "@ChargilySymfonyBundle/config/services.yaml" }
21+
```
22+
23+
4. Configure the api keys Add the follow line in config/services.yml
24+
```bash
25+
parameters:
26+
api_key: "API_KEY"
27+
secret_key: "SECRET_KEY"
28+
```
29+
30+
5. Process payment and redirection to payment page
31+
32+
```bash
33+
$payload = array(
34+
"client" => "test",
35+
'client_email' => "test@gmail.com",
36+
"invoice_number" => '123456789',
37+
"amount" => 110,
38+
'discount' => 0,
39+
'mode' => 'CIB',
40+
'back_url' => "https://test.com",
41+
'webhook_url' => "https://test.com" . "/" . "webHookSuffixRoute". "/" ."OrderNumber",
42+
//back_url example when you want to take your host base url
43+
//'back_url' => $request->getSchemeAndHttpHost(),
44+
//webhook_url example when you want to take your host base url and add your suffix route for the webhook
45+
//'webhook_url' => $request->getSchemeAndHttpHost() . "/" . you_back_url_suffix_here . "/" .Order_Number,
46+
'comment' => 'My Payment Comment.',
47+
'api_key' => $this->getParameter('api_key'),
48+
);
49+
50+
$chargyliController = new ChargilyEpaySymfonyController();
51+
52+
$response = $chargyliController->pay($payload);
53+
$status_code = $response->getStatusCode();
54+
$response = json_decode($response->getContent());
55+
if ($status_code == 200) {
56+
//redirect to chargily payment gateway
57+
return $this->redirect($response->response);
58+
} else {
59+
// This is a error message depending on issue that happen
60+
dd($status_code . " " . $response->response);
61+
}
62+
63+
```
64+
65+
6. success Message for the Process payment
66+
```bash
67+
200 => getting redirection link with success => Redirection to url
68+
```
69+
70+
7. Error Message for the Process payment
71+
```bash
72+
400 => There mode must be CIB,EDAHABIA option Only
73+
400 => There amount must be numeric and greather or equal than 75
74+
400 => There is issue \for connecting payment gateway. Sorry \for the inconvenience => with error message
75+
400 => There is missing information in payment parameters
76+
```
77+
8. Webhook Template
78+
```bash
79+
/**
80+
* @Route("/chargily/webhook/{OrderNumber}",name="chargily_webhook")
81+
* @throws \Exception
82+
*/
83+
public function chargilyWebhook(Request $request)
84+
{
85+
//getting your order number
86+
$number = $request->attributes->get('OrderNumber');
87+
88+
//part or code for searching your order by number
89+
/*
90+
*
91+
*/
92+
93+
//getting request content
94+
$data = json_decode($request->getContent(), true);
95+
$headers = json_decode($request->headers, true);
96+
97+
$hashedData = hash_hmac('sha256', json_encode($data) , $this->getParameter('secret_key'));
98+
99+
if (isset($data) and isset($number)) {
100+
if($data['invoice']['status'] == 'paid'){
101+
102+
//part where you update your order status for paid status
103+
104+
return new JsonResponse([
105+
'code' => 200,
106+
'message' => 'Update status with success'
107+
]);
108+
}elseif($data['invoice']['status'] == 'failed'){
109+
//part where you update your order status for failed status
110+
111+
return new JsonResponse([
112+
'code' => 200,
113+
'message' => 'Update status with success'
114+
]);
115+
}
116+
elseif( $data['invoice']['status'] == 'canceled'){
117+
//part where you update your order status for canceled status
118+
return new JsonResponse([
119+
'code' => 200,
120+
'message' => 'Update status with success'
121+
]);
122+
}
123+
} else {
124+
return new JsonResponse([
125+
'code' => 400,
126+
'message' => 'Update status Failed'
127+
]);
128+
}
129+
130+
}
131+
```
132+
9. Clear the Cache And Enjoy
133+
```bash
134+
php bin/console cache:clear
135+
```
136+
137+
This Plugin is to integrate ePayment gateway with Chargily easily.
138+
- Currently support payment by **CIB / EDAHABIA** cards and soon by **Visa / Mastercard**
139+
- This repo is recently created for **Sylius Plugin**, If you are a developer and want to collaborate to the development of this plugin, you are welcomed!
140+
141+
# Contribution tips
142+
1. Make a fork of this repo.
143+
2. Take a tour to our [API documentation here](https://dev.chargily.com/docs/#/epay_integration_via_api)
144+
3. Get your API Key/Secret from [ePay by Chargily](https://epay.chargily.com.dz) dashboard for free.
145+
4. Start developing.
146+
5. Finished? Push and merge.

composer.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "chargily/epay-symfony",
3+
"type": "symfony-bundle",
4+
"description": "Chargily epay plugin for symfony.",
5+
"license": "MIT",
6+
"require": {
7+
"php": "^7.4 || ^8.0",
8+
"guzzlehttp/guzzle": "7.4.x-dev",
9+
"ext-json": "*"
10+
},
11+
"require-dev": {
12+
"behat/behat": "^3.4",
13+
"behat/mink": "^1.7@dev",
14+
"behat/mink-browserkit-driver": "^1.3",
15+
"behat/mink-extension": "^2.2",
16+
"behat/mink-selenium2-driver": "^1.3",
17+
"friends-of-behat/context-service-extension": "^1.2",
18+
"friends-of-behat/cross-container-extension": "^1.1",
19+
"friends-of-behat/service-container-extension": "^1.0",
20+
"friends-of-behat/symfony-extension": "^1.2.1",
21+
"friends-of-behat/variadic-extension": "^1.1",
22+
"lakion/mink-debug-extension": "^1.2.3",
23+
"phpspec/phpspec": "^5.0",
24+
"sylius-labs/coding-standard": "^2.0",
25+
"symfony/browser-kit": "^3.4|^4.1",
26+
"symfony/debug-bundle": "^3.4|^4.1",
27+
"symfony/dotenv": "^3.4|^4.1",
28+
"symfony/intl": "^3.4|^4.1",
29+
"symfony/web-profiler-bundle": "^3.4|^4.1",
30+
"symfony/web-server-bundle": "^3.4|^4.1",
31+
"sensiolabs/security-checker": "^4.1"
32+
},
33+
"prefer-stable": false,
34+
"minimum-stability": "dev",
35+
"autoload": {
36+
"psr-4": {
37+
"Chargily\\SymfonyBundle\\": "src/"
38+
}
39+
}
40+
}

src/ChargilySymfonyBundle.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
namespace Chargily\SymfonyBundle;
3+
4+
use Chargily\SymfonyBundle\Controller\ChargilyEpaySymfonyController;
5+
use Symfony\Component\HttpKernel\Bundle\Bundle;
6+
7+
class ChargilySymfonyBundle extends Bundle
8+
{
9+
public function getPath(): string
10+
{
11+
return __DIR__;
12+
}
13+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Chargily\SymfonyBundle\Controller;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Exception\GuzzleException;
7+
use GuzzleHttp\RequestOptions;
8+
use Payum\Core\Reply\HttpRedirect;
9+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10+
use Symfony\Component\DependencyInjection\ContainerInterface;
11+
use Symfony\Component\HttpFoundation\JsonResponse;
12+
use Symfony\Component\HttpFoundation\Request;
13+
14+
class ChargilyEpaySymfonyController extends AbstractController
15+
{
16+
/*
17+
public function __construct(ContainerInterface $container)
18+
{
19+
$this->setContainer($container);
20+
}
21+
*/
22+
/**
23+
* @Route("/chargily/pay",name="chargily_epay_symfony")
24+
* @throws \Exception
25+
*/
26+
public function pay(array $payload)
27+
{
28+
if(!isset($payload['mode']) or ($payload['mode'] != 'CIB' and $payload['mode'] != 'EDAHABIA')){
29+
return new JsonResponse(['response' => 'There mode must be CIB,EDAHABIA option Only'], 400);
30+
}
31+
32+
if(!isset($payload['amount']) or $payload['amount'] < 75){
33+
return new JsonResponse(['response' => 'There amount must be numeric and greather or equal than 75'], 400);
34+
}
35+
36+
if (isset($payload['invoice_number'])
37+
&& isset($payload['amount'])
38+
&& isset($payload['discount'])
39+
&& isset($payload['back_url'])
40+
&& isset($payload['api_key'])) {
41+
42+
$environment_url = 'http://epay.chargily.com.dz/api/invoice';
43+
44+
$client = new Client();
45+
$client = new \GuzzleHttp\Client(['verify' => false]);
46+
47+
try {
48+
$response = $client->post($environment_url, [
49+
RequestOptions::HEADERS => [
50+
"X-Authorization" => $payload['api_key'],
51+
'Accept' => 'application/json'
52+
],
53+
RequestOptions::FORM_PARAMS => $payload,
54+
RequestOptions::TIMEOUT => 90
55+
]);
56+
57+
$response = json_decode($response->getBody()->getContents(), true);
58+
$redirectUrl = $response['checkout_url'];
59+
return new JsonResponse(['response' => $redirectUrl], 200);
60+
} catch (\Exception $exception) {
61+
return new JsonResponse(['response' => "There is issue for connecting payment gateway. Sorry for the inconvenience. {$exception->getMessage()}"], 400,);
62+
} catch (GuzzleException $e) {
63+
return new JsonResponse(['response' => "There is issue for connecting payment gateway. Sorry for the inconvenience. {$e->getMessage()}"], 400,);
64+
}
65+
} else {
66+
return new JsonResponse(['response' => 'There is missing information in payment parameters'], 400);
67+
}
68+
}
69+
}

src/config/services.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
services:
2+
_defaults:
3+
autowire: true
4+
autoconfigure: true
5+
public: false
6+
7+
chargily.controller:
8+
class: Chargily\SymfonyBundle\Controller\ChargilyEpaySymfonyController
9+
tags: [ controller.service_arguments ]
10+
arguments:
11+
- '@service_container'

0 commit comments

Comments
 (0)