<commit_before><?php

declare(strict_types=1);

/*
 * +----------------------------------------------------------------------+
 * |                          ThinkSNS Plus                               |
 * +----------------------------------------------------------------------+
 * | Copyright (c) 2017 <NAME> Technology Co., Ltd.     |
 * +----------------------------------------------------------------------+
 * | This source file is subject to version 2.0 of the Apache license,    |
 * | that is bundled with this package in the file LICENSE, and is        |
 * | available through the world-wide-web at the following url:           |
 * | http://www.apache.org/licenses/LICENSE-2.0.html                      |
 * +----------------------------------------------------------------------+
 * | Author: <NAME> <<EMAIL>>                          |
 * | Homepage: www.thinksns.com                                           |
 * +----------------------------------------------------------------------+
 */

namespace Zhiyi\Plus\Packages\Wallet\Types;

use DB;
use Illuminate\Http\Request;
use Zhiyi\Plus\Packages\Wallet\Order;
use Zhiyi\Plus\Models\User as UserModel;
use Zhiyi\Plus\Repository\WalletPingPlusPlus;
use Zhiyi\Plus\Models\WalletOrder as WalletOrderModel;
use Zhiyi\Plus\Packages\Wallet\TargetTypes\RechargeTarget;
use Zhiyi\Plus\Services\Wallet\Charge as WalletChargeService;

class RechargeType extends Type
{
    /**
     * 创建充值订单.
     *
     * @param $owner
     * @param int $amount
     * @param string $type
     * @param array $extra
     * @return mixed
     * @author BS <<EMAIL>>
     */
    public function create($owner, int $amount, string $type, array $extra = [])
    {
        $owner = $this->checkUserId($owner);
        $order = $this->createOrder($owner, $amount);

        if (app(WalletChargeService::class)->checkRechargeArgs($type, $extra)) {
            $transaction = function () use ($order, $extra, $type) {
                $pingppCharge = app(WalletChargeService::class)->newCreate($order->getOrderModel(), $type, $extra);
                $order->getOrderModel()->target_id = $pingppCharge->id;
                $order->save();

                return [
                    'pingpp_order' => $pingppCharge,
                    'order' => $order->getOrderModel(),
                ];
            };

            return DB::transaction($transaction);
        }

        return false;
    }

    /**
     * 主动取回凭据.
     *
     * @return boolen
     * @author BS <<EMAIL>>
     */
    public function retrieve(WalletOrderModel $walletOrder): bool
    {
        $pingppCharge = app(WalletChargeService::class)->query($walletOrder->target_id);

        if ($pingppCharge['paid'] === true) {
            return $this->complete($walletOrder);
        }

        return false;
    }

    /**
     * 异步回调通知.
     *
     * @param Request $request
     * @return boolen
     * @author BS <<EMAIL>>
     */
    public function webhook(Request $request): bool
    {
        if ($this->verifyWebHook($request)) {
            $pingppCharge = $request->json('data.object');
            $walletOrder = WalletChargeModel::find(app(WalletChargeService::class)->unformatChargeId($pingppCharge['order_no']));

            if ($walletOrder || $walletOrder->status === 1) {
                return false;
            }

            return $this->complete($walletOrder);
        }

        return false;
    }

    /**
     * 完成订单.
     *
     * @return boolen
     * @author BS <<EMAIL>>
     */
    public function complete(WalletOrderModel $order): bool
    {
        $order = new Order($walletOrder);

        return $order->autoComplete();
    }

    /**
     * Check user.
     *
     * @param int|UserModel $user
     * @return int
     * @author BS <<EMAIL>>
     */
    protected function checkUserId($user): int
    {
        if ($user instanceof UserModel) {<commit_msg>fix: 修复函数接收参数名错误<commit_after><?php

declare(strict_types=1);

/*
 * +----------------------------------------------------------------------+
 * |                          ThinkSNS Plus                               |
 * +----------------------------------------------------------------------+
 * | Copyright (c) 2017 <NAME>ang Technology Co., Ltd.     |
 * +----------------------------------------------------------------------+
 * | This source file is subject to version 2.0 of the Apache license,    |
 * | that is bundled with this package in the file LICENSE, and is        |
 * | available through the world-wide-web at the following url:           |
 * | http://www.apache.org/licenses/LICENSE-2.0.html                      |
 * +----------------------------------------------------------------------+
 * | Author: <NAME> <<EMAIL>>                          |
 * | Homepage: www.thinksns.com                                           |
 * +----------------------------------------------------------------------+
 */

namespace Zhiyi\Plus\Packages\Wallet\Types;

use DB;
use Illuminate\Http\Request;
use Zhiyi\Plus\Packages\Wallet\Order;
use Zhiyi\Plus\Models\User as UserModel;
use Zhiyi\Plus\Repository\WalletPingPlusPlus;
use Zhiyi\Plus\Models\WalletOrder as WalletOrderModel;
use Zhiyi\Plus\Packages\Wallet\TargetTypes\RechargeTarget;
use Zhiyi\Plus\Services\Wallet\Charge as WalletChargeService;

class RechargeType extends Type
{
    /**
     * 创建充值订单.
     *
     * @param $owner
     * @param int $amount
     * @param string $type
     * @param array $extra
     * @return mixed
     * @author BS <<EMAIL>>
     */
    public function create($owner, int $amount, string $type, array $extra = [])
    {
        $owner = $this->checkUserId($owner);
        $order = $this->createOrder($owner, $amount);

        if (app(WalletChargeService::class)->checkRechargeArgs($type, $extra)) {
            $transaction = function () use ($order, $extra, $type) {
                $pingppCharge = app(WalletChargeService::class)->newCreate($order->getOrderModel(), $type, $extra);
                $order->getOrderModel()->target_id = $pingppCharge->id;
                $order->save();

                return [
                    'pingpp_order' => $pingppCharge,
                    'order' => $order->getOrderModel(),
                ];
            };

            return DB::transaction($transaction);
        }

        return false;
    }

    /**
     * 主动取回凭据.
     *
     * @return boolen
     * @author BS <<EMAIL>>
     */
    public function retrieve(WalletOrderModel $walletOrder): bool
    {
        $pingppCharge = app(WalletChargeService::class)->query($walletOrder->target_id);

        if ($pingppCharge['paid'] === true) {
            return $this->complete($walletOrder);
        }

        return false;
    }

    /**
     * 异步回调通知.
     *
     * @param Request $request
     * @return boolen
     * @author BS <<EMAIL>>
     */
    public function webhook(Request $request): bool
    {
        if ($this->verifyWebHook($request)) {
            $pingppCharge = $request->json('data.object');
            $walletOrder = WalletChargeModel::find(app(WalletChargeService::class)->unformatChargeId($pingppCharge['order_no']));

            if ($walletOrder || $walletOrder->status === 1) {
                return false;
            }

            return $this->complete($walletOrder);
        }

        return false;
    }

    /**
     * 完成订单.
     *
     * @return boolen
     * @author BS <<EMAIL>>
     */
    public function complete(WalletOrderModel $walletOrder): bool
    {
        $order = new Order($walletOrder);

        return $order->autoComplete();
    }

    /**
     * Check user.
     *
     * @param int|UserModel $user
     * @return int
     * @author BS <<EMAIL>>
     */
    protected function checkUserId($user): int
    {
        if ($user instanceof UserModel) {
{'fraction_non_alphanumeric': 0.21412096360840593, 'fraction_numerical': 0.0025627883136852894, 'mean_word_length': 1.5096463022508038, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3736471', 'n_tokens_mistral': 2180, 'n_tokens_neox': 2172, 'n_words': 435}