×ðÁú¿­Ê±¹ÙÍøµÇ¼

Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel CashierʵÏÖStripe¶©Ôĸ¶¿î£¿

laravel¿ª·¢£ºÔõÑùʹÓÃlaravel cashierʵÏÖstripe¶©Ôĸ¶¿î£¿

StripeÊÇÒ»¼ÒÈ«ÇòÁìÏȵÄÔÚÏßÖ§¸¶´¦Àí¹«Ë¾£¬ËüÌṩÁËÔÚÈ«Çò¹æÄ£ÄÚ¿É¿¿µÄÖ§¸¶»ù´¡ÉèÊ©£¬ÉÁ¿ª·¢ÕߺÍÓªÒµÖ°Ô±Äܹ»Àû±ãµØʵÏÖÔÚÏ߸¶¿î¹¦Ð§¡£Ê¹ÓÃStripe£¬¿ª·¢Õß¿ÉÒÔÇáËɵØʵÏÖÓû§µÄÖ§¸¶Á÷³Ì£¬²¢ÇÒ¿ÉÒÔÖ§³Ö¶àÖÖ¸¶¿î·½·¨£¬°üÀ¨ÐÅÓÿ¨¡¢Ö§¸¶±¦¡¢Î¢ÐŵÈ¡£

LaravelÊÇÒ»ÖÖºÜÊÇÊ¢ÐеÄPHP¿ò¼Ü£¬Ëü¾ßÓмòÆÓ¡¢ÓÅÑÅ¡¢¸ßЧµÈÌص㣬²¢ÌṩÁËÐí¶àÇ¿Ê¢µÄ¹¦Ð§¡£Laravel CashierÊÇLaravelµÄÒ»¸öÀ©Õ¹°ü£¬ËüÌṩÁËÒ»¸ö¼òÆÓ¶øÇ¿Ê¢µÄ·½·¨À´ÖÎÀíStripe¶©Ôĸ¶¿î£¬Ê¹µÃÔÚLaravelÓ¦ÓÃÖÐʵÏÖ¶©Ôĺ͸¶¿î±äµÃÔ½·¢ÈÝÒס£

ÏÂÃ潫»áÏÈÈÝÔõÑùÔÚLaravelÓ¦ÓÃÖÐʹÓÃLaravel CashierÀ©Õ¹°üÀ´ÖÎÀíStripe¶©Ôĸ¶¿î¡£

µÚÒ»²½£º×°ÖÃLaravel CashierÀ©Õ¹°ü

ÔÚLaravelÓ¦ÓÃÖУ¬×°ÖÃLaravel CashierÀ©Õ¹°üºÜÊǼòÆÓ¡£Ê×ÏÈ£¬Í¨¹ýComposer×°Öãº

composer require laravel/cashier

µÇ¼ºó¸´ÖÆ

È»ºó£¬ÔËÐÐÊý¾Ý¿âǨá㣺

php artisan migrate

µÇ¼ºó¸´ÖÆ

µÚ¶þ²½£ºÉèÖÃStripe APIÃÜÔ¿

ÔÚʹÓÃLaravel Cashier֮ǰ£¬ÐèÒªÔÚ.envÎļþÖÐÉèÖÃStripe APIÃÜÔ¿¡£¿ÉÒÔÔÚStripe DashboardÉÏÕÒµ½ÄãµÄÃÜÔ¿£¬²¢½«ÆäÌí¼Óµ½.envÎļþÖУº

STRIPE_KEY=your-stripe-key
STRIPE_SECRET=your-stripe-secret

µÇ¼ºó¸´ÖÆ

µÚÈý²½£º½¨ÉèStripe¶©ÔÄÍýÏë

ÔÚʹÓÃLaravel Cashier֮ǰ£¬ÐèÒªÔÚStripe DashboardÉϽ¨É趩ÔÄÍýÏë¡£¿ÉÒÔÔÚStripe DashboardÉÏÑ¡Ôñ¡°Billing-Subscriptions-Plans¡±À´½¨É趩ÔÄÍýÏë¡£

½¨É趩ÔÄÍýÏëÐèÒªÖ¸¶¨ÍýÏëµÄÃû³Æ¡¢¼ÛÇ®¡¢¸¶¿îÖÜÆÚµÈÐÅÏ¢¡£ÔÚ½¨ÉèÍ궩ÔÄÍýÏëºó£¬ÐèÒª¼Ç϶©ÔÄÍýÏëµÄID£¬ºóÃæ»áÓõ½¡£

µÚËIJ½£º½¨Éè×Ô½ç˵Óû§Ä£×Ó

Laravel CashierÔÚʹÓÃÖÐÐèҪʹÓÃ×Ô½ç˵Óû§Ä£×Ó£¬Òò´ËÐèÒªÔÚLaravelÓ¦ÓÃÖн¨ÉèÒ»¸ö×Ô½ç˵Óû§Ä£×Ó¡£¿ÉÒÔͨ¹ýÔËÐÐÒÔÏÂÏÂÁîÀ´½¨Éè¸ÃÄ£×Ó£º

php artisan make:model User

µÇ¼ºó¸´ÖÆ

È»ºó£¬ÔÚUserÄ£×ÓÖÐÌí¼ÓÒÔÏ´úÂëÒÔʹÓÃLaravel Cashier£º

use LaravelCashierBillable;

class User extends Authenticatable
{
    use Billable;
}

µÇ¼ºó¸´ÖÆ

ͨ¹ýÔÚUserÄ£×ÓÖÐʹÓÃLaravel CashierµÄBillable trait£¬¿ÉÒÔʹ¸ÃÄ£×Ó¾ßÓж©ÔÄÍýÏëºÍ¸¶¿îÏà¹ØµÄ¹¦Ð§¡£

µÚÎå²½£º½¨É趩ÔÄ¿ØÖÆÆ÷

ÔÚLaravelÓ¦ÓÃÖУ¬ÐèÒª½¨ÉèÒ»¸ö¿ØÖÆÆ÷À´´¦Àí¶©ÔÄÏà¹ØµÄ²Ù×÷£¬°üÀ¨½¨É趩ÔÄ¡¢¸üж©ÔÄ¡¢×÷·Ï¶©ÔĵÈ¡£

¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´½¨É趩ÔÄ¿ØÖÆÆ÷£º

php artisan make:controller SubscriptionController

µÇ¼ºó¸´ÖÆ

È»ºó£¬ÔÚSubscriptionControllerÖÐÌí¼ÓÒÔÏ´úÂëÒÔʵÏÖ¶©ÔĹ¦Ð§£º

use IlluminateHttpRequest;

class SubscriptionController extends Controller
{
    public function create(Request $request)
    {
        $user = $request->user();
        $plan = $request->input('plan');
        $user->newSubscription('main', $plan)->create($request->stripeToken);
        return redirect('/home')->with('success', 'Subscription created successfully.');
    }

    public function edit(Request $request)
    {
        $user = $request->user();
        $subscription = $user->subscription('main');
        $subscription->swap($request->input('plan'));
        return redirect('/home')->with('success', 'Subscription updated successfully.');
    }

    public function destroy(Request $request)
    {
        $user = $request->user();
        $subscription = $user->subscription('main');
        $subscription->cancel();
        return redirect('/home')->with('success', 'Subscription cancelled successfully.');
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÊö´úÂëÖУ¬create()ÒªÁìÓÃÓÚ½¨ÉèеĶ©ÔÄ£¬edit()ÒªÁìÓÃÓÚ¸üж©ÔÄÍýÏ룬destroy()ÒªÁìÓÃÓÚ×÷·Ï¶©ÔÄ¡£

µÚÁù²½£º½¨É趩ÔÄÊÓͼ

×îºó£¬ÔÚLaravelÓ¦ÓÃÖн¨ÉèÒ»¸ö¶©ÔÄÊÓͼ£¬²¢ÔÚ¸ÃÊÓͼÖаüÀ¨¶©ÔÄ±íµ¥¡£¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´½¨É趩ÔÄÊÓͼ£º

php artisan make:view subscribe

µÇ¼ºó¸´ÖÆ

È»ºó£¬ÔÚ½¨ÉèµÄsubscribe.blade.phpÊÓͼÖÐÌí¼ÓÒÔÏÂ±íµ¥£º

<form action="{{ route('subscription.create') }}" method="post">
    @csrf
    <label for="plan">Subscription Plan</label>
    <select name="plan">
        <option value="basic">Basic ($10/month)</option>
        <option value="pro">Pro ($20/month)</option>
    </select>
    <script
        src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-key="{{ env('STRIPE_KEY') }}"
        data-amount="1000"
        data-name="My Site"
        data-description="Monthly Subscription"
        data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
        data-locale="auto"
        data-currency="usd"
    >
    </script>
</form>

µÇ¼ºó¸´ÖÆ

ͨ¹ýÔÚÉÏÊö±íµ¥ÖÐʹÓÃStripe Checkout£¬¿ÉÒÔʹÓû§Ö±½Ó´ÓÓ¦ÓÃÖоÙÐÐÖ§¸¶¡£

È»ºó£¬ÔÚLaravelÓ¦ÓÃÖÐÌí¼Ó¶©ÔÄ·ÓÉ£¬ÒÔ±ãÔÚÊÓͼÖÐʹÓø÷ÓÉ£º

Route::post('/subscription', [SubscriptionController::class, 'create'])->name('subscription.create');
Route::put('/subscription', [SubscriptionController::class, 'edit'])->name('subscription.edit');
Route::delete('/subscription', [SubscriptionController::class, 'destroy'])->name('subscription.destroy');

µÇ¼ºó¸´ÖÆ

×îºó£¬ÔÚÓ¦ÓÃÖÐÌí¼ÓÏà¹ØµÄÒ³ÃæÀ´ÏÔʾ¶©ÔĺÍ×÷·Ï¶©ÔĵÄ״̬£¬ÒÔ¼°ÆäËûÏà¹ØÐÅÏ¢¡£

×ܽá

ʹÓÃLaravel CashierºÍStripe£¬¿ÉÒÔÉÁ¿ª·¢ÕßÇáËɵØʵÏÖ¶©Ôĺ͸¶¿î¹¦Ð§¡£ÔÚ±¾ÎÄÖУ¬ÏÈÈÝÁËÔõÑùÔÚLaravelÓ¦ÓÃÖÐʹÓÃLaravel CashierÀ´ÖÎÀíStripe¶©Ôĸ¶¿î£¬°üÀ¨×°ÖÃLaravel Cashier¡¢ÉèÖÃStripe APIÃÜÔ¿¡¢½¨ÉèStripe¶©ÔÄÍýÏë¡¢½¨Éè×Ô½ç˵Óû§Ä£×Ó¡¢½¨É趩ÔÄ¿ØÖÆÆ÷ºÍ¶©ÔÄÊÓͼµÈ¡£

ËäÈ»£¬ÔÚʵÏÖ¶©Ôĺ͸¶¿î¹¦Ð§Ê±£¬»¹ÐèÒª×ñÊØÏà¹ØµÄÖ´·¨¡¢»®¶¨ºÍ±ê×¼£¬È·±£Óû§µÄÒþ˽ºÍÖ§¸¶Çå¾²¡£Ï£ÍûÕâƪÎÄÕ¶ÔLaravel¿ª·¢ÕßÓÐËù×ÊÖú£¬ÈÃËûÃÇÔ½·¢ÇáËɵØʵÏÖStripeÖ§¸¶¹¦Ð§¡£

ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel CashierʵÏÖStripe¶©Ôĸ¶¿î£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