|
|
@@ -19,7 +19,6 @@ class BillingController extends Controller |
|
|
|
//Should validate that all amounts are positive integers in a reasonable range |
|
|
|
public function secret(Request $request) { |
|
|
|
Stripe::setApiKey(env('STRIPE_SECRET')); |
|
|
|
Log::debug($request->packs); |
|
|
|
|
|
|
|
$amount = $request->packs[ 'credits10' ]*1099 + |
|
|
|
$request->packs[ 'credits50' ]*5499 + $request->packs[ 'credits100' ]*10999 |
|
|
@@ -28,18 +27,17 @@ class BillingController extends Controller |
|
|
|
$transaction = new Transaction; |
|
|
|
$transaction->credits = $request->packs['credits10']*10 + |
|
|
|
$request->packs['credits50']*50 + |
|
|
|
$request->packs['credits[100]']*100 + |
|
|
|
$request->packs['credits[1000]']*1000; |
|
|
|
$request->packs['credits100']*100 + |
|
|
|
$request->packs['credits1000']*1000; |
|
|
|
$transaction->credits_extra = |
|
|
|
$request->packs['credits50']*5 + |
|
|
|
$request->packs['credits[100]']*10 + |
|
|
|
$request->packs['credits[1000]']*150; |
|
|
|
$request->packs['credits100']*10 + |
|
|
|
$request->packs['credits1000']*150; |
|
|
|
|
|
|
|
$transaction->user_id = Auth::user()->id; |
|
|
|
$transaction->charge = $amount; |
|
|
|
$transaction->status = 'processing'; |
|
|
|
$transaction->completed = false; |
|
|
|
$transaction->save(); |
|
|
|
|
|
|
|
$intent = PaymentIntent::create([ |
|
|
|
'amount' => $amount, |
|
|
@@ -47,15 +45,39 @@ class BillingController extends Controller |
|
|
|
'customer' => Auth::user()->customer_id, |
|
|
|
'metadata' => ['transaction_id' => $transaction->id] |
|
|
|
]); |
|
|
|
$transaction->intent_id = $intent->id; |
|
|
|
|
|
|
|
$transaction->save(); |
|
|
|
return $intent->client_secret; |
|
|
|
} |
|
|
|
|
|
|
|
public function getCards() { |
|
|
|
Stripe::setApiKey(env('STRIPE_SECRET')); |
|
|
|
|
|
|
|
return PaymentMethod::all([ |
|
|
|
'customer' => Auth::user()->customer_id, |
|
|
|
'type' => 'card' |
|
|
|
]); |
|
|
|
} |
|
|
|
|
|
|
|
public function chargeEvent(Request $request) { |
|
|
|
Stripe::setApiKey(env('STRIPE_SECRET')); |
|
|
|
$event = \Stripe\Event::constructFrom($request->all()); |
|
|
|
$charge = $event->data->object; |
|
|
|
$transaction = Transaction::where('intent_id', $charge->payment_intent); |
|
|
|
$user = $transaction->user; |
|
|
|
|
|
|
|
if ($event->type == 'charge.succeeded') { |
|
|
|
Log::debug($charge->metadata['transaction_id']); |
|
|
|
$user->credits = $user->credits + $transaction->credits + $transaction->credits_extra; |
|
|
|
$transaction->status = 'completed'; |
|
|
|
$transaction->completed = true; |
|
|
|
$user->save(); |
|
|
|
$transaction->save(); |
|
|
|
|
|
|
|
} else { |
|
|
|
$transaction->status = $charge->status; |
|
|
|
$transaction->save(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |