定制 orkhanahmadov/yandex-checkout 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

orkhanahmadov/yandex-checkout

Composer 安装命令:

composer require orkhanahmadov/yandex-checkout

包简介

Easy and complete YooKassa (previously Yandex Checkout) integration for Laravel

README 文档

README

Latest Stable Version Latest Unstable Version Total Downloads GitHub license

Build Status Test Coverage Maintainability Quality Score StyleCI

Easy and complete YooKassa (previously Yandex Checkout) integration for Laravel

Todo

  • Test coverage

Table of Contents

  1. Requirements
  2. Installation
  3. Usage
  4. Models
  5. Commands
  6. Events
  7. Configuration

Requirements

  • PHP 7.3 or above
  • Laravel 6 or above

Installation

You can install the package via composer:

composer require orkhanahmadov/yandex-checkout

Run this command to publish required migration file:

php artisan vendor:publish --provider="Orkhanahmadov\YandexCheckout\YandexCheckoutServiceProvider" --tag=migrations

Usage

First, set Yandex Checkout shop ID and secret key in .env file. You can get these from YooMoney merchant page.

YANDEX_CHECKOUT_SHOP_ID=
YANDEX_CHECKOUT_SECRET_KEY=

To use Yandex Checkout service you need instance of Orkhanahmadov\YandexCheckout\YandexCheckoutService. You can instantiate this class using Laravel's service container, for example by injecting to your controller

use Orkhanahmadov\YandexCheckout\YandexCheckoutService;

class MyController
{
    public function index(YandexCheckoutService $yandexCheckout)
    {
        //
    }
}

Or you can use Laravel's service resolver to create instance of the class:

use Orkhanahmadov\YandexCheckout\YandexCheckoutService;

class MyClass
{
    public function doSomething()
    {
        $yandexCheckout = app(YandexCheckoutService::class);
        //
    }
}

Available methods:

createPayment()

Creates new payment based on passed credentials and accepts 2 arguments:

  • Model - Eloquent model payment is associated
  • Payment request - Payment request that contains amount, currency, etc information
$product = Product::first();
$yandexCheckout = app(YandexCheckoutService::class);
$paymentRequest = CreatePaymentRequest::builder()->build([
    'amount' => [
        'value' => 49.99,
        'currency' => 'RUB',
    ],
    'confirmation' => [
        'type' => 'redirect',
        'return_url' => 'https://example.com',
    ],
    'capture' => true,
    'description' => 'Payment for product: ' . $product->id,
]);
$yandexCheckout->createPayment($product, $paymentRequest);

Method returns created instance of Orkhanahmadov\YandexCheckout\Models\YandexCheckout model.

You should use $confirmation_url property to get unique payment URL and redirect user to this URL to start payment.

paymentInfo()

Gets information on previously created payment. Accepts single argument:

  • Payment - This is Yandex Checkout's payment id as a string, or instance of previously created Orkhanahmadov\YandexCheckout\Models\YandexCheckout model.
$product = Product::first();
$yandexCheckout = app(YandexCheckoutService::class);
$payment = $yandexCheckout->createPayment($product, ...);

$paymentInfo = $yandexCheckout->paymentInfo($payment);
// or
$paymentInfo = $yandexCheckout->paymentInfo('1234-ABCD-5678');

Method returns updated instance of Orkhanahmadov\YandexCheckout\Models\YandexCheckout model with Yandex Checkout's response.

Models

Package ships with Orkhanahmadov\YandexCheckout\Models\YandexCheckout Eloquent model. Model stores following information for each payment:

  • payment_id - string, unique payment key provided by Yandex Checkout
  • status - string, payment status code
  • response - array, serialized checkout object

Besides usual Eloquent functionality this model also has specific accessors, scopes and relationship abilities which you can utilize.

Accessors

  • succeeded - Returns true if payment marked as "succeeded", false otherwise
  • paid - Returns true if checkout is paid, false otherwise
  • confirmation_url - Returns "confirmation URL" which should be used to start payment
  • cancellation_reason - Returns payment's cancellation/fail reason. Returns null when payment is successful or not started yet

Scopes

  • succeeded() - Filters "succeeded" payments only
  • pending() - Filters "pending" payments only. Pending payments are the payments that has status other than "succeeded" or "canceled".

Relationship

You can make any existing Eloquent model "payable" and attach Yandex Checkouts to it. Use Orkhanahmadov\YandexCheckout\Traits\HandlesYandexCheckout trait in your existing model to establish direct model relationship.

