|
| 1 | +# Laravel payment processor package for UnitPay gateway |
| 2 | + |
| 3 | +[](https://packagist.org/packages/maksa988/laravel-unitpay) |
| 4 | +[](https://www.codefactor.io/repository/github/maksa988/laravel-unitpay) |
| 5 | +[](https://scrutinizer-ci.com/g/maksa988/laravel-unitpay) |
| 6 | +[](https://packagist.org/packages/maksa988/laravel-unitpay) |
| 7 | +[](https://packagist.org/packages/maksa988/laravel-unitpay) |
| 8 | + |
| 9 | +Accept payments via UnitPay ([unitpay.ru](https://unitpay.ru/)) using this Laravel framework package ([Laravel](https://laravel.com)). |
| 10 | + |
| 11 | +- receive payments, adding just the two callbacks |
| 12 | + |
| 13 | +#### Laravel 5.3, 5.4, PHP >= 5.6.4 |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +Require this package with composer. |
| 18 | + |
| 19 | +``` bash |
| 20 | +composer require maksa988/laravel-unitpay |
| 21 | +``` |
| 22 | + |
| 23 | +If you don't use auto-discovery, add the ServiceProvider to the providers array in `config/app.php` |
| 24 | + |
| 25 | +```php |
| 26 | +Maksa988\UnitPay\UnitPayServiceProvider::class, |
| 27 | +``` |
| 28 | + |
| 29 | +Add the `UnitPay` facade to your facades array: |
| 30 | + |
| 31 | +```php |
| 32 | +'UnitPay' => Maksa988\UnitPay\Facades\UnitPay::class, |
| 33 | +``` |
| 34 | + |
| 35 | +Copy the package config to your local config with the publish command: |
| 36 | +``` bash |
| 37 | +php artisan vendor:publish --provider="Maksa988\UnitPay\UnitPayServiceProvider" |
| 38 | +``` |
| 39 | + |
| 40 | +## Configuration |
| 41 | + |
| 42 | +Once you have published the configuration files, please edit the config file in `config/unitpay.php`. |
| 43 | + |
| 44 | +- Create an account on [unitpay.ru](http://unitpay.ru) |
| 45 | +- Add your project, copy the `project_id`, `secret_key` and `secret_key_second` params and paste into `config/unitpay.php` |
| 46 | +- After the configuration has been published, edit `config/unitpay.php` |
| 47 | +- Set the callback static function for `searchOrder` and `paidOrder` |
| 48 | +- Create route to your controller, and call `UnitPay::handle` method |
| 49 | + |
| 50 | +## Usage |
| 51 | + |
| 52 | +1) Generate a payment url or get redirect: |
| 53 | + |
| 54 | +```php |
| 55 | +$amount = 100; // Payment`s amount |
| 56 | + |
| 57 | +$email = "example@gmail.com"; // Your customer`s email |
| 58 | + |
| 59 | +$description = "Test payment"; |
| 60 | + |
| 61 | +// |
| 62 | + |
| 63 | +$url = UnitPay::getPayUrl($amount, $order_id, $email, $description, $currency); |
| 64 | + |
| 65 | +$redirect = UnitPay::redirectToPayUrl($amount, $order_id, $email, $description, $currency); |
| 66 | +``` |
| 67 | + |
| 68 | +2) Process the request from UnitPay: |
| 69 | +``` php |
| 70 | +UnitPay::handle(Request $request) |
| 71 | +``` |
| 72 | + |
| 73 | +## Important |
| 74 | + |
| 75 | +You must define callbacks in `config/unitpay.php` to search the order and save the paid order. |
| 76 | + |
| 77 | + |
| 78 | +``` php |
| 79 | +'searchOrder' => null // UnitPayController@searchOrder(Request $request) |
| 80 | +``` |
| 81 | + |
| 82 | +``` php |
| 83 | +'paidOrder' => null // UnitPayController@paidOrder(Request $request, $order) |
| 84 | +``` |
| 85 | + |
| 86 | +## Example |
| 87 | + |
| 88 | +The process scheme: |
| 89 | + |
| 90 | +1. The request comes from `unitpay.ru` `GET` `http://yourproject.com/unitpay/result` (with params). |
| 91 | +2. The function`UnitPayController@handlePayment` runs the validation process (auto-validation request params). |
| 92 | +3. The method `searchOrder` will be called (see `config/unitpay.php` `searchOrder`) to search the order by the unique id. |
| 93 | +4. If the current order status is NOT `paid` in your database, the method `paidOrder` will be called (see `config/unitpay.php` `paidOrder`). |
| 94 | + |
| 95 | +Add the route to `routes/web.php`: |
| 96 | +``` php |
| 97 | + Route::get('/unitpay/result', 'UnitPayController@handlePayment'); |
| 98 | +``` |
| 99 | + |
| 100 | +> **Note:** |
| 101 | +don't forget to save your full route url (e.g. http://example.com/unitpay/result ) for your project on [unitpay.ru](unitpay.ru). |
| 102 | + |
| 103 | +Create the following controller: `/app/Http/Controllers/UnitPayController.php`: |
| 104 | + |
| 105 | +``` php |
| 106 | +class UnitPayController extends Controller |
| 107 | +{ |
| 108 | + /** |
| 109 | + * Search the order in your database and return that order |
| 110 | + * to paidOrder, if status of your order is 'paid' |
| 111 | + * |
| 112 | + * @param Request $request |
| 113 | + * @param $order_id |
| 114 | + * @return bool|mixed |
| 115 | + */ |
| 116 | + public function searchOrder(Request $request, $order_id) |
| 117 | + { |
| 118 | + $order = Order::where('id', $order_id)->first(); |
| 119 | + |
| 120 | + if($order) { |
| 121 | + $order['_orderSum'] = $order->sum; |
| 122 | + |
| 123 | + // If your field can be `paid` you can set them like string |
| 124 | + $order['_orderStatus'] = $order['status']; |
| 125 | + |
| 126 | + // Else your field doesn` has value like 'paid', you can change this value |
| 127 | + $order['_orderStatus'] = ('1' == $order['status']) ? 'paid' : false; |
| 128 | + |
| 129 | + return $order; |
| 130 | + } |
| 131 | + |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * When paymnet is check, you can paid your order |
| 137 | + * |
| 138 | + * @param Request $request |
| 139 | + * @param $order |
| 140 | + * @return bool |
| 141 | + */ |
| 142 | + public function paidOrder(Request $request, $order) |
| 143 | + { |
| 144 | + $order->status = 'paid'; |
| 145 | + $order->save(); |
| 146 | + |
| 147 | + // |
| 148 | + |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Start handle process from route |
| 154 | + * |
| 155 | + * @param Request $request |
| 156 | + * @return mixed |
| 157 | + */ |
| 158 | + public function handlePayment(Request $request) |
| 159 | + { |
| 160 | + return UnitPay::handle($request); |
| 161 | + } |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | + |
| 166 | +## Changelog |
| 167 | + |
| 168 | +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. |
| 169 | + |
| 170 | +## Contributing |
| 171 | + |
| 172 | +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 173 | + |
| 174 | +## Security |
| 175 | + |
| 176 | +If you discover any security related issues, please send me an email at maksa988ua@gmail.com instead of using the issue tracker. |
| 177 | + |
| 178 | +## Credits |
| 179 | + |
| 180 | +- [Maksa988](https://github.com/maksa988) |
| 181 | +- [All Contributors](../../contributors) |
| 182 | + |
| 183 | +## License |
| 184 | + |
| 185 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments