Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel CashierʵÏÖ¶©ÔÄÖ§¸¶£¿
laravelÊÇÒ»¿îÊ¢ÐеÄphp¿ò¼Ü£¬Äܹ»¼«´óµØ¼ò»¯webÓ¦ÓóÌÐòµÄ¿ª·¢Àú³Ì¡£laravel cashierÊÇÒ»¸ö¸¶¿î´¦Öóͷ£¿â£¬ËüÌṩÁË´¦Öóͷ£¶©Ôĸ¶¿îµÄ¹¦Ð§¡£Ëü¿ÉÒÔÓëstripe¼¯³É£¬Ê¹Óû§Äܹ»ÔËÐоßÓж©ÔĹ¦Ð§µÄÓ¦ÓóÌÐò¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃlaravel cashierʵÏÖ¶©Ôĸ¶¿î¡£
½¨ÉèStripeÕÊ»§
ÔÚʹÓÃLaravel CashierÇ°£¬ÐèÒªÏȽ¨ÉèÒ»¸öStripeÕÊ»§¡£StripeÊÇÒ»¸ö´¦Öóͷ£ÔÚÏßÖ§¸¶µÄÔÆƽ̨£¬ËüµÄAPI¿ÉÒÔÇáËɵØÓëLaravel Cashier¼¯³É¡£»á¼ûStripeÍøÕ¾£¬½¨ÉèÒ»¸öÕÊ»§£¬È»ºóµÇ¼µ½¿ØÖÆ̨¡£ÔÚ¿ØÖÆ̨ÖУ¬¿ÉÒÔÕÒµ½APIÃÜÔ¿£¬ÕâÊÇÔÚLaravel CashierÖм¯³ÉStripeËùÐèµÄÒªº¦ÐÅÏ¢¡£
×°ÖÃLaravel Cashier
ʹÓÃComposerÔÚLaravelÏîÄ¿ÖÐ×°ÖÃLaravel Cashier¡£ÔÚÖÕ¶ËÖУ¬½øÈëLaravelÏîÄ¿µÄ¸ùĿ¼²¢Ö´ÐÐÒÔÏÂÏÂÁ
composer require laravel/cashier
µÇ¼ºó¸´ÖÆ
Ö´ÐÐÉÏÊöÏÂÁîºó£¬Composer½«×Ô¶¯×°ÖÃËùÐèµÄÒÀÀµÏ²¢½«Laravel CashierÌí¼Óµ½ÏîÄ¿ÖС£
ÉèÖÃLaravel Cashier
ÔÚ×°ÖÃLaravel CashierÖ®ºó£¬ÐèÒªÉèÖÃËüÒÔ׼ȷµØʹÓÃStripe API¡£Ê×ÏÈ£¬ÔÚÏîÄ¿Öз¿ªconfig/services.phpÎļþ£¬È»ºó½«ÒÔÏ´úÂëÌí¼Óµ½ÎļþÖУº
'stripe' => [ 'model' => AppModelsUser::class, 'key' => env('STRIPE_KEY'), 'secret' => env('STRIPE_SECRET'), ],
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëΪLaravel CashierÌṩÁËÐëÒªµÄÉèÖÃÐÅÏ¢£¬°üÀ¨Ê¹ÓÃStripeËùÐèµÄAPIÃÜÔ¿ºÍÓû§Ä£×Ó¡£
È»ºó£¬ÔÚ.envÎļþÖÐÉèÖÃStripe APIÃÜÔ¿ºÍStripe Connect¿Í»§¶ËID£º
STRIPE_KEY=your-stripe-key STRIPE_SECRET=your-stripe-secret STRIPE_CLIENT_ID=your-stripe-client-id
µÇ¼ºó¸´ÖÆ
½¨É趩ÔÄÍýÏë
ÔÚ¼¯³ÉStripe APIÖ®ºó£¬ÐèÒª½¨ÉèÒ»¸ö¶©ÔÄÍýÏëÒÔÔÚÓ¦ÓóÌÐòÖÐʹÓá£ÔÚStripe¿ØÖÆ̨ÖУ¬µ¼º½µ½¡°Products¡±Ñ¡Ï£¬È»ºóµ¥»÷¡°New Product¡±ÒÔ½¨ÉèÒ»¸ö²úÆ·¡£ÔÚ½¨Éè²úÆ·ºó£¬·¿ª¡°Billing¡±Ñ¡Ï£¬È»ºóµ¥»÷¡°New Plan¡±ÒÔ½¨ÉèÒ»¸öÍýÏ롣ƾ֤ÐèÒªÌîдÍýÏëÃû¡¢¼ÛÇ®ºÍÖÜÆÚ£¬È»ºóµ¥»÷¡°Create Plan¡±¡£
¸üÐÂÓû§Ä£×Ó
Laravel CashierʹÓÃÓû§Ä£×ÓÀ´ÖÎÀí¶©ÔÄ¡£Òò´Ë£¬ÔÚ¼ÌÐø֮ǰ£¬ÐèÒªÔÚÓû§Ä£×ÓÖаüÀ¨Ò»Ð©ÒªÁì¡£·¿ªÓû§Ä£×Ó²¢½«ÒÔÏ´úÂëÌí¼Óµ½Îļþĩβ£º
use LaravelCashierBillable; class User extends Model implements AuthenticatableContract, CanResetPasswordContract { use Authenticatable, CanResetPassword, Billable; }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÔÚÓû§Ä£×ÓÖÐʹÓÃÁËLaravel CashierµÄBillable trait¡£ÕâÔÊÐíÔÚÓû§Ä£×ÓÖÐʹÓÃÓëLaravel CashierÏà¹ØµÄÒªÁì¡£
ʵÏÖ¶©ÔĹ¦Ð§
ʹÓÃLaravel CashierʵÏÖ¶©ÔĹ¦Ð§ºÜÊǼòÆÓ¡£ÔÚÊÓͼÖУ¬Ö»ÐèÌí¼ÓÒÔÏÂ±íµ¥£º
<form method="POST" action="{{ route('subscribe') }}"> {{ csrf_field() }} <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" data-key="{{ config('services.stripe.key') }}" data-amount="999" data-name="My Awesome Website" data-description="Monthly Subscription" data-image="https://stripe.com/img/documentation/checkout/marketplace.png" data-locale="auto"> </script> </form>
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëΪÄúµÄÍøÕ¾Ìí¼ÓÒ»¸öStripe¶©ÔÄ°´Å¥¡£µ±Óû§µ¥»÷¸Ã°´Å¥Ê±£¬½«Ìøתµ½StripeÒ³Ã棬ÒÔÍê³É¶©ÔÄÁ÷³Ì¡£
ÔÚ¿ØÖÆÆ÷ÖУ¬Ö»ÐèÌí¼ÓÒÔÏ´úÂ룬¼´¿É½«¶©ÔÄÐÅÏ¢ÓëÓû§Ä£×Ó¹ØÁªÆðÀ´£º
public function subscribe(Request $request) { $user = $request->user(); $user->newSubscription('main', 'plan_id')->create($request->stripeToken); return redirect()->back(); }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂë»á½¨ÉèÒ»¸öеĶ©ÔÄ¡¢½«ÆäÓëÓû§Ä£×Ó¹ØÁª£¬²¢½«¶©Ôĸ¶¿îÐÅÏ¢´æ´¢ÔÚStripeÖС£È»ºó£¬½«Óû§Öض¨Ïò»ØÒ³Ã棬²¢¼ÌÐøÔËÐÐÓ¦ÓóÌÐò¡£
×÷·Ï¶©ÔÄ
ÔÚLaravel CashierÖУ¬×÷·Ï¶©ÔĺÜÊǼòÆÓ¡£Ö»ÐèÔÚ¿ØÖÆÆ÷ÖÐÌí¼ÓÒÔÏ´úÂ룬¼´¿É×÷·ÏÌض¨µÄ¶©ÔÄ£º
public function cancel(Request $request) { $user = $request->user(); $user->subscription('main')->cancel(); return redirect()->back(); }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂ뽫×÷·ÏÌض¨¶©ÔÄ£¬²¢½«Óû§Öض¨Ïò»ØÒ³Ãæ¡£
½áÂÛ
Laravel CashierÊÇÒ»¸öÇ¿Ê¢µÄ¸¶¿î´¦Öóͷ£¿â£¬¿ÉÒÔ¼«´óµØ¼ò»¯¶©Ôĸ¶¿îµÄÁ÷³Ì¡£Í¨¹ýʹÓÃStripe API£¬Laravel CashierÄܹ»ÇáËɵؽ«¶©ÔÄÁ÷³Ì¼¯³Éµ½LaravelÓ¦ÓóÌÐòÖС£Í¨¹ý±¾ÎĵÄÖ¸µ¼£¬Äú¿ÉÒÔÇáËɵØʹÓÃLaravel CashierʵÏÖ¶©Ôĸ¶¿î¹¦Ð§¡£
ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel CashierʵÏÖ¶©ÔÄÖ§¸¶£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