承接 beebmx/laravel-pay 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

beebmx/laravel-pay

Composer 安装命令:

composer require beebmx/laravel-pay

包简介

Create payments with Laravel

README 文档

README

Latest Stable Version License

Payments for Laravel is inspired by the official Laravel Cashier. The approach is slightly different to Cashier and the main behavior is the payment transactions.

Installation

You can install the package via composer:

composer require beebmx/laravel-pay

You can publish and run the migrations with:

php artisan vendor:publish --provider="Beebmx\LaravelPay\PayServiceProvider" --tag="pay-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --provider="Beebmx\LaravelPay\PayServiceProvider" --tag="pay-config"

Configuration

Before use Payments for Laravel you need to add the Payable trait to your user model, the default location of this is in App\Models\Users:

use Beebmx\LaravelPay\Payable;

class User extends Authenticatable {

    use Payable;
}

Custom models

You can use your own models to fit your needs, just set in the App\Providers\AppServiceProvider and inform Payments for Laravel in the boot method:

use App\Models\Address;
use App\Models\Pay\User;
use App\Models\Transaction;
use App\Models\TransactionItem;
use Beebmx\LaravelPay\Pay;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Pay::useCustomerModel(User::class);
    Pay::useAddressModel(Address::class);
    Pay::useTransactionModel(Transaction::class);
    Pay::useTransactionItemModel(TransactionItem::class);
}

Just remember, if you change this for your own models, make sure to extend these models with the Payments for Laravel:

use Beebmx\LaravelPay\Address as PayAddress;

class Address extends PayAddress
{
    //
}

Drivers

Sandbox

This works as a playground, with full capabilities as a testing or production driver.

Stripe

To work with the stripe driver you need to set as default driver and configurate your testing or production keys:

<?php

return [
    'default' => 'stripe',
    
    'drivers' => [
        'stripe' => [
            'driver' => Beebmx\LaravelPay\Drivers\StripeDriver::class,
            'secret' => env('STRIPE_SECRET_KEY', null),
            'public' => env('STRIPE_PUBLIC_KEY', null),
            'webhooks' => [
                'handler' => Beebmx\LaravelPay\Http\Webhooks\StripeHandler::class,
                'events' => [
                    'customer.deleted',
                    'customer.updated',
                    'invoice.payment_action_required',
                    'payment_intent.canceled',
                    'payment_intent.payment_failed',
                    'payment_intent.requires_action',
                    'payment_intent.succeeded',
                ],
            ],
        ],
    ]
];

Conekta

To work with the conekta driver you need to set as default driver and configurate your testing or production keys:

<?php

return [
    'default' => 'conekta',
    
    'drivers' => [
        'conekta' => [
            'driver' => Beebmx\LaravelPay\Drivers\ConektaDriver::class,
            'secret' => env('CONEKTA_SECRET_KEY', null),
            'public' => env('CONEKTA_PUBLIC_KEY', null),
            'webhooks' => [
                'handler' => Beebmx\LaravelPay\Http\Webhooks\ConektaHandler::class,
            ],
        ],
    ]
];

Customer

Create customer

A user can be a customer (this is not a requirement) and will create a customer in the correspond driver. To create a customer just use the method createCustomerWithDriver:

$customer = $user->createCustomerWithDriver();

Basic information

When a user becomes to customer, the columns used to create it, comes from the model, but you can customize this with:

public function driverName()
{
    return "{$this->your_own_colunm_name} {$this->your_own_colunm_lastname}";
}

public function driverEmail()
{
    return $this->your_own_colunm_email;
}

public function driverPhone()
{
    return $this->your_own_colunm_phone;
}

Creating a user can be useful for recurring payments or to avoid asking for payment methods all the time.

Payment Methods

To add a payment method, first you need to have a customer on the driver platform. After you have your customer just add the right token or payment_method:

$paymentMethod = $user->addPaymentMethod('pm_card_visa');

Shipping

If your payment have a shipping, just add the proper address before perform the payment:

$address = $user->addresses->first();
$user->shipping($address);

Remember that this package have an address model build in out of the box.

Payments

To perform a payment is as easy as:

$user->charge([
    'name' => 'Product',
    'price' => 500,
]);

If you have more than one product or service:

$user->charge([[
    'name' => 'Product 01',
    'price' => 100
], [
    'name' => 'Product 02',
    'price' => 200
]]);

If you have a discount to be applied you can set it:

$user
    ->discount(200)
    ->charge([[
        'name' => 'Product 01',
        'price' => 100,
        'quantity' => 2
    ], [
        'name' => 'Product 02',
        'price' => 200,
        'quantity' => 1,
    ]]);

