Skip to content

Commit 3ce8d1c

Browse files
docs(readme): Update readme
1 parent 307a3eb commit 3ce8d1c

File tree

2 files changed

+161
-3
lines changed

2 files changed

+161
-3
lines changed

README.md

Lines changed: 160 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,172 @@ Also, register the Facade like so:
3636

3737
## Configuration
3838

39-
To get started, you'll need to publish all vendor assets:
39+
You can publish the configuration file using this command:
4040

4141
```bash
42-
$ php artisan vendor:publish --provider="Unicodeveloper\Paystack\PaystackServiceProvider"
42+
php artisan config:publish unicodeveloper/laravel-paystack
4343
```
4444

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+
* Unique transaction reference
78+
*/
79+
'reference' => 'payref'.time().'tranx',
80+
];
81+
```
82+
83+
84+
##General payment flow
85+
86+
Though there are multiple ways to pay an order, most payment gateways except you to follow the following flow in your checkout process:
87+
88+
###1. The customer is redirected to the payment provider
89+
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.
90+
91+
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.
92+
93+
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.
94+
95+
96+
###2. The customer pays on the site of the payment provider
97+
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.
98+
99+
###3. The customer gets redirected back
100+
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.
101+
102+
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.
103+
104+
The payment result can be something like "payment ok", "customer cancelled payment" or "payment declined".
105+
106+
45107
## Usage
46108

109+
1. Open your .env file and add your public key, secret key, merchant email and payment url like so:
110+
111+
```php
112+
PAYSTACK_PUBLIC_KEY=xxxxxxxxxxxxx
113+
PAYSTACK_SECRET_KEY=xxxxxxxxxxxxx
114+
PAYSTACK_PAYMENT_URL=https://api.paystack.co
115+
116+
```
117+
118+
2. Set up routes and controller methods like so:
119+
120+
```php
121+
Route::post('/pay', 'PaymentController@redirectToGateway')->name('pay');
122+
Route::get('/payment/callback', 'PaymentController@handleGatewayCallback');
123+
```
124+
125+
```php
126+
<?php
127+
128+
namespace App\Http\Controllers;
129+
130+
use Illuminate\Http\Request;
131+
132+
use App\Http\Requests;
133+
use App\Http\Controllers\Controller;
134+
use Paystack;
135+
136+
class PaymentController extends Controller
137+
{
138+
139+
/**
140+
* Redirect the User to Paystack Payment Page
141+
* @return Url
142+
*/
143+
public function redirectToGateway()
144+
{
145+
return Paystack::getAuthorizationUrl()->redirectNow();
146+
}
147+
148+
/**
149+
* Obtain Paystack payment information
150+
* @return [type] [description]
151+
*/
152+
public function handleGatewayCallback()
153+
{
154+
$paymentDetails = Paystack::getPaymentData();
155+
156+
dd($paymentDetails); // Now you have the payment details,
157+
// you can store the authorisation code in your db to allow for recurrent subscriptions
158+
// you can then redirect or do whatever you want
159+
}
160+
}
161+
```
162+
163+
3. A sample form will look like so:
164+
165+
```html
166+
<form method="POST" action="{{ route('pay') }}" accept-charset="UTF-8" class="form-horizontal" role="form">
167+
<div class="row" style="margin-bottom:40px;">
168+
<div class="col-md-8 col-md-offset-2">
169+
<p>
170+
<div>
171+
Lagos Eyo Print Tee Shirt
172+
₦ 2,950
173+
</div>
174+
</p>
175+
<input type="hidden" name="email" value="[email protected]"> {{-- required --}}
176+
<input type="hidden" name="orderID" value="345">
177+
<input type="hidden" name="amount" value="800"> {{-- required --}}
178+
<input type="hidden" name="quantity" value="3">
179+
<input type="hidden" name="reference" value="{{ config('paystack.reference') }}"> {{-- required --}}
180+
<input type="hidden" name="key" value="{{ config('paystack.secretKey') }}"> {{-- required --}}
181+
{{ csrf_field() }}
182+
183+
<p>
184+
<button class="btn btn-success btn-lg btn-block" type="submit" value="Pay Now!">
185+
<i class="fa fa-plus-circle fa-lg"></i> Pay Now!
186+
</button>
187+
</p>
188+
</div>
189+
</div>
190+
</form>
191+
```
192+
193+
When clicking the submit button the customer gets redirected to the Paystack site.
194+
195+
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.
196+
197+
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.
198+
199+
We must validate if the redirect to our site is a valid request (we don't want imposters to wrongfully place non-paid order).
200+
201+
In the controller that handles the request coming from the payment provider, we have
202+
203+
`Paystack::getPaymentData()` - This function calls the verification methods and ensure it is a valid transction else it throws an exception.
204+
47205
## Contributing
48206

49207
Please feel free to fork this package and contribute by submitting a pull request to enhance the functionalities.

resources/config/paystack.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@
2929
/**
3030
* Unique transaction reference
3131
*/
32-
'reference' => 'payref'.time().'tranx'
32+
'reference' => 'payref'.time().'tranx',
3333
];

0 commit comments

Comments
 (0)