My SMM panel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

206 lines
5.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. if ($event->type == 'charge.succeeded') {
  79. $this->creditUser($transaction->id);
  80. } else {
  81. $transaction->status = $charge->status;
  82. $transaction->save();
  83. }
  84. }
  85. public function changeDefaultCard(String $card) {
  86. $user = Auth::user();
  87. $user->payment_method = $card;
  88. $user->save();
  89. $cards = $this->getCards();
  90. return PaymentMethod::all([
  91. 'customer' => Auth::user()->customer_id,
  92. 'type' => 'card'
  93. ]);
  94. }
  95. public function deleteCard(Request $request) {
  96. $this->stripe->paymentMethods->detach($request->card);
  97. $user = Auth::user();
  98. if ($request->card == $user->payment_method) {
  99. $user->payment_method = null;
  100. $user->save();
  101. }
  102. return ($this->getCards());
  103. }
  104. //Receives a request with a packs. It is an array of each type of credit
  105. //amount to be bought
  106. public function payeer(Request $request) {
  107. $user = Auth::user();
  108. $transaction = $this->attempt($request->packs);
  109. $shopid = config('services.payeer.id');
  110. $secret = config('services.payeer.secret');
  111. $param_key = config('services.payeer.param_key');
  112. $total = $transaction->credits/100 + $transaction->credits_extra/100;
  113. $description = base64_encode("You will receive $total credits.");
  114. $arHash = [$shopid, $transaction->id, $transaction->charge/100, 'USD',
  115. $description];
  116. $params = ['reference' => ['transaction_id' => $transaction->id]];
  117. $key = md5($param_key.$transaction->id);
  118. $encodedParams = @urlencode(base64_encode(openssl_encrypt(
  119. json_encode($params), 'AES-256-CBC', $key, OPENSSL_RAW_DATA
  120. )));
  121. $arHash[] = $encodedParams;
  122. $arHash[] = $secret;
  123. $signature = strtoupper(hash('sha256', implode(':', $arHash)));
  124. $user->paying = true; $user->save();
  125. return [ 'signature' => $signature, 'params' => $encodedParams, 'shop'
  126. => $shopid, 'transaction' => $transaction->id, 'amount' =>
  127. $transaction->charge/100, 'description' => $description ];
  128. }
  129. //This needs to check the ip of the sender
  130. public function processPayeer(Request $request) {
  131. $secret = config('services.payeer.secret');
  132. $arHash = [$request->m_operation_id,
  133. $request->m_operation_ps,
  134. $request->m_operation_day,
  135. $request->m_operation_pay_date,
  136. $request->m_shop,
  137. $request->m_orderid,
  138. $request->m_amount,
  139. $request->m_curr,
  140. $request->m_desc,
  141. $request->m_status
  142. ];
  143. if (isset($request->m_params)) {
  144. $arHash[] = $request->m_params;
  145. }
  146. $arHash[] = $secret;
  147. $signature = strtoupper(hash('sha256', implode(':', $arHash)));
  148. if ($signature == $request->m_sign && $request->m_status == 'success'){
  149. $this->creditUser((int) $request->m_orderid);
  150. return $request->m_orderid.'|success';
  151. } else {
  152. $transaction = Transaction::find($request->orderid);
  153. $transaction->status = 'error';
  154. $transaction->save();
  155. return $request->m_orderid.'|error';
  156. }
  157. }
  158. //Credits the user of a given transaction id
  159. public function creditUser($transaction_id) {
  160. $transaction = Transaction::find($transaction_id);
  161. if ($transaction->completed) {
  162. return;
  163. }
  164. $user = $transaction->user;
  165. $user->credits = $user->credits + $transaction->credits +
  166. $transaction->credits_extra;
  167. $transaction->status = 'completed';
  168. $transaction->completed = true;
  169. $user->save();
  170. $transaction->save();
  171. }
  172. }