Or you can set it with more information like:

$user
    ->discount([
        'amount' => 200, 
        'code' => 'coupon-code',
    ])
    ->charge([[
        'name' => 'Product 01',
        'price' => 100,
        'quantity' => 2
    ], [
        'name' => 'Product 02',
        'price' => 200,
        'quantity' => 1,
    ]]);

It's important that the user has at least one payment method.

If you have a shipping address:

$address = $user->addresses->first();

$user
    ->shipping($address)
    ->charge([
        'name' => 'Testing product',
        'price' => 500,
    ]);

If you don't need to create a payment method you can create a charge with a one time payment token:

$user
    ->token('tok_visa4242')
    ->charge([
        'name' => 'Testing product',
        'price' => 500,
    ]);

If you are using the token option, the createCustomerWithDriver is optional and not required.

If you need the payment transaction, you can retrive it with the getTransaction() method:

$payment = $user->charge([
    'name' => 'Product',
    'price' => 500,
]);

$payment->getTransaction()

Payment with model

If you have a model to manage your products or services, you can include it in the charge method using your own model or an id.

$payment = $user->charge([
    'name' => 'Product',
    'price' => 500,
    'model' => \App\Models\Product::first(),
]);
$payment = $user->charge([
    'name' => 'Product',
    'price' => 500,
    'model' => 1,
]);

Webhooks

By default Payments for Laravel provides a handy management of webhooks.

First in your App\Http\Middleware\VerifyCsrfToken file, you need to allow the path of the WebhookController:

protected $except = [
    'pay/webhooks',
];

The default path is pay/webhooks but you can change it in the config file, updating the pay.path and pay.webhooks.

You can create manually your endpoint in your driver provider site or with the command:

php artisan pay:webhook

The driver sandbox doesn't allow the creation of webhooks.

Handlers

The WebhookController always react to the events emitted from the driver provider and passes to the correct handler. By default, every driver has their own handler defined in the config/pay.php file:

'drivers' => [
    'stripe' => [
        'webhooks' => [
            'handler' => Beebmx\LaravelPay\Http\Webhooks\StripeHandler::class,
            'events' => [
                'customer.deleted',
                'customer.updated',
                'invoice.payment_action_required',
                'payment_intent.canceled',
                'payment_intent.payment_failed',
                'payment_intent.requires_action',
                'payment_intent.succeeded',
            ],
        ],
    ],

    'conekta' => [
        'webhooks' => [
            'handler' => Beebmx\LaravelPay\Http\Webhooks\ConektaHandler::class,
        ],
    ],
],

You can change the handler with your own implementation, just make your to extends from the right handler.

<?php

use Beebmx\LaravelPay\Http\Webhooks\StripeHandler as PayStripeHandler;

class StripeHandler extends PayStripeHandler
{
    // ...
}

The methods names should correspond to the convention, first all methods should be prefixed with handle and the "camel case" name of the webhook you wish to handle. For example, if you wish to handle the payment_intent.succeeded webhook, you should add a handlePaymentIntentSucceeded method to the controller:

<?php

use Beebmx\LaravelPay\Http\Webhooks\StripeHandler as PayStripeHandler;

class StripeHandler extends PayStripeHandler
{
    public function handlePaymentIntentSucceeded($payload)
    {
        // ...
    }
}

On each handled method an array with $payload correspondence is always received.

Webhook events

On each webhook call, the following events could be dispatched:

  • Beebmx\LaravelPay\Events\WebhookReceived
  • Beebmx\LaravelPay\Events\WebhookHandled

You can create your own implementation of the payload:

<?php

namespace App\Listeners;

use Beebmx\LaravelPay\Events\WebhookReceived;

class StripeEventListener
{
    public function handle(WebhookReceived $event)
    {
        // $event->payload['type']
    }
}

To listen this implementation is required to register in your App\Providers\EventServiceProvider:

<?php

namespace App\Providers;

use App\Listeners\StripeEventListener;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Beebmx\LaravelPay\Events\WebhookReceived;

class EventServiceProvider extends ServiceProvider
{
    protected $listen = [
        WebhookReceived::class => [
            StripeEventListener::class,
        ],
    ];
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

beebmx/laravel-pay 适用场景与选型建议

beebmx/laravel-pay 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 43 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 12 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「payment」 「payments」 「stripe」 「laravel」 「conekta」 「laravel-payment」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 beebmx/laravel-pay 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 beebmx/laravel-pay 我们能提供哪些服务?
定制开发 / 二次开发

基于 beebmx/laravel-pay 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 43
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-12-30