sger/laravel-paypal

Laravel 的 PayPal 桥接器

0.1 2016-07-08 11:05 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:31:11 UTC


README

Build Status Code Climate Test Coverage Issue Count

Laravel Paypal 是使用 PayPal RESTful API 包的 PHP SDK 为 Laravel 5 创建的桥接器。本包受到 https://github.com/vinkla/vimeo 的启发。

安装

使用 Composer 在项目的根目录中要求此包。

composer require sger/laravel-paypal

将服务提供者添加到 config/app.php 中的 providers 数组。

Sger\Paypal\PaypalServiceProvider::class
'Paypal' => Sger\Paypal\Facades\Paypal::class

配置

php artisan vendor:publish

这将在您的应用中创建一个 config/paypal.php 文件,其中包含两种类型的连接 'sandbox' 和 'live'。

使用方法

在您的 routes.php 中创建例如

Route::resource('payments', 'PaymentsController');

然后在您的控制器中 store 方法中添加以下代码

$payer = new Payer;
$payer->setPaymentMethod("paypal");

$item1 = new Item();
$item1->setName('test')
	->setCurrency('EUR')
	->setQuantity(1)
	->setPrice(10);

$itemList = new ItemList();
$itemList->setItems(array($item1));

$amount = new Amount();
$amount->setCurrency('EUR')
	->setTotal(10);

$transaction = new Transaction();
$transaction->setAmount($amount)
	->setItemList($itemList)
	->setDescription("Payment description")
	->setInvoiceNumber(uniqid());

$baseUrl = \URL::to('/');

$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("$baseUrl/execute-payment?success=true")
->setCancelUrl("$baseUrl/execute-payment?success=false");

$payment = new Payment();

$payment->setIntent("sale")
	->setPayer($payer)
	->setRedirectUrls($redirectUrls)
	->setTransactions(array($transaction));

try {
	$payment->create(\Paypal::connection('sandbox'));
} catch (\PPConnectionException $ex) {
	return  "Exception: " . $ex->getMessage() . PHP_EOL;
	exit(1);
}

$approvalUrl = $payment->getApprovalLink();
// redirect user to the $approvalUrl

许可

Laravel Paypal 使用 MIT 许可证 (MIT) 许可。