roduankd/laravel-moyasar

这是我创建的包:laravel-moyasar

2.0.1 2022-02-23 19:49 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

这里应该放置你的描述。请限制为一到两段。考虑添加一个小示例。

安装

您可以通过composer安装此包

composer require roduankd/laravel-moyasar

然后您需要在.env文件中定义以下变量

MOYASAR_API_PUBLISHABLE_KEY="your api key goes here"
MOYASAR_CURRENCY=USD

可选,您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="laravel-moyasar-config"

这是已发布配置文件的内容

return [
    // Required
    // Specify where to render the form
    // Can be a valid CSS selector and a reference to a DOM element
    'element' => '.mysr-form',

    // Required
    // Currency of the payment transaction
    'currency' => env('MOYASAR_CURRENCY', 'USD'),

    // Required
    // A small description of the current payment process
    'description' => 'default description',

    // Required
    'publishable_key' => env('MOYASAR_API_PUBLISHABLE_KEY', null),

    // Required
    // This URL is used to redirect the user when payment process has completed
    // Payment can be either a success or a failure, which you need to verify on you system (We will show this in a couple of lines)
    'callback_url' => 'moyasar.callback',

    // Highly recommended
    // This URL is use by moyasar to send to save the payment ID before redirecting the user to 3-D Secure
    // this is the default on complete route name
    'on_complete_url' => 'moyasar.complete',

    // Optional
    // Required payments methods
    // Default: ['creditcard', 'applepay', 'stcpay']
    'methods' => ['creditcard', 'applepay', 'stcpay'],
];

然后,您需要定义“moyasar.callback”路由。这是用户在完成支付流程后将被重定向到的路由。

Route::get('memberships/{membership}/payment/thanks', [MembershipController::class, 'thanks'])->name('moyasar.callback');

可选,您可以覆盖默认的支付完成路由。这是在用户被重定向到“moyasar.callback”路由之前,由moyasar服务器访问的路由。

Route::post('memberships/{membership}/payment/completed', [RoduanKD\LaravelMoyasar\Controllers\PaymentController::class, 'store'])->name('moyasar.complete');

注意:您可以随意定义路由。

使用监听器

当您覆盖“moyasar.complete”路由时,您可能需要更改模型中的某些内容,每当支付流程完成时。您可以定义监听器来处理TransactionCreated事件,该事件包含payment_id和所有“moyasar.complete”路由参数。

php artisan make:listener MarkMembershipAsPaid --event=\RoduanKD\LaravelMoyasar\Events\TransactionCreated

在您的监听器中,您可以按需更新模型。

/**
 * Handle the event.
 *
 * @param  \RoduanKD\LaravelMoyasar\Events\TransactionCreated  $event
 * @return void
 */
public function handle(TransactionCreated $event)
{
    $membership = Membership::find($event->parameters['membership']);
    $membership->paid_at = now();
    $membership->update();
}

并且您需要更新初始化组件的路由

<x-moyasar-init amount="100" :on-complete="route('moyasar.complete', $membership)" />

可选,您可以使用以下命令发布视图

php artisan vendor:publish --tag="laravel-moyasar-views"

用法

将moysar样式添加到页面的部分。

@include('moyasar::head')

然后包含初始化组件,并提供您需要的所有选项

<x-moyasar-init amount="100" />

测试

composer test

变更日志

请参阅变更日志以获取有关最近更改的更多信息。

贡献

请参阅贡献指南以获取详细信息。

安全漏洞

请审查我们的安全策略,了解如何报告安全漏洞。

鸣谢

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件