首页 > 解决方案 > 使用 Laravel Cashier 和 Jenssegers MongoDB 在 Stripe 中更新订阅后,订阅不会在数据库中更新

问题描述

我无法在 MongoDB 数据库中更新我的新订阅。我使用 Laravel Cashier、Stripe 和 Jenssegers MongoDB。

在条带仪表板中,用户已成功添加为客户和订阅者。

这是错误: [23:24:17] LOG.error: 在 null {"userId":"4ec1b45d36623t2269477d0... 上调用成员函数 prepare()

这是错误所在的位置:

                return true;
        }

        $statement = $this->getPdo()->prepare($query);

        $this->bindValues($statement, $this->prepareBindings($bindings));

这是我的控制器:

namespace App\Http\Controllers;

use App\Plan;
use App\User;
use Exception;
use Illuminate\Http\Request;





class CheckoutController extends Controller
{
 /**
 * The collection name
 *
 * @var array
 */

public function checkout($plan_id)
{
    $plan = Plan::findOrFail($plan_id);
    $intent = auth()->user()->createSetupIntent();
    return view('billing.checkout', compact('plan', 'intent'));
}

public function process(Request $request)
{
    $plan = Plan::findOrFail($request->input('billing_plan_id'));
    try {
        auth()->user()->newSubscription($plan->name, $plan->stripe_plan_id)- 
>create($request->input('payment-method'));
        return redirect()->route('billing')->withMessage('Subscribed Successfully');
    } catch (Exception $e) {
        return redirect()->back()->withError($e->getMessage());
    }
}

这是我的用户模型:

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Auth\User as Authenticatable;
use Laravel\Cashier\Billable;
use Illuminate\Foundation\Auth;

class User extends Authenticatable
{

use  Billable, Notifiable;


protected $connection = 'mongodb';


/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
 protected $fillable = [
    'name', 'email', 'username', 'password', 'phone', 'last_login_at', 
'last_login_ip',
];
/**
 * The collection name
 *
 * @var array
 */
 protected $table = 'users';



 /**
  * The attributes that are mass assignable.
  *
  * @var array
  */
   protected $dates = ['deleted_at'];
 /**
  * The attributes that should be hidden for arrays.
  *
  * @var array
  */
 protected $hidden = [
    'password', 'remember_token',
];

/**
 * The attributes that should be cast to native types.
 *
 * @var array
 */
protected $casts = [
    'email_verified_at' => 'datetime',
];

}

这是我的计划模型: 命名空间应用程序;

use Jenssegers\Mongodb\Eloquent\Model;


/**
 * @method static findOrFail($plan_id)
 */
class Plan extends Model
{

 protected $fillable = [
     'name',
    'price',
    'stripe_plan_id'
];

}

这是我的订阅迁移:

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;
use Jenssegers\Mongodb\Schema\Blueprint;





class CreateSubscriptionsTable extends Migration
{
/**
 * The name of the database connection to use.
 *
 * @var string
 */
protected $connection = 'mongodb';



public function up()
{
    Schema::create('subscriptions', function (Blueprint $collection) {
        $collection->bigIncrements('id');
        $collection->unsignedBigInteger('userid');
        $collection->string('name');
        $collection->string('stripe_id');
        $collection->string('stripe_status');
        $collection->string('stripe_plan')->nullable()->change();
        $collection->integer('quantity')->nullable()->change();
        $collection->timestamp('trial_ends_at')->nullable();
        $collection->timestamp('ends_at')->nullable();
        $collection->timestamps();
        $collection->index(['user_id', 'stripe_status']);
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('subscriptions');
}
}

请帮我找出这个问题的根源以及如何解决它。

标签: phplaravellaravel-cashierjenssegers-mongodb

解决方案


部分解决方案: 虽然我在下面遇到同样的错误

"Call to a member function prepare() on null {"userId":"4ec1b45d36623t2269477d0...".

为了让订阅在数据库中更新,我进入了 Cashier 的 Subscription.php 文件并替换了

use Illuminate\Database\Eloquent\Model; 

use Jenssegers\Mongodb\Eloquent\Model;

这将修复数据库更新问题,但 Connection.php 文件中仍然存在一些奇怪的东西导致错误。

 $statement = $this->getPdo()->prepare($query);

推荐阅读