Laravel Cashier - สมัครสมาชิกตลอดชีพ

ฉันกำลังพยายามสมัครสมาชิกรายเดือน รายปี และตลอดชีพ ซึ่งการสมัครสมาชิกรายเดือนและรายปีทำงานได้ดี ฉันจะสมัครสมาชิกตลอดชีพ/ตลอดไปได้อย่างไร? เมื่อใดก็ตามที่ฉันส่งชื่อแผนและรหัสแผนไปที่ $user->newSubscription() ฉันได้รับข้อผิดพลาด:

You passed a non-recurring price but this field only accepts recurring prices.

ด้านล่างคือรหัสของฉันสำหรับการสมัครสมาชิก:

$paymentMethod = $user->defaultPaymentMethod();

        if ($period == 'yearly') {
            $selectedPlan = $plan->plan_year;
        } elseif($period=='monthly') {
            $selectedPlan = $plan->plan_month;
        }
        else{
            $selectedPlan = $plan->plan_lifetime;
        }   

        $subscription = $user->newSubscription($plan->name, $selectedPlan);

person Naveed Ali    schedule 23.12.2020    source แหล่งที่มา
comment
อาจจะไม่สมัครสมาชิก เนื่องจากคุณสามารถชำระเงินเพียงครั้งเดียวได้ตลอดชีวิต   -  person HTMHell    schedule 23.12.2020
comment
แต่ฉันจะตรวจสอบได้อย่างไรว่าผู้ใช้สมัครสมาชิกหรือชำระเงินแล้ว   -  person Naveed Ali    schedule 23.12.2020
comment
ตามฐานข้อมูลของคุณ ไม่ว่าในกรณีใด คุณไม่ควรพึ่งพาโซลูชันการประมวลผลเดียว (Stripe) และโค้ดของโซลูชันเหล่านั้น จะเกิดอะไรขึ้นหากคุณต้องเปลี่ยนหรือเพิ่มวิธีการชำระเงินอื่น   -  person HTMHell    schedule 23.12.2020


คำตอบ (1)


สมมติว่าคุณใช้ ราคา แทนแผน คุณควรสร้างใบแจ้งหนี้แทนการสมัครรับข้อมูล : :

if ($period == 'yearly') {
    $subscription = $user->newSubscription($plan->name, $plan->plan_year);
} elseif($period=='monthly') {
    $subscription = $user->newSubscription($plan->name, $plan->plan_month);
} else {
    // Create an invoice item with the lifetime price
    StripeInvoiceItem::create([
        'customer' => $user->stripeId(),
        'price' => $plan->plan_lifetime,
    ], $user->stripeOptions());

    // create the invoice with the lifetime item and finalize it automatically after 1 hour
    $user->invoice(['auto_advance' => true]);
}

ถัดไป เพิ่ม invoice.paid ลงใน webhooks ของคุณและความต้องการของคุณในการฟังการแจ้งเตือนนี้โดยขยาย WebhookController ที่นี่คุณจะพบใบแจ้งหนี้ที่ตรงกัน และตรวจสอบว่ารายการในใบแจ้งหนี้มีรหัสเดียวกันกับ $plan->lifetime_plan หรือไม่ หากเป็นเช่นนั้น คุณสามารถอัปเดตคอลัมน์ในรูปแบบลูกค้าของคุณเพื่อตั้งค่าให้มีการสมัครใช้งานตลอดชีพ:

public function handleInvoicePaid(array $payload)
{
    if ($user = $this->getUserByStripeId($payload['data']['object']['customer'])) {
        $data = $payload['data']['object'];

        $invoice = $user->findInvoice($data['id']);

        if (isset($invoice)) {
            $plan = ...

            if ($invoice->invoiceItems()->contains(function (InvoiceLineItem $item) use ($plan) {
                return $item->price->id === $plan->lifetime_plan;
            })) {
                $user->update(['has_lifetime_subscription' => true]);
            }
        }
    }

    return $this->successMethod();
}

ในแอปพลิเคชันของคุณ คุณสามารถตรวจสอบว่าผู้ใช้มีการสมัครสมาชิกแบบตลอดชีพหรือแบบปกติ:

if ($user->has_lifetime_subscription || $user->subscribed()) {
    // ...
}
person sebdesign    schedule 23.12.2020