Skip to content

Commit 5af3340

Browse files
committed
added method for splitpayments
0 parents  commit 5af3340

16 files changed

+1366
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
build
2+
vendor
3+
.DS_Store
4+
composer.lock
5+
.idea

.travis.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
language: php
2+
3+
php:
4+
- 5.5
5+
- 5.6
6+
- 7.0
7+
- hhvm
8+
9+
matrix:
10+
allow_failures:
11+
- php: hhvm
12+
13+
install: travis_retry composer install --no-interaction --prefer-source
14+
15+
script:
16+
- mkdir -p build/logs
17+
- php vendor/bin/phpunit -c phpunit.xml.dist
18+
- phpunit --coverage-text --coverage-clover=coverage.clover
19+
- phpunit --coverage-clover build/logs/clover.xml
20+
21+
after_script:
22+
- wget https://scrutinizer-ci.com/ocular.phar
23+
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
24+
- travis_retry php vendor/bin/coveralls -v
25+
26+
notifications:
27+
slack: red-creek:5lI8ybvl6YTcCNPosh4TE13h

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Changelog
2+
3+
All Notable changes to `laravel-paystack` will be documented in this file
4+
5+
## 2015-11-04
6+
- Initial release

CONTRIBUTING.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Contributing
2+
3+
Contributions are **welcome** and will be fully **credited**.
4+
5+
We accept contributions via Pull Requests on [Github](https://github.com/unicodeveloper/laravel-paystack).
6+
7+
8+
## Pull Requests
9+
10+
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer).
11+
12+
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
13+
14+
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
15+
16+
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
17+
18+
- **Create feature branches** - Don't ask us to pull from your master branch.
19+
20+
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
21+
22+
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
23+
24+
25+
## Running Tests
26+
27+
``` bash
28+
$ composer test
29+
```
30+
31+
32+
**Happy coding**!

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The MIT License (MIT)
2+
3+
Copyright (c) 2015 Prosper Otemuyiwa <prosperotemuyiwa@gmail.com>
4+
5+
> Permission is hereby granted, free of charge, to any person obtaining a copy
6+
> of this software and associated documentation files (the "Software"), to deal
7+
> in the Software without restriction, including without limitation the rights
8+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
> copies of the Software, and to permit persons to whom the Software is
10+
> furnished to do so, subject to the following conditions:
11+
>
12+
> The above copyright notice and this permission notice shall be included in
13+
> all copies or substantial portions of the Software.
14+
>
15+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
> THE SOFTWARE.

