finller / laravel-mangopay
mangopay 作为服务提供者
Requires
- php: ^7.4|^8.0
- illuminate/contracts: ^5.0|^6.0|^7.0|^8.0|^9.0
- illuminate/support: ^5.0|^6.0|^7.0|^8.0|^9.0
- mangopay/php-sdk-v2: ^3.7
- spatie/laravel-package-tools: ^1.11
Requires (Dev)
- brianium/paratest: ^6.2
- nunomaduro/collision: ^5.3
- orchestra/testbench: ^6.15
- phpunit/phpunit: ^9.3
- spatie/laravel-ray: ^1.23
- vimeo/psalm: ^4.8
This package is auto-updated.
Last update: 2024-07-26 15:36:01 UTC
README
此包允许您使用 Model 使用 mangopay API。目标是使 API 使用更加自然和用户友好。在底层,它使用 mangopay 官方的 PHP SDK。
重要:目前此包仅提供带有 SEPA 托管的直接借记 PayIn。如果您想进行信用卡 PayIn,您可以使用 PHP mangopay SDK 的服务提供者。
只需在模型上添加一个 trait
class User extends Authenticatable { use HasMangopayUser; }
然后,您就有很多函数可以轻松地与 mangopay 一起工作。
$user->updateOrCreateMangopayUser(); $user->createMangopayBankAccount(); $user->createMangopayTransfer();
它还提供了一个服务:MangopayServiceProvider,因此您可以在需要时访问 mangopay SDK。
安装
通过 composer 安装包
composer require finller/laravel-mangopay
您必须使用以下命令发布和运行迁移
php artisan vendor:publish --provider="Finller\Mangopay\MangopayServiceProvider" --tag="migrations" php artisan migrate
您必须使用以下命令发布配置文件
php artisan vendor:publish --provider="Finller\Mangopay\MangopayServiceProvider" --tag="config"
这是发布配置文件的内容:必须指定一个临时文件夹以及 API 凭据。
return [ 'api' => [ 'id' => '', 'secret' => '', ], 'folder' => storage_path('mangopay'), 'defaultCurrency' => 'EUR', ];
使用方法
设置您的 Model
此包使用一个 Trait,该 Trait 提供了许多函数,最重要的是它将您的数据库与 mangopay 数据建立了连接。
您可以在任何 Model 上使用此 Trait,而不仅仅是 User。
use Finller\Mangopay\Traits\HasMangopayUser; class User extends Authenticatable { use HasMangopayUser; } // or class Company extends Model { use HasMangopayUser; }
默认情况下,mangopay 用户是 LEGAL。您可以定义您的用户是 NATURAL(个人)还是 LEGAL(公司或组织)
class Company extends Model { use HasMangopayUser; protected function mangopayUserIsLegal(){ return true; //or use some logic to determine the value }; }
如果您已经在数据库中存储了用户数据,并希望与 mangopay 同步,只需添加
use Finller\Mangopay\Traits\HasMangopayUser; class User extends Authenticatable { use HasMangopayUser; public function buildMangopayUserData(): array { return [ 'Name' => $this->company_name, 'Email' => $this->email, 'HeadquartersAddress' => [ 'AddressLine1' => $this->address->street, 'AddressLine2' => null, 'City' => $this->address->city, 'Region' => null, 'PostalCode' => $this->address->postal_code, 'Country' => $this->address->country_code, ], "LegalRepresentativeEmail" => $this->representative->email, "LegalRepresentativeBirthday" => $this->representative->birthdate->getTimestamp(), "LegalRepresentativeCountryOfResidence" => $this->representative->country_code, "LegalRepresentativeNationality" => $this->representative->nationality_code, "LegalRepresentativeFirstName" => $this->representative->first_name, "LegalRepresentativeLastName" => $this->representative->last_name, ]; } }
这些数据将在您调用 $user->createMangopayUser();
或 $user->updateMangopayUser();
时使用。
在示例中,所有 Mangopay 所需的个人数据都是从您的 Model 中获取的。请注意,此包在数据库中存储的唯一信息是 mangopay 用户 ID 和 mangopay 用户 KYC 级别。
创建和更新您的 mangopay 用户
然后您可以像这样创建和更新您的 mangopay 用户
$user->createMangopayUser(); //or $user->updateMangopayUser(); //or $user->updateOrCreateMangopayUser();
如果您不使用 buildMangopayUserData
方法,或者如果您想覆盖它,您可以传递一个数据数组:来自方法的数组 buildMangopayUserData
和作为变量传递的数组将被合并。
$user->createMangopayUser([ 'Name' => $this->company_name, 'Email' => 'put your email here', 'HeadquartersAddress' => [ 'AddressLine1' => $this->address->street, 'AddressLine2' => null, 'City' => $this->address->city, 'Region' => null, 'PostalCode' => $this->address->postal_code, 'Country' => $this->address->country_code, ], "LegalRepresentativeEmail" => $this->representative->email, "LegalRepresentativeBirthday" => $this->representative->birthdate->getTimestamp(), "LegalRepresentativeCountryOfResidence" => $this->representative->country_code, "LegalRepresentativeNationality" => $this->representative->nationality_code, "LegalRepresentativeFirstName" => $this->representative->first_name, "LegalRepresentativeLastName" => $this->representative->last_name, ]);
请注意,某些字段是创建 mangopay 用户时必须的(请参阅 mangopay 文档)。
管理您的 mangopay 钱包
$user->createMangopayWallet([ 'Description'=>'Main Wallet', 'Currency'=>'EUR', 'Tag'=>'a name or any info' ]); //get the list of the user's wallets $user->mangopayWallets();
添加银行账户和授权
$bankAccount = $company->createMangopayBankAccount([ 'IBAN' => 'an IBAN', 'Tag' => 'any name or tag', 'BIC' => 'BIC is optional', 'OwnerName' => 'the name', 'OwnerAddress' => [ 'AddressLine1' => 'street', 'AddressLine2' => null, 'City' => 'the city name', 'Region' => 'region is required for some countries', 'PostalCode' => ' a postal code', 'Country' => 'country code like FR, ...', ], ]); //retreive all users bank accounts $bankAccounts = $company->mangopayBankAccounts(); $mandate = $company->createMangopayMandate([ 'BankAccountId'=> "xxxx", 'Culture'=> 'FR', 'ReturnURL'=>'your-website.com' ]);
有许多函数可以管理一切,所以请不要犹豫去探索这个 trait(方法名称相当清晰)。
执行 PayIn 和 PayOut
重要:目前此包仅支持带有 SEPA 托管的直接借记 PayIn。如果您想进行信用卡 PayIn,您必须使用服务提供者以及 PHP SDK。
//SEPA PayIn $payIn = $user->createMangopayMandatePayIn([ 'DebitedFunds'=>[ 'Amount'=>1260,//12.60€ 'Currency'=>'EUR', ], 'Fees'=>[ 'Amount'=>0,//0€ 'Currency'=>'EUR', ], 'BankAccountId'=>123456, 'CreditedWalletId'=>123456, 'CreditedUserId'=>123456,//by default it's the owner of the wallet 'MandateId'=>123456, 'StatementDescriptor'=>'Your company name or a ref', ]); $payout = $user->createMangopayPayOut([ 'DebitedFunds'=>[ 'Amount'=>1260,//12.60€ 'Currency'=>'EUR', ], 'Fees'=>[ 'Amount'=>0,//0€ 'Currency'=>'EUR', ], 'BankAccountId'=>123456, 'DebitedWalletId'=>7891011, 'BankWireRef'=>'Your company name or a ref', ]);
从 Mangopay 用户 ID 获取 Laravel 用户和 Mangopay 用户
这种情况在处理 Mangopay 钩子时经常发生。您可以使用 MangopayPivot
Model。
use Finller\Mangopay\Models\MangopayPivot; $mangopayUserId = "123456"; $pivot = MangopayPivot::findByMangopayId($mangopayUserId); $laravelUser = $pivot->billable;//this will give you the laravel User or whatever Model you use $mangopayUser = $pivot->mangopayUser();//this will give you the Mangopay User Object
管理 Mangopay 钩子
这是一个如何在 Laravel 中处理 Mangopay 钩子的示例。
设置路由
Route::namespace('Mangopay')->group(function () { Route::get('hook/mangopay/payin', 'MangopayHookController@payin'); Route::get('hook/mangopay/payout', 'MangopayHookController@payout'); Route::get('hook/mangopay/kyc', [MangopayHookController::class, "kyc"]); });
定义控制器
use App\Events\Mangopay\PayInFailed; use App\Events\Mangopay\PayInSucceeded; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use MangoPay\EventType; class MangopayHookController extends Controller { /** * Handle mangopay hook * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function payin(Request $request) { $eventType = $request->input('EventType'); $Id = $request->input('RessourceId'); $date = $request->input('Timestamp'); if (!!$Id and !!$eventType) { switch ($eventType) { case EventType::PayinNormalSucceeded: event(new PayInSucceeded($Id, $date)); break; case EventType::PayinNormalFailed: event(new PayInFailed($Id, $date)); break; } } //you have to respond in less than 2 secondes with a 200 status code return response(); } }
定义事件
然后您就可以监听 mangopay 钩子了!
use Illuminate\Broadcasting\Channel; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Broadcasting\PresenceChannel; use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Queue\SerializesModels; use MangoPay\PayIn; class PayInFailed { use Dispatchable, InteractsWithSockets, SerializesModels; public $Id; public $date; public $type = PayIn::class; /** * Create a new event instance. * * @return void */ public function __construct($Id, $date) { $this->Id = $Id; $this->date = $date; } /** * Get the channels the event should broadcast on. * * @return \Illuminate\Broadcasting\Channel|array */ public function broadcastOn() { return new PrivateChannel('channel-name'); } }
部署到生产环境
默认情况下,MangoPay SDK 使用沙箱 API URL。如果您想进入生产环境,必须在配置文件中定义生产 API URL,如下所示:
return [ 'api' => [ 'id' => env('MANGOPAY_ID'), 'secret' => env('MANGOPAY_KEY'), 'url' => env('MANGOPAY_URL', "https://api.mangopay.com") //<-- this is the production base url ], 'folder' => storage_path('mangopay'), 'defaultCurrency' => 'EUR', ];
测试
composer test
更新日志
有关最近更改的更多信息,请参阅更新日志。
贡献
有关详细信息,请参阅贡献指南。
安全漏洞
有关如何报告安全漏洞,请查看我们的安全策略。
鸣谢
许可
MIT 许可证(MIT)。有关更多信息,请参阅许可文件。