此包已被废弃,不再维护。作者建议使用https://github.com/paypal/Checkout-PHP-SDK 包。

Laravel-Paypal 是一个简单的包,帮助您使用 PayPal REST API SDK,在 Laravel 5 项目中处理直接信用卡支付、存储信用卡支付和 PayPal 账户支付。

1.1 2015-03-03 13:31 UTC

This package is auto-updated.

Last update: 2021-01-07 11:21:13 UTC


README

laravel-paypal 处理直接信用卡支付、存储信用卡支付和 PayPal 账户余额支付。它是为与 Laravel 5 一起使用而开发的,并利用了最新的 PayPal REST API SDK。

安装

  1. 在 Laravel 项目的根目录下运行此命令以使用 Composer 安装:composer require netshell/paypal dev-master

  2. 接下来,将服务提供者添加到 app/config/app.php 文件中的 providers 数组中。

'providers' => array(
    // ...
    'Netshell\Paypal\PaypalServiceProvider',
)
  1. 最后,将别名添加到 app/config/app.php 文件中的 aliases 数组中。
'aliases' => array(
    // ...
    'Paypal' => 'Netshell\Paypal\Facades\Paypal',
)

## 配置

使用 $apiContext->setConfig() 方法传入您的 PayPal 详细信息。下面是控制器构造函数中沙盒配置的一个示例

    private $_apiContext;

    public function __construct()
    {
        $this->_apiContext = PayPal::ApiContext(
            config('services.paypal.client_id'),
            config('services.paypal.secret'));
		
		$this->_apiContext->setConfig(array(
			'mode' => 'sandbox',
			'service.EndPoint' => 'https://api.sandbox.paypal.com',
			'http.ConnectionTimeOut' => 30,
			'log.LogEnabled' => true,
			'log.FileName' => storage_path('logs/paypal.log'),
			'log.LogLevel' => 'FINE'
		));

    }

假设您已在 config/services.php 中设置了您的开发者信息

'paypal' => [
	'client_id' => 'Client_ID',
	'secret' => 'Your_secret'
],

代码示例

列出支付

Paypal::getAll(array('count' => 1, 'start_index' => 0), $this->_apiContext);

支付详情

Paypal::getById($payment_id, $this->_apiContext);

示例控制器

此示例提供了一个控制器,它运行包配置,如配置部分中的代码片段所示。

use PayPal;
use Redirect;
// ...
public function getCheckout()
{
	$payer = PayPal::Payer();
	$payer->setPaymentMethod('paypal');

	$amount = PayPal:: Amount();
	$amount->setCurrency('EUR');
	$amount->setTotal(42); // This is the simple way,
	// you can alternatively describe everything in the order separately;
	// Reference the PayPal PHP REST SDK for details.

	$transaction = PayPal::Transaction();
	$transaction->setAmount($amount);
	$transaction->setDescription('What are you selling?');

	$redirectUrls = PayPal:: RedirectUrls();
	$redirectUrls->setReturnUrl(action('ThisController@getDone'));
	$redirectUrls->setCancelUrl(action('ThisController@getCancel'));

	$payment = PayPal::Payment();
	$payment->setIntent('sale');
	$payment->setPayer($payer);
	$payment->setRedirectUrls($redirectUrls);
	$payment->setTransactions(array($transaction));

	$response = $payment->create($this->_apiContext);
	$redirectUrl = $response->links[1]->href;
	
	return Redirect::to( $redirectUrl );
}

public function getDone(Request $request)
{
	$id = $request->get('paymentId');
	$token = $request->get('token');
	$payer_id = $request->get('PayerID');
	
	$payment = PayPal::getById($id, $this->_apiContext);

	$paymentExecution = PayPal::PaymentExecution();

	$paymentExecution->setPayerId($payer_id);
	$executePayment = $payment->execute($paymentExecution, $this->_apiContext);

    // Clear the shopping cart, write to database, send notifications, etc.

    // Thank the user for the purchase
	return view('checkout.done');
}

public function getCancel()
{
    // Curse and humiliate the user for cancelling this most sacred payment (yours)
	return view('checkout.cancel');
}

自定义 PayPal 支付页面

首先我们需要创建一个新的 WebProfile 来获取 ID,然后将来我们可以简单地设置此 ID 到支付对象。

public function createWebProfile(){

	$flowConfig = PayPal::FlowConfig();
	$presentation = PayPal::Presentation();
	$inputFields = PayPal::InputFields();
	$webProfile = PayPal::WebProfile();
	$flowConfig->setLandingPageType("Billing"); //Set the page type

	$presentation->setLogoImage("https://www.example.com/images/logo.jpg")->setBrandName("Example ltd"); //NB: Paypal recommended to use https for the logo's address and the size set to 190x60.

	$inputFields->setAllowNote(true)->setNoShipping(1)->setAddressOverride(0);
	
	$webProfile->setName("Example " . uniqid())
		->setFlowConfig($flowConfig)
		// Parameters for style and presentation.
		->setPresentation($presentation)
		// Parameters for input field customization.
		->setInputFields($inputFields);

	$createProfileResponse = $webProfile->create($this->_apiContext);
        
	return $createProfileResponse->getId(); //The new webprofile's id
}

现在将 WebProfile 的 ID 设置到支付对象 $payment->setExperienceProfileId("XP-ABCD-EFGH-ILMN-OPQR");