rajakannan/paypal
Composer 安装命令:
composer require rajakannan/paypal
包简介
Laravel plugin For Processing Payments Through Paypal Express Checkout. Can Be Used Independently With Other Applications.
README 文档
README
- Introduction
- PayPal API Credentials
- Installation
- Configuration
- Usage
- Handling PayPal IPN
- Creating Subscriptions
- Support
- PayPal Documentation
Introduction
By using this plugin you can process or refund payments and handle IPN (Instant Payment Notification) from PayPal in your Laravel application.
Currently only PayPal Express Checkout API Is Supported.
I have also created a demo application which utilizes this package. Following is the demo link for the application:
https://laravel-paypal-demo.srmk.info/
PayPal API Credentials
This package uses the classic paypal express checkout. Refer to this link on how to create API credentials:
https://developer.paypal.com/docs/classic/api/apiCredentials/#create-an-api-signature
Installation
- Use following command to install:
composer require srmklive/paypal:~1.0
- Add the service provider to your
$providersarray inconfig/app.phpfile like:
Srmklive\PayPal\Providers\PayPalServiceProvider::class
- Add the alias to your
$aliasesarray inconfig/app.phpfile like:
'PayPal' => Srmklive\PayPal\Facades\PayPal::class
- Run the following command to publish configuration:
php artisan vendor:publish --provider "Srmklive\PayPal\Providers\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 [ 'mode' => 'sandbox', // Can only be 'sandbox' Or 'live'. If empty or invalid, 'live' will be used. 'sandbox' => [ 'username' => '', // Api Username 'password' => '', // Api Password 'secret' => '', // This refers to api signature 'certificate' => '', // Link to paypals cert file, storage_path('cert_key_pem.txt') ], 'live' => [ 'username' => '', // Api Username 'password' => '', // Api Password 'secret' => '', // This refers to api signature 'certificate' => '', // Link to paypals cert file, storage_path('cert_key_pem.txt') ], 'payment_action' => 'Sale', // Can Only Be 'Sale', 'Authorization', 'Order' 'currency' => 'USD', 'notify_url' => '', // Change this accordingly for your application. 'validate_ssl' => true, // Validate SSL when creating api client. ];
Usage
Following are some ways through which you can access the paypal provider:
// Import the class namespaces first, before using it directly use Srmklive\PayPal\Services\ExpressCheckout; use Srmklive\PayPal\Services\AdaptivePayments; $provider = new ExpressCheckout; // To use express checkout. $provider = new AdaptivePayments; // To use adaptive payments. // Through facade. No need to import namespaces $provider = PayPal::setProvider('express_checkout'); // To use express checkout(used by default). $provider = PayPal::setProvider('adaptive_payments'); // To use adaptive payments.
Override PayPal API Configuration
You can override PayPal API configuration by calling setApiCredentials method:
$provider->setApiCredentials($config);
Set Currency
By default the currency used is USD. If you wish to change it, you may call setCurrency method to set a different currency before calling any respective API methods:
$provider->setCurrency('EUR')->setExpressCheckout($data);
Additional PayPal API Parameters
By default only a specific set of parameters are used for PayPal API calls. However, if you wish specify any other additional parameters you may call the addOptions method before calling any respective API methods:
$options = [ 'BRANDNAME' => 'MyBrand', 'LOGOIMG' => 'https://example.com/mylogo.png', 'CHANNELTYPE' => 'Merchant' ]; $provider->addOptions($options)->setExpressCheckout($data);
Warning: Any parameters should be referenced accordingly to the API call you will perform. For example, if you are performing SetExpressCheckout, then you must provide the parameters as documented by PayPal for SetExpressCheckout to addOptions method.
Express Checkout
$data = []; $data['items'] = [ [ 'name' => 'Product 1', 'price' => 9.99, 'qty' => 1 ], [ 'name' => 'Product 2', 'price' => 4.99, 'qty' => 2 ] ]; $data['invoice_id'] = 1; $data['invoice_description'] = "Order #{$data[invoice_id]} Invoice"; $data['return_url'] = url('/payment/success'); $data['cancel_url'] = url('/cart'); $total = 0; foreach($data['items'] as $item) { $total += $item['price']*$item['qty']; } $data['total'] = $total;
-
SetExpressCheckout
$response = $provider->setExpressCheckout($data); // Use the following line when creating recurring payment profiles (subscriptions) $response = $provider->setExpressCheckout($data, true); // This will redirect user to PayPal return redirect($response['paypal_link']);
-
GetExpressCheckoutDetails
$response = $provider->getExpressCheckoutDetails($token);
-
DoExpressCheckoutPayment
// Note that 'token', 'PayerID' are values returned by PayPal when it redirects to success page after successful verification of user's PayPal info. $response = $provider->doExpressCheckoutPayment($data, $token, $PayerID);
-
RefundTransaction
$response = $provider->refundTransaction($transactionid); // To issue partial refund, you must provide the amount as well for refund: $response = $provider->refundTransaction($transactionid, 9.99);
-
CreateBillingAgreement
// The $token is the value returned from SetExpressCheckout API call $response = $provider->createBillingAgreement($token);
-
CreateRecurringPaymentsProfile
// The $token is the value returned from SetExpressCheckout API call $startdate = Carbon::now()->toAtomString(); $profile_desc = !empty($data['subscription_desc']) ? $data['subscription_desc'] : $data['invoice_description']; $data = [ 'PROFILESTARTDATE' => $startdate, 'DESC' => $profile_desc, 'BILLINGPERIOD' => 'Month', // Can be 'Day', 'Week', 'SemiMonth', 'Month', 'Year' 'BILLINGFREQUENCY' => 1, // 'AMT' => 10, // Billing amount for each billing cycle 'CURRENCYCODE' => 'USD', // Currency code 'TRIALBILLINGPERIOD' => 'Day', // (Optional) Can be 'Day', 'Week', 'SemiMonth', 'Month', 'Year' 'TRIALBILLINGFREQUENCY' => 10, // (Optional) set 12 for monthly, 52 for yearly 'TRIALTOTALBILLINGCYCLES' => 1, // (Optional) Change it accordingly 'TRIALAMT' => 0, // (Optional) Change it accordingly ]; $response = $provider->createRecurringPaymentsProfile($data, $token);
-
GetRecurringPaymentsProfileDetails
$response = $provider->getRecurringPaymentsProfileDetails($profileid);
-
UpdateRecurringPaymentsProfile
$response = $provider->updateRecurringPaymentsProfile($data, $profileid);
-
ManageRecurringPaymentsProfileStatus
// Cancel recurring payment profile $response = $provider->cancelRecurringPaymentsProfile($profileid); // Suspend recurring payment profile $response = $provider->suspendRecurringPaymentsProfile($profileid); // Reactivate recurring payment profile $response = $provider->reactivateRecurringPaymentsProfile($profileid);
Adaptive Payments
To use adaptive payments, you must set the provider to use Adaptive Payments:
PayPal::setProvider('adaptive_payments');
- Pay
// Change the values accordingly for your application $data = [ 'receivers' => [ [ 'email' => 'johndoe@example.com', 'amount' => 10, 'primary' => true, ], [ 'email' => 'janedoe@example.com', 'amount' => 5, 'primary' => false ] ], 'payer' => 'EACHRECEIVER', // (Optional) Describes who pays PayPal fees. Allowed values are: 'SENDER', 'PRIMARYRECEIVER', 'EACHRECEIVER' (Default), 'SECONDARYONLY' 'return_url' => url('payment/success'), 'cancel_url' => url('payment/cancel'), ]; $response = $provider->createPayRequest($data); // The above API call will return the following values if successful: // 'responseEnvelope.ack', 'payKey', 'paymentExecStatus'
Next, you need to redirect the user to PayPal to authorize the payment
$redirect_url = $provider->getRedirectUrl('approved', $response['payKey']); return redirect($redirect_url);
Handling PayPal IPN
You can also handle Instant Payment Notifications from PayPal. Suppose you have set IPN URL to http://example.com/ipn/notify/ in PayPal. To handle IPN you should do the following:
-
First add the
ipn/notifytp your routes file:Route::post('ipn/notify','PayPalController@postNotify'); // Change it accordingly in your application
-
Open
App\Http\Middleware\VerifyCsrfToken.phpand add your IPN route to$excludedroutes variable.'ipn/notify' -
Write the following code in the function where you will parse IPN response:
/** * Retrieve IPN Response From PayPal * * @param \Illuminate\Http\Request $request */ public function postNotify(Request $request) { // Import the namespace Srmklive\PayPal\Services\ExpressCheckout first in your controller. $provider = new ExpressCheckout; $request->merge(['cmd' => '_notify-validate']); $post = $request->all(); $response = (string) $provider->verifyIPN($post); if ($response === 'VERIFIED') { // Your code goes here ... } }
Create Subscriptions
- For example, you want to create a recurring subscriptions on paypal, first pass data to
SetExpressCheckoutAPI call in following format:
// Always update the code below accordingly to your own requirements. $data = []; $data['items'] = [ [ 'name' => "Monthly Subscription", 'price' => 0, 'qty' => 1, ], ]; $data['subscription_desc'] = "Monthly Subscription #1"; $data['invoice_id'] = 1; $data['invoice_description'] = "Monthly Subscription #1"; $data['return_url'] = url('/paypal/ec-checkout-success?mode=recurring'); $data['cancel_url'] = url('/'); $total = 0; foreach ($data['items'] as $item) { $total += $item['price'] * $item['qty']; } $data['total'] = $total;
- Next perform the remaining steps listed in
SetExpressCheckout. - Next perform the exact steps listed in
GetExpressCheckoutDetails. - Finally do the following for
CreateRecurringPaymentsProfile
$amount = 9.99; $description = "Monthly Subscription #1"; $response = $provider->createMonthlySubscription($token, $amount, $description); // To create recurring yearly subscription on PayPal $response = $provider->createYearlySubscription($token, $amount, $description);
Support
This plugin only supports Laravel 5.1 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.
rajakannan/paypal 适用场景与选型建议
rajakannan/paypal 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 41 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 03 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「rest」 「web service」 「paypal」 「laravel paypal」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rajakannan/paypal 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rajakannan/paypal 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rajakannan/paypal 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Model of a web-based resource
A PSR-7 compatible library for making CRUD API endpoints
Retrieve a WebResourceInterface instance over HTTP
Api bundle
http客户端
统计信息
- 总下载量: 41
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-03-03