use Illuminate\Database\Eloquent\Model;
use Orkhanahmadov\YandexCheckout\Traits\HandlesYandexCheckout;

class Product extends Model
{
    use HandlesYandexCheckout;
}

Now Product model has direct relationship with Yandex Checkouts. By using HandlesYandexCheckout your model also gets access to payment related relationships and payment methods.

createPayment()

$product = Product::first();

$paymentRequest = CreatePaymentRequest::builder()->build([
    'amount' => [
      'value' => 49.99,
      'currency' => 'RUB',
    ],
    'confirmation' => [
      'type' => 'redirect',
      'return_url' => 'https://example.com',
    ],
    'capture' => true,
    'description' => 'Payment for product: ' . $product->id,
]);
$product->createPayment($paymentRequest);

yandexCheckouts()

Eloquent relationship method. Return all related Yandex Checkouts.

$product = Product::first();
$product->yandexCheckouts; // returns collection of related Yandex Checkouts
$product->yandexCheckouts()->where('payment_id', '123-ABC-456'); // use it as regular Eloquent relationship
$product->yandexCheckouts()->pending(); // use scopes on YandexCheckout model

Commands

Package ships with artisan command for checking payment results.

php artisan yandex-checkout:check

Executing above command will loop through all "pending" checkouts and update their models.

Command also accepts payment ID as an argument to check single checkout result.

php artisan yandex-checkout:check 1234-ABCD-5678

You can set up a Cron job schedule to frequently check all "pending" checkout.

protected function schedule(Schedule $schedule)
{
    $schedule->command('yandex-checkout:check')->everyMinute();
}

Events

Package ships with Laravel events which gets fired on specific conditions.

Available event classes:

  • Orkhanahmadov\YandexCheckout\Events\CheckoutCreated - gets fired when new checkout is created
  • Orkhanahmadov\YandexCheckout\Events\CheckoutSucceeded - gets fired when payment status changes to "succeeded"
  • Orkhanahmadov\YandexCheckout\Events\CheckoutCanceled - gets fired when payment status changes to "canceled"
  • Orkhanahmadov\YandexCheckout\Events\CheckoutChecked - gets fired when payment information is checked

Each event receives instance of Orkhanahmadov\YandexCheckout\Models\YandexCheckout Eloquent model as public $yandexCheckout property.

You can set up event listeners to trigger when specific payment event gets fired.

protected $listen = [
    'Orkhanahmadov\YandexCheckout\Events\CheckoutSucceeded' => [
        'App\Listeners\DispatchOrder',
        'App\Listeners\SendInvoice',
    ],
];

Configuration

Run this command to publish package config file:

php artisan vendor:publish --provider="Orkhanahmadov\YandexCheckout\YandexCheckoutServiceProvider" --tag=config

Config file contains following settings:

  • shop_id - Defines Yandex Checkout's "shop ID", defaults to .env variable
  • secret_key - Defines Yandex Checkout's "secret key", defaults to .env variable
  • table_name - Defines name for Yandex Checkout payments database table. Default: "yandex_checkouts"
  • events - Payment events related settings
    • created - "Checkout created" event class. By default uses Orkhanahmadov\YandexCheckout\Events\CheckoutCreated class
    • succeeded - "Checkout succeeded" event class. By default uses Orkhanahmadov\YandexCheckout\Events\CheckoutSucceeded class
    • canceled - "Checkout canceled" event class. By default uses Orkhanahmadov\YandexCheckout\Events\CheckoutCanceled class
    • checked - "Checkout checked" event class. By default uses Orkhanahmadov\YandexCheckout\Events\CheckoutChecked class

If you want to use your own event class for specific payment event you can replace class namespace with your class namespace. Each checkout event receives instance of Orkhanahmadov\YandexCheckout\Models\YandexCheckout Eloquent model. Because of this, make sure you add payment model as dependency to your event class constructor signature or you can extend Orkhanahmadov\YandexCheckout\Events\CheckoutEvent class which already has payment model as dependency.

Setting specific payment event to null disables that event.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email orkhan@fastmail.com instead of using the issue tracker.

Credits

License

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

orkhanahmadov/yandex-checkout 适用场景与选型建议

orkhanahmadov/yandex-checkout 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.32k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2020 年 11 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 orkhanahmadov/yandex-checkout 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 5.32k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-11-11