zenithcoder/laravel-paypal
Composer 安装命令:
composer require zenithcoder/laravel-paypal
包简介
laravel paypal integration for payment processing
README 文档
README
Introduction
By using this library you can process payment from PayPal and listen to events in your Laravel application.
This plugin supports the new paypal rest api.
PayPal API Credentials
This package uses the new paypal rest api. Refer to this link on how to create API credentials:
https://developer.paypal.com/docs/api/overview/
Installation
- Use following command to install:
If you intend to use ExpressCheckout, please to the following README. v2.0 & v3.0 uses the new rest api.
composer require zenithcoder/laravel-paypal
php artisan vendor:publish --provider "zenithcoder\PayPal\PayPalServiceProvider"
Configuration
- After installation, you will need to add your paypal settings. Following is the code you will find in config/paypal.php, which you should update accordingly.
return [ 'client_id' => env('PAYPAL_CLIENT_ID'), //clientid from dashboard 'client_secret' => env('PAYPAL_CLIENT_SECRET'),//client secret from dashboard 'paymentUrl' => env('PAYPAL_PAYMENT_URL'),//payment url e.g. sandbox= https://api-m.sandbox.paypal.com ];
- Add this to
.env.exampleand.env
#PayPal Setting & API Credentials
PAYPAL_CLIENT_ID=
PAYPAL_CLIENT_SECRET=
PAYPAL_PAYMENT_URL=
##General payment flow
Though there are multiple ways to pay an order, most payment gateways expect you to follow the following flow in your checkout process:
###1. The customer is redirected to the payment provider 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.
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,etc
###2. The customer pays on the site of the payment provider 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.
###3. Handle different Event We will post an event to the webhook URL set for your transaction's domain. If it was a live transaction, we will post to your live webhook url and vice-versa.
use Zenithcoder\Paypal\Paypal; public function redirectToPaypal(Request $request) { $this->processCheckout(); return $this->PaypalClient()->getAuthPaymentUrl()->redirectNow(); } public function handlePaypalWebhook() { $paymentDetails = $this->PaypalClient()->getPaymentData(); dd($paymentDetails); $event_types = $paymentDetails['event_types']; switch ($event_types) { case "PAYMENT.SALE.COMPLETED": $this->completedPayment(); break; case "BILLING.SUBSCRIPTION.PAYMENT.FAILED": // Handle payment failed break; // Handle other webhooks case "BILLING.SUBSCRIPTION.CANCELLED": // Handle subscription cancelled break; case "BILLING.SUBSCRIPTION.SUSPENDED": // Handle subscription suspended break; // Handle other webhooks default: break; } } public function PaypalClient(){ return new Paypal(); } public function completedPayment() { //update payment status } public function processCheckout() { try{ $user = Auth::user(); $subscription = Subscription::create([ 'user_id' => Auth::user()->id, 'total'=> request()->query('amount') ]); $cartItems = json_decode( request()->query('meta'),true); foreach($cartItems as $cartItem) { $subscription->transactions()->create([ 'qty' => $cartItem->qty, 'total' => $cartItem->qty*$cartItem->price ]); } } catch(\Exception $e) { return $e->getMessage(); } }
Route::post('/pay', [ 'uses' => 'PaymentController@redirectToPaypal', 'as' => 'pay' ]);
Route::get('/paypal/webhook', 'PaymentController@handlePaypalWebhook');
A sample form will look like so:
<form method="POST" action="{{ route('pay') }}" accept-charset="UTF-8" class="form-horizontal" role="form"> <div class="row" style="margin-bottom:40px;"> <div class="col-md-8 col-md-offset-2"> <p> <div> Robotsea $350 </div> </p> <input type="hidden" name="email" value="awonusiolajide@yahoo.com"> <input type="hidden" name="orderID" value="345"> <input type="hidden" name="amount" value="800"> <input type="hidden" name="quantity" value="3"> <input type="hidden" name="currency_code" value="USD"> <input type="hidden" name="metadata" value="{{ json_encode($array = ['key_name' => 'value',]) }}" > {{-- For other necessary things you want to add to your payload. it is optional though --}} {{ csrf_field() }} {{-- works only when using laravel 5.1, 5.2 --}} <input type="hidden" name="_token" value="{{ csrf_token() }}"> {{-- employ this in place of csrf_field only in laravel 5.0 --}} <p> <button class="btn btn-success btn-lg btn-block" type="submit" value="Pay Now!"> <i class="fa fa-plus-circle fa-lg"></i> Pay Now! </button> </p> </div> </div> </form>
Support
This version supports Laravel 6 or greater.
- In case of any issues, kindly create one on the Issues section.
- If you would like to contribute:
- Fork this repository.
- Implement your features.
- Generate pull request.
zenithcoder/laravel-paypal 适用场景与选型建议
zenithcoder/laravel-paypal 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 05 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 zenithcoder/laravel-paypal 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zenithcoder/laravel-paypal 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 17
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-05-01