README.md

Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
# laravel-paystack
2+
3+
[![Latest Stable Version](https://poser.pugx.org/unicodeveloper/laravel-paystack/v/stable.svg)](https://packagist.org/packages/unicodeveloper/laravel-paystack)
4+
[![License](https://poser.pugx.org/unicodeveloper/laravel-paystack/license.svg)](LICENSE.md)
5+
[![Build Status](https://img.shields.io/travis/unicodeveloper/laravel-paystack.svg)](https://travis-ci.org/unicodeveloper/laravel-paystack)
6+
[![Quality Score](https://img.shields.io/scrutinizer/g/unicodeveloper/laravel-paystack.svg?style=flat-square)](https://scrutinizer-ci.com/g/unicodeveloper/laravel-paystack)
7+
[![Total Downloads](https://img.shields.io/packagist/dt/unicodeveloper/laravel-paystack.svg?style=flat-square)](https://packagist.org/packages/unicodeveloper/laravel-paystack)
8+
9+
> A Laravel 5 Package for working with Paystack seamlessly
10+
11+
## Installation
12+
13+
[PHP](https://php.net) 5.4+ or [HHVM](http://hhvm.com) 3.3+, and [Composer](https://getcomposer.org) are required.
14+
15+
To get the latest version of Laravel Paystack, simply add the following line to the require block of your `composer.json` file.
16+
17+
```
18+
"unicodeveloper/laravel-paystack": "1.0.*"
19+
```
20+
21+
You'll then need to run `composer install` or `composer update` to download it and have the autoloader updated.
22+
23+
Once Laravel Paystack is installed, you need to register the service provider. Open up `config/app.php` and add the following to the `providers` key.
24+
25+
* `Unicodeveloper\Paystack\PaystackServiceProvider::class`
26+
27+
Also, register the Facade like so:
28+
29+
```php
30+
'aliases' => [
31+
...
32+
'Paystack' => Unicodeveloper\Paystack\Facades\Paystack::class,
33+
...
34+
]
35+
```
36+
37+
## Configuration
38+
39+
You can publish the configuration file using this command:
40+
41+
```bash
42+
php artisan vendor:publish --provider="Unicodeveloper\Paystack\PaystackServiceProvider"
43+
```
44+
45+
A configuration-file named `paystack.php` with some sensible defaults will be placed in your `config` directory:
46+
47+
```php
48+
<?php
49+
50+
return [
51+
52+
/**
53+
* Public Key From Paystack Dashboard
54+
*
55+
*/
56+
'publicKey' => getenv('PAYSTACK_PUBLIC_KEY'),
57+
58+
/**
59+
* Secret Key From Paystack Dashboard
60+
*
61+
*/
62+
'secretKey' => getenv('PAYSTACK_SECRET_KEY'),
63+
64+
/**
65+
* Paystack Payment URL
66+
*
67+
*/
68+
'paymentUrl' => getenv('PAYSTACK_PAYMENT_URL'),
69+
70+
/**
71+
* Optional email address of the merchant
72+
*
73+
*/
74+
'merchantEmail' => getenv('MERCHANT_EMAIL'),
75+
76+
];
77+
```
78+
79+
80+
##General payment flow
81+
82+
Though there are multiple ways to pay an order, most payment gateways expect you to follow the following flow in your checkout process:
83+
84+
###1. The customer is redirected to the payment provider
85+
After the customer has gone through the checkout process and is ready to pay, the customer must be redirected to site of the payment provider.
86+
87+
The redirection is accomplished by submitting a form with some hidden fields. The form must post to the site of the payment provider. The hidden fields minimally specify the amount that must be paid, the order id and a hash.
88+
89+
The hash is calculated using the hidden form fields and a non-public secret. The hash used by the payment provider to verify if the request is valid.
90+
91+
92+
###2. The customer pays on the site of the payment provider
93+
The customer arrived on the site of the payment provider and gets to choose a payment method. All steps necessary to pay the order are taken care of by the payment provider.
94+
95+
###3. The customer gets redirected back
96+
After having paid the order the customer is redirected back. In the redirection request to the shop-site some values are returned. The values are usually the order id, a paymentresult and a hash.
97+
98+
The hash is calculated out of some of the fields returned and a secret non-public value. This hash is used to verify if the request is valid and comes from the payment provider. It is paramount that this hash is thoroughly checked.
99+
100+
101+
## Usage
102+
103+
Open your .env file and add your public key, secret key, merchant email and payment url like so:
104+
105+
```php
106+
PAYSTACK_PUBLIC_KEY=xxxxxxxxxxxxx
107+
PAYSTACK_SECRET_KEY=xxxxxxxxxxxxx
108+
PAYSTACK_PAYMENT_URL=https://api.paystack.co
109+
MERCHANT_EMAIL=unicodeveloper@gmail.com
110+
```
111+
112+
Set up routes and controller methods like so:
113+
114+
Note: Make sure you have `/payment/callback` registered in Paystack Dashboard [https://dashboard.paystack.co/#/settings/developer](https://dashboard.paystack.co/#/settings/developer) like so:
115+
116+
![payment-callback](https://cloud.githubusercontent.com/assets/2946769/12746754/9bd383fc-c9a0-11e5-94f1-64433fc6a965.png)
117+
118+
```php
119+
Route::post('/pay', 'PaymentController@redirectToGateway')->name('pay'); // Laravel 5.1.17 and above
120+
121+
OR
122+
123+
Route::post('/pay', [
124+
'uses' => 'PaymentController@redirectToGateway',
125+
'as' => 'pay'
126+
]);
127+
128+
Route::get('/payment/callback', 'PaymentController@handleGatewayCallback');
129+
```
130+
131+
```php
132+
<?php
133+
134+
namespace App\Http\Controllers;
135+
136+
use Illuminate\Http\Request;
137+
138+
use App\Http\Requests;
139+
use App\Http\Controllers\Controller;
140+
use Paystack;
141+
142+
class PaymentController extends Controller
143+
{
144+
145+
/**
146+
* Redirect the User to Paystack Payment Page
147+
* @return Url
148+
*/
149+
public function redirectToGateway()
150+
{
151+
return Paystack::getAuthorizationUrl()->redirectNow();
152+
}
153+
154+
/**
155+
* Obtain Paystack payment information
156+
* @return void
157+
*/
158+
public function handleGatewayCallback()
159+
{
160+
$paymentDetails = Paystack::getPaymentData();
161+
162+
dd($paymentDetails);
163+
// Now you have the payment details,
164+
// you can store the authorization_code in your db to allow for recurrent subscriptions
165+
// you can then redirect or do whatever you want
166+
}
167+
}
168+
```
169+
170+
Let me explain the fluent methods this package provides a bit here.
171+
```php
172+
/**
173+
* This fluent method does all the dirty work of sending a POST request with the form data
174+
* to Paystack Api, then it gets the authorization Url and redirects the user to Paystack
175+
* Payment Page. I abstracted all of it, so you don't have to worry about that.
176+
* Just eat your cookies while coding!
177+
*/
178+
Paystack::getAuthorizationUrl()->redirectNow();
179+
180+
/**
181+
* This fluent method does all the dirty work of verifying that the just concluded transaction was actually valid,
182+
* It verifies the transaction reference with Paystack Api and then grabs the data returned from Paystack.
183+
* In that data, we have a lot of good stuff, especially the `authorization_code` that you can save in your db
184+
* to allow for easy recurrent subscription.
185+
*/
186+
Paystack::getPaymentData();
187+
188+
/**
189+
* This method gets all the customers that have performed transactions on your platform with Paystack
190+
* @returns array
191+
*/
192+
Paystack::getAllCustomers();
193+
194+
/**
195+
* This method gets all the plans that you have registered on Paystack
196+
* @returns array
197+
*/
198+
Paystack::getAllPlans();
199+
200+
/**
201+
* This method gets all the transactions that have occurred
202+
* @returns array
203+
*/
204+
Paystack::getAllTransactions();
205+
206+
/**
207+
* This method generates a unique super secure cryptograhical hash token to use as transaction reference
208+
* @returns string
209+
*/
210+
Paystack::genTranxRef();
211+
```
212+
213+
A sample form will look like so:
214+
215+
```html
216+
<form method="POST" action="{{ route('pay') }}" accept-charset="UTF-8" class="form-horizontal" role="form">
217+
<div class="row" style="margin-bottom:40px;">
218+
<div class="col-md-8 col-md-offset-2">
219+
<p>
220+
<div>
221+
Lagos Eyo Print Tee Shirt
222+
₦ 2,950
223+
</div>
224+
</p>
225+
<input type="hidden" name="email" value="otemuyiwa@gmail.com"> {{-- required --}}
226+
<input type="hidden" name="orderID" value="345">
227+
<input type="hidden" name="amount" value="800"> {{-- required in kobo --}}
228+
<input type="hidden" name="quantity" value="3">
229+
<input type="hidden" name="reference" value="{{ Paystack::genTranxRef() }}"> {{-- required --}}
230+
<input type="hidden" name="key" value="{{ config('paystack.secretKey') }}"> {{-- required --}}
231+
{{ csrf_field() }} {{-- works only when using laravel 5.1, 5.2 --}}
232+
233+
<input type="hidden" name="_token" value="{{ csrf_token() }}"> {{-- employ this in place of csrf_field only in laravel 5.0 --}}
234+
235+
236+
<p>
237+
<button class="btn btn-success btn-lg btn-block" type="submit" value="Pay Now!">
238+
<i class="fa fa-plus-circle fa-lg"></i> Pay Now!
239+
</button>
240+
</p>
241+
</div>
242+
</div>
243+
</form>
244+
```
245+
246+
When clicking the submit button the customer gets redirected to the Paystack site.
247+
248+
So now we've redirected the customer to Paystack. The customer did some actions there (hopefully he or she paid the order) and now gets redirected back to our shop site.
249+
250+
Paystack will redirect the customer to the url of the route that is specified in the Callback URL of the Web Hooks section on Paystack dashboard.
251+
252+
We must validate if the redirect to our site is a valid request (we don't want imposters to wrongfully place non-paid order).
253+
254+
In the controller that handles the request coming from the payment provider, we have
255+
256+
`Paystack::getPaymentData()` - This function calls the verification methods and ensure it is a valid transction else it throws an exception.
257+
258+
You can test with these details
259+
260+
```bash
261+
Card Number: 4123450131001381
262+
Expiry Date: any date in the future
263+
CVV: 883
264+
```
265+
266+
## Todo
267+
268+
* Charge Returning Customers
269+
* Add Comprehensive Tests
270+
* Implement Transaction Dashboard to see all of the transactions in your laravel app
271+
272+
## Contributing
273+
274+
Please feel free to fork this package and contribute by submitting a pull request to enhance the functionalities.
275+
276+
## How can I thank you?
277+
278+
Why not star the github repo? I'd love the attention! Why not share the link for this repository on Twitter or HackerNews? Spread the word!
279+
280+
Don't forget to [follow me on twitter](https://twitter.com/unicodeveloper)!
281+
282+
Thanks!
283+
Prosper Otemuyiwa.
284+
285+
## License
286+
287+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

0 commit comments

Comments
 (0)