My SMM panel
 
 
 
 
 
 

165 lines
4.7 KiB

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Stripe\Stripe;
  5. use Stripe\Customer;
  6. use Stripe\PaymentIntent;
  7. use Stripe\PaymentMethod;
  8. use Illuminate\Support\Facades\Log;
  9. use Illuminate\Support\Facades\Auth;
  10. use App\Models\Transaction;
  11. class BillingController extends Controller
  12. {
  13. protected $stripe;
  14. protected $user;
  15. public function __construct() {
  16. $this->stripe = new \Stripe\StripeClient(config('services.stripe.secret'));
  17. Stripe::setApiKey(config('services.stripe.secret'));
  18. $this->user = Auth::user();
  19. }
  20. protected function attempt($packs) {
  21. $user = Auth::user();
  22. foreach($packs as $value) {
  23. if ($value < 0) {
  24. abort(422);
  25. }
  26. }
  27. $amount = $packs[ 'credits10' ]*1099 +
  28. $packs[ 'credits50' ]*5499 + $packs[ 'credits100' ]*10999
  29. + $packs[ 'credits1000' ]*101000;
  30. $transaction = new Transaction;
  31. $transaction->credits = $packs['credits10']*1000 +
  32. $packs['credits50']*5000 +
  33. $packs['credits100']*10000 +
  34. $packs['credits1000']*100000;
  35. $transaction->credits_extra =
  36. $packs['credits50']*500 +
  37. $packs['credits100']*1000 +
  38. $packs['credits1000']*15000;
  39. $transaction->user_id = $user->id;
  40. $transaction->charge = $amount;
  41. $transaction->status = 'processing';
  42. $transaction->completed = false;
  43. $transaction->save();
  44. return $transaction;
  45. }
  46. //Expects an array 'packs' representing the amount of each multiple of credits.
  47. //Should validate that all amounts are positive integers in a reasonable range
  48. public function stripeSecret(Request $request) {
  49. $user = Auth::user();
  50. $transaction = $this->attempt($request->packs);
  51. $intent = PaymentIntent::create([
  52. 'amount' => $amount,
  53. 'currency' => 'usd',
  54. 'customer' => $user->customer_id,
  55. 'description' => "You have received $total_credits credits.",
  56. 'receipt_email' => Auth::user()->email,
  57. 'metadata' => ['transaction_id' => $transaction->id]
  58. ]);
  59. $transaction->intent_id = $intent->id;
  60. //Save the card as a default if none is set and it was selected
  61. if ($user->payment_method == null && $request->card) {
  62. $this->changeDefaultCard($request->card);
  63. }
  64. $transaction->save();
  65. return $intent->client_secret;
  66. }
  67. public function getCards() {
  68. return PaymentMethod::all([
  69. 'customer' => Auth::user()->customer_id,
  70. 'type' => 'card'
  71. ]);
  72. }
  73. //Adds correct credit amount to the charged user, precise to two decimal places
  74. public function chargeEvent(Request $request) {
  75. $event = \Stripe\Event::constructFrom($request->all());
  76. $charge = $event->data->object;
  77. $transaction = Transaction::where('intent_id', $charge->payment_intent)->first();
  78. $user = $transaction->user;
  79. if ($event->type == 'charge.succeeded') {
  80. $user->credits = $user->credits + $transaction->credits + $transaction->credits_extra;
  81. $transaction->status = 'completed';
  82. $transaction->completed = true;
  83. $user->save();
  84. $transaction->save();
  85. } else {
  86. $transaction->status = $charge->status;
  87. $transaction->save();
  88. }
  89. }
  90. public function changeDefaultCard(String $card) {
  91. $user = Auth::user();
  92. $user->payment_method = $card;
  93. $user->save();
  94. $cards = $this->getCards();
  95. return PaymentMethod::all([
  96. 'customer' => Auth::user()->customer_id,
  97. 'type' => 'card'
  98. ]);
  99. }
  100. public function deleteCard(Request $request) {
  101. $this->stripe->paymentMethods->detach($request->card);
  102. $user = Auth::user();
  103. if ($request->card == $user->payment_method) {
  104. $user->payment_method = null;
  105. $user->save();
  106. }
  107. return ($this->getCards());
  108. }
  109. //Receives a request with a packs. It is an array of each type of credit
  110. //amount to be bought
  111. public function payeer(Request $request) {
  112. $user = Auth::user();
  113. $transaction = $this->attempt($request->packs);
  114. $shopid = config('services.payeer.id');
  115. $secret = config('services.payeer.secret');
  116. $param_key = config('services.payeer.param_key');
  117. $total = $transaction->credits/100 + $transaction->credits_extra/100;
  118. $description = base64_encode("You will receive $total credits.");
  119. $arHash = [$shopid, $transaction->id, $transaction->charge/100, 'USD',
  120. $description];
  121. $params = ['reference' => ['transaction_id' => $transaction->id]];
  122. $key = md5($param_key.$transaction->id);
  123. $encodedParams = @urlencode(base64_encode(openssl_encrypt(
  124. json_encode($params), 'AES-256-CBC', $key, OPENSSL_RAW_DATA
  125. )));
  126. $arHash[] = $encodedParams;
  127. $arHash[] = $secret;
  128. $signature = strtoupper(hash('sha256', implode(':', $arHash)));
  129. $user->paying = true; $user->save();
  130. return [ 'signature' => $signature, 'params' => $encodedParams, 'shop'
  131. => $shopid, 'transaction' => $transaction->id, 'amount' =>
  132. $transaction->charge/100, 'description' => $description ];
  133. }
  134. public function processPayeer(Request $request) {
  135. }
  136. }