rajakannan / paypal
Laravel 插件,用于通过 PayPal Express Checkout 处理支付。可以独立于其他应用程序使用。
Requires
- guzzlehttp/guzzle: ~6.0
- illuminate/support: ~5.1|~5.2|~5.3|~5.4|~5.5
- nesbot/carbon: ~1.0
- dev-master
- 1.6.1
- v1.6.0
- 1.5.9
- 1.5.8
- 1.5.7
- 1.5.6
- 1.5.5
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.9
- 1.4.8
- 1.4.7
- 1.4.6
- 1.4.5
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.9
- 1.3.8
- 1.3.7
- 1.3.6
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.9
- 1.2.8
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.0
- 1.0.3
- 1.0.2
- 1.0.1
- 0.2.2
- 0.2.1
- 0.2
This package is auto-updated.
Last update: 2024-09-28 02:23:21 UTC
README
简介
使用此插件,您可以在 Laravel 应用程序中处理或退款支付,并处理来自 PayPal 的 IPN(即时支付通知)。
目前仅支持 PayPal Express Checkout API。
我还创建了一个 示例应用程序,它使用了此包。以下是该应用程序的示例链接
https://laravel-paypal-demo.srmk.info/
PayPal API 凭据
此包使用经典的 PayPal Express Checkout。有关如何创建 API 凭据的说明,请参阅此链接
https://developer.paypal.com/docs/classic/api/apiCredentials/#create-an-api-signature
安装
- 使用以下命令安装
composer require srmklive/paypal:~1.0
- 将服务提供者添加到
config/app.php文件中的$providers数组中,例如
Srmklive\PayPal\Providers\PayPalServiceProvider::class
- 将别名添加到
config/app.php文件中的$aliases数组中,例如
'PayPal' => Srmklive\PayPal\Facades\PayPal::class
- 运行以下命令以发布配置
php artisan vendor:publish --provider "Srmklive\PayPal\Providers\PayPalServiceProvider"
配置
- 安装完成后,您需要添加您的 PayPal 设置。以下是在 config/paypal.php 中找到的代码,您应相应地进行更新。
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. ];
使用
以下是一些访问 PayPal 提供者的方法
// 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.
覆盖 PayPal API 配置
您可以通过调用 setApiCredentials 方法来覆盖 PayPal API 配置
$provider->setApiCredentials($config);
设置货币
默认使用的货币是 USD。如果您想更改它,您可以在调用任何相应的 API 方法之前调用 setCurrency 方法来设置不同的货币
$provider->setCurrency('EUR')->setExpressCheckout($data);
额外的 PayPal API 参数
默认情况下,仅使用 PayPal API 调用的特定参数集。但是,如果您想指定其他任何附加参数,您可以在调用任何相应的 API 方法之前调用 addOptions 方法
$options = [ 'BRANDNAME' => 'MyBrand', 'LOGOIMG' => 'https://example.com/mylogo.png', 'CHANNELTYPE' => 'Merchant' ]; $provider->addOptions($options)->setExpressCheckout($data);
注意:任何参数应根据您将要执行的 API 调用来引用。例如,如果您正在执行 SetExpressCheckout,则您必须提供 PayPal 为 SetExpressCheckout 记录的参数,然后才能在 addOptions 方法中提供这些参数。
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);
-
退款交易
$response = $provider->refundTransaction($transactionid); // To issue partial refund, you must provide the amount as well for refund: $response = $provider->refundTransaction($transactionid, 9.99);
-
创建账单协议
// The $token is the value returned from SetExpressCheckout API call $response = $provider->createBillingAgreement($token);
-
创建重复支付配置文件
// 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);
-
获取重复支付配置文件详细信息
$response = $provider->getRecurringPaymentsProfileDetails($profileid);
-
更新重复支付配置文件
$response = $provider->updateRecurringPaymentsProfile($data, $profileid);
-
管理重复支付配置文件状态
// Cancel recurring payment profile $response = $provider->cancelRecurringPaymentsProfile($profileid); // Suspend recurring payment profile $response = $provider->suspendRecurringPaymentsProfile($profileid); // Reactivate recurring payment profile $response = $provider->reactivateRecurringPaymentsProfile($profileid);
自适应支付
要使用自适应支付,您必须将提供者设置为使用自适应支付
PayPal::setProvider('adaptive_payments');
- 支付
// 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'
接下来,您需要将用户重定向到 PayPal 以授权支付
$redirect_url = $provider->getRedirectUrl('approved', $response['payKey']); return redirect($redirect_url);
处理 PayPal IPN
您还可以处理 PayPal 的即时支付通知。假设您已将 IPN URL 设置为 PayPal 中的 http://example.com/ipn/notify/。要处理 IPN,您应执行以下操作
-
首先将
ipn/notify添加到您的路由文件中Route::post('ipn/notify','PayPalController@postNotify'); // Change it accordingly in your application
-
打开
App\Http\Middleware\VerifyCsrfToken.php并将您的 IPN 路由添加到$excluded路由变量中。'ipn/notify' -
在解析 IPN 响应的函数中写入以下代码
/** * 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 ... } }
创建订阅
- 例如,您想在 PayPal 上创建重复订阅,首先以以下格式将数据传递到
SetExpressCheckoutAPI 调用中
// 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;
- 然后执行在
SetExpressCheckout中列出的剩余步骤。 - 接下来,按照
GetExpressCheckoutDetails中列出的确切步骤进行。 - 最后,针对
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);
支持
此插件仅支持Laravel 5.1及以上版本。
- 如果遇到任何问题,请在问题部分创建一个。
- 如果您想贡献
- 将此存储库进行分支。
- 实现您的功能。
- 生成拉取请求。