digitapeu/paypal

Laravel插件,用于通过PayPal Express Checkout处理支付。可以独立于其他应用程序使用。

1.6.11 2019-12-24 06:21 UTC

README

Software License Latest Version on Packagist Total Downloads StyleCI Code Quality

简介

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

目前仅支持PayPal Express Checkout API。

GitHub仓库 https://github.com/Digitapeu/laravel-paypal

PayPal API凭证

此包使用经典PayPal Express Checkout。有关如何创建API凭证的说明,请参阅此链接

https://developer.paypal.com/docs/classic/api/apiCredentials/#create-an-api-signature

安装

  • 使用以下命令安装
composer require digitapeu/paypal:~1.6.1
  • 将服务提供者添加到您的$providers数组中的config/app.php文件,如下所示
Digitap\PayPal\Providers\PayPalServiceProvider::class
  • 将别名添加到您的$aliases数组中的config/app.php文件,如下所示
'PayPal' => Digitap\PayPal\Facades\PayPal::class
  • 运行以下命令发布配置
php artisan vendor:publish --provider "Digitap\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'    => env('PAYPAL_SANDBOX_API_USERNAME', ''),
        'password'    => env('PAYPAL_SANDBOX_API_PASSWORD', ''),
        'secret'      => env('PAYPAL_SANDBOX_API_SECRET', ''),
        'certificate' => env('PAYPAL_SANDBOX_API_CERTIFICATE', ''),
        'app_id'      => '', // Used for testing Adaptive Payments API in sandbox mode
    ],
    'live' => [
        'username'    => env('PAYPAL_LIVE_API_USERNAME', ''),
        'password'    => env('PAYPAL_LIVE_API_PASSWORD', ''),
        'secret'      => env('PAYPAL_LIVE_API_SECRET', ''),
        'certificate' => env('PAYPAL_LIVE_API_CERTIFICATE', ''),
        'app_id'      => '', // Used for Adaptive Payments API
    ],

    'payment_action' => 'Sale', // Can only be 'Sale', 'Authorization' or 'Order'
    'currency'       => 'USD',
    'notify_url'     => '', // Change this accordingly for your application.
    'locale'         => '', // force gateway language  i.e. it_IT, es_ES, en_US ... (for express checkout only)
    'validate_ssl'   => true, // Validate SSL when creating api client.
];
  • 将其添加到.env.example.env
#PayPal Setting & API Credentials - sandbox
PAYPAL_SANDBOX_API_USERNAME=
PAYPAL_SANDBOX_API_PASSWORD=
PAYPAL_SANDBOX_API_SECRET=
PAYPAL_SANDBOX_API_CERTIFICATE=

#PayPal Setting & API Credentials - live
PAYPAL_LIVE_API_USERNAME=
PAYPAL_LIVE_API_PASSWORD=
PAYPAL_LIVE_API_SECRET=
PAYPAL_LIVE_API_CERTIFICATE=

使用

以下是您可以通过它们访问PayPal提供者的方式

// Import the class namespaces first, before using it directly
use Digitap\PayPal\Services\ExpressCheckout;
use Digitap\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,
        'desc'  => 'Description for product 1'
        'qty' => 1
    ],
    [
        'name' => 'Product 2',
        'price' => 4.99,
        'desc'  => 'Description for product 2',
        '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;

//give a discount of 10% of the order amount
$data['shipping_discount'] = round((10 / 100) * $total, 2);

  • 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);

  • RefundTransaction

    $response = $provider->refundTransaction($transactionid);
    
    // To issue partial refund, you must provide the amount as well for refund:
    $response = $provider->refundTransaction($transactionid, 9.99);      

  • CreateBillingAgreement

    // The $token is the value returned from SetExpressCheckout API call
    $response = $provider->createBillingAgreement($token);

  • DoReferenceTransaction

    // The $token is the value returned from CreateBillingAgreement API call
    // $action Can be Order, Sale or Authorization
    // $amount to withdraw from the given BillingAgreement defaults to $. To overwrite use $provider->addOptions
    $response = $provider->doReferenceTransaction($token,$action,$amount);

  • GetTransactionDetails

    // The $token is the value returned from doReferenceTransaction API call
    $response = $provider->getTransactionDetails($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' => 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);

  • GetRecurringPaymentsProfileDetails

    $response = $provider->getRecurringPaymentsProfileDetails($profileid);

  • UpdateRecurringPaymentsProfile

    $response = $provider->updateRecurringPaymentsProfile($data, $profileid);

  • 管理RecurringPaymentsProfile状态

    // 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的即时支付通知。假设您已在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响应的功能中编写以下代码

    /**
     * Retrieve IPN Response From PayPal
     *
     * @param \Illuminate\Http\Request $request
     */
    public function postNotify(Request $request)
    {
        // Import the namespace Digitap\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上创建周期性订阅,首先以以下格式将数据传递给SetExpressCheckout API调用:
// 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;

//give a discount of 10% of the order amount
$data['shipping_discount'] = round((10 / 100) * $total, 2);
$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或更高版本。

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