PHP-找不到条带用法记录类

时间:2018-10-15 23:51:31

标签: php laravel composer-php stripe-payments

我正在尝试将使用记录添加到我的订阅中。使用条带创建使用记录端点(https://stripe.com/docs/api#usage_record_create)。

运行我的函数时出现错误,返回说Class 'Stripe\UsageRecord' not found in file我还没有定义名称空间,因为我曾经使用\Stripe\直接引用它使用composer来访问它。我尝试过composer update,但似乎并没有解决问题。我猜想它UsageRecord.php中缺少composer install文件,但是我不知道在哪里将文件副本添加到stripe包中

  public function stripeUsageRecord()
  {
    $authUser = auth()->user();
    $business = $authUser['business_id'];
    $user_amount = Transactions::select("user_id")
      ->where("business_id", "=", $business)
      ->groupBy("user_id")->count();
    $current_time = Carbon::now()->toDateTimeString();
    \Stripe\Stripe::setApiKey(env("STRIPE_SECRET"));

    \Stripe\UsageRecord::create(array(
      "quantity" => $user_amount,
      "timestamp" => $current_time,
      "subscription_item" => 'sub_DnAKVwNY2Sc4zf',
      "action" => 'set'
    ));
  }

2 个答案:

答案 0 :(得分:1)

很可能您使用的库版本太旧。 Stripe\UsageRecord在6.6.0版本中是introduced,所以我建议将库更新到最新版本:

composer require "stripe/stripe-php:^6.19"

您绝对不应该修改vendor目录的内容并从不同版本的库中复制和粘贴类。

答案 1 :(得分:-1)

好像我缺少条纹包的一部分。

我在线找到了UsageRecord.php的副本,并在路径vendor\stripe\stripe-php\lib\UsageRecord.php中创建了文件

然后,我添加了我在网上找到的代码的内容,并将其添加到了文件中,并且可以正常工作。内容发布在下面:

<?php

namespace Stripe;

/**
* Class UsageRecord
*
* @package Stripe
*
* @property string $id
* @property string $object
* @property bool $livemode
* @property int $quantity
* @property string $subscription_item
* @property int $timestamp
*/
class UsageRecord extends ApiResource
{

const OBJECT_NAME = "usage_record";

/**
* @param array|null $params
* @param array|string|null $options
*
* @return \Stripe\ApiResource The created resource.
*/
public static function create($params = null, $options = null)
{
  self::_validateParams($params);
  if (!array_key_exists('subscription_item', $params)) {
      throw new Error\InvalidRequest("Missing subscription_item param in request", null);
  }
  $subscription_item = $params['subscription_item'];
  $url = "/v1/subscription_items/$subscription_item/usage_records";
  $request_params = $params;
  unset($request_params['subscription_item']);

  list($response, $opts) = static::_staticRequest('post', $url, $request_params, $options);
  $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  $obj->setLastResponse($response);
  return $obj;
}
}