我有一个 Laravel 应用程序,人们充当买家和卖家。在卖家销售他的产品之前,他应该通过一个按钮链接他的贝宝帐户。如果他们点击按钮,他们应该会看到贝宝登录屏幕,登录后他们会发送回我的页面,我可以在其中从贝宝帐户获取电子邮件并将其保存到我的数据库中。我将充当托管人并持有买家的钱,直到买家从卖家收到货物为止,之后我可以将钱发送给卖家(当卖家链接他的贝宝个人资料时检查链接的电子邮件地址)并将其发送到那里。
现在的问题是,我无法获取电子邮件地址。我没有看到任何对我有帮助的例子。我不想进行任何付款,只是在卖家能够创建要销售的产品之前将 PayPal 帐户链接到我的用户帐户。我如何只接收 PayPal 电子邮件,以便将其保存在我的数据库中给用户?
我尝试过这样的,但它似乎贬值了
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Srmklive\PayPal\Services\PayPal as PayPalClient;
use Illuminate\Support\Facades\Redirect;
class PayPalController extends Controller
{
// Step 1: Redirect to PayPal OAuth page
public function connect(Request $request)
{
$provider = new PayPalClient;
$provider->setApiCredentials(config('paypal'));
// Access Token holen
$accessToken = $provider->getAccessToken();
// OAuth-URL generieren und den Benutzer dorthin weiterleiten
return redirect()->away("https://www.sandbox.paypal.com/connect?flowEntry=static&client_id=" . config('paypal.sandbox.client_id') . "&scope=openid&redirect_uri=" . route('paypal.callback'));
}
public function handlePayPalCallback(Request $request)
{
$provider = new PayPalClient;
$provider->setApiCredentials(config('paypal'));
// Hier kommt der Authorization Code aus dem Request
$authCode = $request->input('code');
// Tausche den Authorization Code gegen ein Access Token
$tokenData = $provider->getAccessToken([
'grant_type' => 'authorization_code',
'code' => $authCode,
]);
// Abrufen der Benutzerinformationen
$userInfo = $provider->getUserInfo($tokenData['access_token']);
// E-Mail-Adresse des Benutzers anzeigen
dd($userInfo['email']);
}
}
您可以通过将 PayPal 与 Laravel Socialite 集成来获取卖家的 PayPal 电子邮件。操作方法如下:
composer require laravel/socialite
composer require socialiteproviders/paypal
将 PayPal 提供商添加到您的 config/services.php 中:
'paypal' => [
'client_id' => env('PAYPAL_CLIENT_ID'),
'client_secret' => env('PAYPAL_CLIENT_SECRET'),
'redirect' => env('PAYPAL_REDIRECT_URI'),
],
在 .env 文件中设置您的 PayPal 凭据:
PAYPAL_CLIENT_ID=your_paypal_client_id
PAYPAL_CLIENT_SECRET=your_paypal_client_secret
PAYPAL_REDIRECT_URI=https://yourdomain.com/paypal/callback
在routes/web.php中更新你的路线:
use App\Http\Controllers\PayPalController;
Route::get('/paypal/connect', [PayPalController::class, 'redirectToProvider'])->name('paypal.connect');
Route::get('/paypal/callback', [PayPalController::class, 'handleProviderCallback'])->name('paypal.callback');
修改您的 PayPalController:
namespace App\Http\Controllers;
use Laravel\Socialite\Facades\Socialite;
class PayPalController extends Controller
{
public function redirectToProvider()
{
return Socialite::driver('paypal')->redirect();
}
public function handleProviderCallback()
{
$paypalUser = Socialite::driver('paypal')->user();
// Save PayPal email to the seller's profile
$user = auth()->user();
$user->paypal_email = $paypalUser->getEmail();
$user->save();
return redirect()->route('dashboard')->with('success', 'PayPal account linked successfully.');
}
}
通过在 app/Providers/EventServiceProvider.php 中添加事件监听器来确保提供程序已加载:
protected $listen = [
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
'SocialiteProviders\\PayPal\\PayPalExtendSocialite@handle',
],
];