saleemepoch / paypal

Laravel插件,用于通过PayPal Express处理支付,带有或不带有情境式结账。可用于独立应用程序。

1.1.6 2016-04-08 11:40 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:42:16 UTC


README

分支

这是Srmklive/laravel-paypal的分支。计划使其更加完善和敏捷。

  • 简介
  • 安装
  • 配置
  • 使用
    • [Express Checkout] (#usage-express-checkout)
      • [SetExpressCheckout] (#usage-ec-setexpresscheckout)
      • [GetExpressCheckoutDetails] (#usage-ec-getexpresscheckoutdetails)
      • [DoExpressCheckoutPayment] (#usage-ec-doexpresscheckoutpayment)
      • [RefundTransaction] (#usage-ec-refundtransaction)
      • [CreateBillingAgreement] (#usage-ec-createbillingagreement)
      • [CreateRecurringPaymentsProfile] (#usage-ec-createrecurringprofile)
      • [GetRecurringPaymentsProfileDetails] (#usage-ec-getrecurringprofiledetails)
      • [UpdateRecurringPaymentsProfile] (#usage-ec-updaterecurringprofile)
      • [ManageRecurringPaymentsProfileStatus] (#usage-ec-managerecurringprofile)
    • [Adaptive Payments] (#usage-adaptive-payments)
  • 处理PayPal IPN
  • 支持

简介

通过PayPal处理的Laravel插件。使用此插件,您可以在Laravel应用程序中处理或退款支付并处理来自PayPal的IPN(即时支付通知)。

目前仅支持PayPal Express Checkout、Adaptive Payments API和情境式结账。

安装

  • 使用以下命令安装
composer require saleemepoch/paypal
  • 将服务提供者添加到config/app.php文件中的$providers数组,如下所示
'saleemepoch\PayPal\Providers\PayPalServiceProvider' // Laravel 5
saleemepoch\PayPal\Providers\PayPalServiceProvider::class // Laravel 5.1 or greater
  • 将别名添加到config/app.php文件中的$aliases数组,如下所示
'PayPal' => 'saleemepoch\PayPal\Facades\PayPal' // Laravel 5
'PayPal' => saleemepoch\PayPal\Facades\PayPal::class // Laravel 5.1 or greater
  • 运行以下命令发布配置
php artisan vendor:publish

配置

  • 安装后,您需要添加您的PayPal设置。以下是在config/paypal.php中找到的代码,您应该相应地更新它。
return [
    'mode' => 'sandbox', // Can only be 'sandbox' Or 'live'. If empty or invalid, 'live' will be used.
    'sandbox' => [
        'username' => '',
        'password' => '',
        'secret' => '',
        'certificate' => '',
        /*
        If using In-Context Checkout uncomment the following line. Otherwise,
        leave it commented and it will use the default gateway: https://www.sandbox.paypal.com
        */
        //'gateway_url' => 'https://www.sandbox.paypal.com/checkoutnow/',
    ],
    'live' => [
        'username' => '',
        'password' => '',
        'secret' => '',
        'certificate' => '',
		/*
		If using In-Context Checkout uncomment the following line. Otherwise,
		leave it commented and it will use the default gateway: https://www.sandbox.paypal.com
		*/
		//'gateway_url' => 'https://www.paypal.com/checkoutnow/',
    ],
    'payment_action' => 'Sale', // Can Only Be 'Sale', 'Authorization', 'Order'
    'currency' => 'USD',
    'notify_url' => '', // Change this accordingly for your application.
];

情境式结账

除了更改网关(如上所述)外,请记住还包括所需的JS,没有它情境式结账将无法工作。

点击此处获取更多信息:https://developer.paypal.com/docs/classic/express-checkout/in-context/integration/

使用

  • 设置提供者
PayPal::setProvider('express_checkout');    // To use PayPal Express Checkout API (Used by default)
PayPal::setProvider('adaptive_payments');   // To use PayPal Adaptive Payments API

Express Checkout

$data = [];
$data['items'] = [
    [
        'name' => 'Product 1',
        'price' => 9.99
    ],
    [
        'name' => 'Product 2',
        'price' => 4.99
    ]
];

$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'];
}

$data['total'] = $total;

  • SetExpressCheckout

    $response = PayPal::getProvider()->setExpressCheckout($data);
    
    // Use the following line when creating recurring payment profiles (subscriptions)
    $response = PayPal::getProvider()->setExpressCheckout($data, true);
    
     // This will redirect user to PayPal
    return redirect($response['paypal_link']);
    

  • GetExpressCheckoutDetails

    $response = PayPal::getProvider()->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 = PayPal::getProvider()->doExpressCheckoutPayment($data, $token, $PayerID);
    

  • RefundTransaction

    $response = PayPal::getProvider()->refundTransaction($transactionid);
    

  • CreateBillingAgreement

    // The $token is the value returned from SetExpressCheckout API call
    $response = PayPal::getProvider()->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' => 12, // set 12 for monthly, 52 for yearly
        '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 = PayPal::getProvider()->createRecurringPaymentsProfile($data, $token);
    

  • GetRecurringPaymentsProfileDetails

    $response = PayPal::getProvider()->getRecurringPaymentsProfileDetails($profileid);
    

  • UpdateRecurringPaymentsProfile

    $response = PayPal::getProvider()->updateRecurringPaymentsProfile($data, $profileid);
    

  • ManageRecurringPaymentsProfileStatus

    // Cancel recurring payment profile
    $response = PayPal::getProvider()->cancelRecurringPaymentsProfile($profileid);
    
    // Suspend recurring payment profile
    $response = PayPal::getProvider()->suspendRecurringPaymentsProfile($profileid);
    
    // Reactivate recurring payment profile
    $response = PayPal::getProvider()->reactivateRecurringPaymentsProfile($profileid);    
    

处理PayPal IPN

您还可以处理来自PayPal的即时支付通知。假设您已在PayPal中将IPN URL设置为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的控制器中,执行以下操作

    // Put this above controller definition
    use saleemepoch\PayPal\Traits\IPNResponse As PayPalIPN;
    
    // Then add the following before function declaration
    use PayPalIPN;
    
  • 上述步骤将PayPal IPN响应保存为session中的ipn。以下是可以根据您自己的需求更改的代码,用于处理IPN

    /**
     * Retrieve IPN Response From PayPal
     *
     * @param \Illuminate\Http\Request $request
     */
    public function postNotify(Request $request)
    {
        $post = [];
        $request_params = $request->all();
    
        foreach ($request_params as $key=>$value)
            $post[$key] = $value;
    
        $post['cmd'] = '_notify-validate';
    
        $response = $this->verifyIPN($post);
    
        session([
            'ipn' => $response
        ]);
    }        
    

支持

此插件仅支持Laravel 5或更高版本。

  • 如有任何问题,请在问题部分创建一个问题。
  • 如果您想贡献
    • 此存储库。
    • 实现您的功能。
    • 生成拉取请求。