obydul / larapal
LaraPal 是一个 Laravel 插件,用于通过 PayPal 处理支付。
v1.0.8
2023-10-18 07:50 UTC
Requires
- laravel/framework: >=7.0
README
简介
使用此插件,您可以在 Laravel 应用程序中处理或退款 PayPal 支付。
PayPal API 凭据
此包使用经典 PayPal 快速结账。有关如何创建 API 凭据的说明,请参阅以下链接
https://developer.paypal.com/docs/classic/api/apiCredentials/#create-an-api-signature
安装
- 使用以下命令进行安装
composer require obydul/larapal
- Laravel 5.5 使用包自动发现,因此不需要您手动添加 ServiceProvider。如果您不使用自动发现,请将 service provider 添加到您的
config/app.php
文件中的$providers
数组中
Obydul\LaraPal\LarapalServiceProvider::class
- 运行以下命令以发布配置
php artisan vendor:publish --provider="Obydul\LaraPal\LarapalServiceProvider"
安装完成。
配置
- 安装后,您需要将 PayPal 凭据设置在 .env 文件中。
LARAPAL_MODE=sandbox # sandbox or live LARAPAL_API_USERNAME= # paypal api username LARAPAL_API_PASSWORD= # paypal api password LARAPAL_API_SIGNATURE= # paypal api signature
- 现在优化应用程序:
php artisan optimize && php artisan config:clear
。
用法
以下是一些访问 LaraPal 提供商的方法
// Import the class namespaces first, before using it directly use Obydul\LaraPal\Services\ExpressCheckout; // Create object instance $paypal = new ExpressCheckout(); // Redirect user to PayPal to obtain charging permissions $paypal->doExpressCheckout(123.45, 'LaraPal Test Checkout', 'invoice_id', 'USD'); // single payment $paypal->doExpressMultipleCheckout($items, 'invoice_id', 'USD', false, $customFields); // multiple items // Perform payment, token and PayerID are being returned with GET response from PayPal $paypal->doSinglePayment($_GET['token'], $_GET['PayerID']; // single payment $paypal->doMultiplePayment($_GET['token'], $_GET['PayerID']; // multiple payment // Perform refund based on transaction ID $paypal->doRefund($transactionId, 'invoice_id', false, 0, 'USD', ''); // full refund $paypal->doRefund($transactionId, 'invoice_id', true, 5.15, 'USD', ''); // partial refund // Get transaction details $details = $paypal->getTransactionDetails($transactionId);
doExpressCheckout
// Structure - invoice ID must be unique doExpressCheckout(AMOUNT, 'DESCRIPTION', 'INVOICE', 'CURRENCY', SHIPPING, CUSTOMFIELDS); doExpressMultipleCheckout(ITEMS, 'INVOICE', 'CURRENCY', SHIPPING, CUSTOMFIELDS); // Normal call doExpressCheckout(123.45, 'LaraPal Test Checkout', 'invoice_id', 'USD'); // Pass custom fields to your order $customFields = array( 'identifier' => "Example.com/ID", 'customerEmail' => "customer@email.com", ); // Now do the express checkout. If you don't like to pass custom fields, then remove $customFields. $paypal->doExpressCheckout(123.45, 'LaraPal Test Checkout', 'invoice_id', 'USD', false, $customFields); // multiple items payment $items = array( array( "name" => "Product 1", "price" => "12.25", "quantity" => "1", "product_id" => 111 ), array( "name" => "Product 2", "price" => "25.50", "quantity" => "1", "product_id" => 112 ) ); $paypal->doExpressMultipleCheckout($items, $invoice_id, 'USD', false, $customFields);
doRefund
// Structure doExpressCheckout(TRANSACTION_ID, 'INVOICE_ID', 'IS_PARTIAL', PARTIAL_AMOUNT, CURRENCY, NOTE); // Full refund doRefund($transactionId, 'invoice_id', false, 0, 'USD', '') // Partial refund doRefund($transactionId, 'invoice_id', true, 12.25, 'USD', '') // you can pass note also
示例
安装 LaraPal 后,创建路由并创建一个名为 'PayPalController' 的控制器
// Routes Route::get('payment-status', 'PayPalController@paymentStatus')->name('payment-status'); Route::get('single-payment', 'PayPalController@singlePayment'); Route::get('multiple-payment', 'PayPalController@multipleItemsPayment'); Route::get('do-the-payment', 'PayPalController@doThePayment'); Route::get('refund-payment', 'PayPalController@doRefund'); Route::get('cancel-payment', 'PayPalController@cancelPayment'); // Controller php artisan make:controller PayPalController
现在在 PayPalController 中添加以下内容:PayPalController.php
现在只需访问 URL 执行操作
http://example.com/single-payment http://example.com/multiple-payment http://example.com/refund-payment
支付成功后,您将被重定向到 http://example.com/payment-status
,并看到类似的消息: 成功!交易 ID:9TR987531T2702301
。
许可
MIT 许可证 (MIT)。有关更多信息,请参阅许可文件。
其他
受 paypal-express-checkout 启发,并感谢 romaonthego。
如有任何问题,请在 问题 部分创建一个。
感谢您安装 LaraPal。