Untitled
unknown
php
2 years ago
8.0 kB
5
Indexable
<?php namespace StripeWebhooks; use Dingo\Api\Facade\API; use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Mail; class ChargeWebhooksController extends \BaseController { public function capture_charge_success () { $failed = false; $purchased_item = []; $event = \Input::all(); $transaction_meta = \TransactionMeta::where('key', 'stripe_payment_intent')->where('value', $event['data']['object']['payment_intent'])->first(); // If the description is not set it means that the charge was for a one-time payment if (!isset($transaction_meta)) { $failed = true; } else { // Get transaction and additional associated information $transaction = \Transaction::find($transaction_meta->transaction_id); $user = \User::find($transaction->user_id); if ($transaction->status !== 'success') { // Get the transaction item and add to the user's purchases $transaction_meta_for = \TransactionMeta::where('key', 'for')->where('transaction_id', $transaction->id)->first(); $ipnWebhookController = new \TwoCheckoutWebhooks\IPNWebhookController(); switch ($transaction_meta_for->value) { case 'basket': // Update user meeting tickets status $user_tickets = \UserMeetingTicket::where('transaction_id', $transaction->id)->where('order_status', 'pending') ->update(array('order_status' => 'completed')); $ipnWebhookController->approve_meeting_purchase(null, $transaction_meta, 'stripe'); break; case 'ora-coin': $response = $ipnWebhookController->approve_ora_coin_purchase($transaction->id, $transaction_meta); if ($response) { // send confirmation email $transaction_meta_vat = \TransactionMeta::where('key', 'vat')->where('transaction_id', $transaction->id)->first(); $transaction_meta_amount = \TransactionMeta::where('key', 'amount')->where('transaction_id', $transaction->id)->first(); $extra_data = [ 'subtotal' => number_format((float)$transaction_meta_amount->value, 2), 'vat' => number_format((float)$transaction_meta_vat->value, 2), 'total' => number_format((float)$transaction_meta_amount->value + (float)$transaction_meta_vat->value, 2) ]; $billed_to = $event['data']['object']['metadata']['address'].', '.$event['data']['object']['metadata']['address2'].', '.$event['data']['object']['metadata']['city']. ', '.$event['data']['object']['metadata']['country'].', '.$event['data']['object']['metadata']['zip'].', '.$event['data']['object']['metadata']['phone']; $planId = \TransactionMeta::where('transaction_id', $transaction->id) ->where('key', 'plan_id') ->first(); $ora_bundle_plan = \OraCoinBundle::where('id', $planId->value)->first(); \Mail::queue('emails.new.ora-coin-purchase', [ 'fullname' => $user->show_nickname ? $user->nickname : $user->fullname, 'transaction' => $transaction, 'extra_data' => $extra_data, 'vat' => number_format((float)$transaction_meta_vat->value, 2), 'billed_to' => $billed_to, 'has_vr_headset' => $user->has_vr_headset, 'quantity' => $ora_bundle_plan->quantity ], function ($message) use ($user) { $message->from('noreply@eyeora.com', 'eyeora VR'); $message->to($user->email, $user->fullname); $message->subject('Thank you for your payment'); }); } break; case 'viewing': $ipnWebhookController->approve_viewing_purchase($transaction->id, $transaction_meta); break; case 'merch': $ipnWebhookController->approve_merch_purchase($transaction->id, $transaction_meta); break; default: break; } $this->notify_user($transaction->user_id, $transaction->id); } $transaction->status = 'success'; $transaction->save(); } if ($failed === false) { return API::response()->array([ 'return' => true, 'output' => [] ])->statusCode(200); } else { return API::response()->array([ 'return' => false, 'output' => [] ])->statusCode(200); } } public function capture_charge_failed () { $purchased_item = []; $transaction = null; $failed = false; $event = \Input::all(); $transaction_meta = \TransactionMeta::where('key', 'stripe_payment_intent')->where('value', $event['data']['object']['payment_intent'])->first(); if (!isset($transaction_meta)) { return API::response()->array([ 'return' => false, 'output' => [], 'message' => 'Transaction not found.' ])->statusCode(200); } // Get transaction and additional associated information $transaction = \Transaction::find($transaction_meta->transaction_id); $transaction->status = 'failed'; $transaction->save(); $transaction_meta_for = \TransactionMeta::where('key', 'for')->where('transaction_id', $transaction->id)->first(); $ipnWebhookController = new \TwoCheckoutWebhooks\IPNWebhookController(); switch ($transaction_meta_for->value) { case 'basket': $ipnWebhookController->reject_meeting_purchase($transaction->id, $transaction_meta, $event['data']['object']['failure_message']); break; case 'ora-coin': $ipnWebhookController->reject_ora_coin_purchase($transaction->id, $transaction_meta, $event['data']['object']['failure_message']); break; case 'viewing': $ipnWebhookController->reject_viewing_purchase($transaction->id, $transaction_meta, $event['data']['object']['failure_message']); break; case 'merch': $ipnWebhookController->reject_merch_purchase($transaction->id, $transaction_meta, $event['data']['object']['failure_message']); break; default: break; } return API::response()->array([ 'return' => true, 'output' => [] ]); } // Notify the user about the payment outcome. function notify_user($user_id, $orderReference, $message = "Your payment transaction was successful.", $outcome = true) { $broadcast = new \Broadcast(); $channel = md5("user-" . $user_id); $message = [ 'type' => 'PAYMENT_RESULT', 'message' => $message, 'outcome' => $outcome, 'ref_no' => $orderReference, 'notification_uid' => uniqid('noti-'), 'created_at' => date('Y-m-d H:i:s', time()) ]; $broadcast->send_message($channel, 'notification', $message); } }
Editor is loading...