Examples of code I've written in PHP, Javascript, SCSS, etc.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

279 行
7.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. $allowed = ['185.71.65.92', '185.71.65.189', '149.202.17.210'];
  132. $ipAddress = $request->ip();
  133. if (!in_array($ipAddress, $allowed)){
  134. abort(401);
  135. }
  136. Log::debug('Processing Payeer payment');
  137. Log::debug($request);
  138. $secret = config('services.payeer.secret');
  139. $arHash = [$request->m_operation_id,
  140. $request->m_operation_ps,
  141. $request->m_operation_day,
  142. $request->m_operation_pay_date,
  143. $request->m_shop,
  144. $request->m_orderid,
  145. $request->m_amount,
  146. $request->m_curr,
  147. $request->m_desc,
  148. $request->m_status
  149. ];
  150. if (isset($request->m_params)) {
  151. $arHash[] = $request->m_params;
  152. }
  153. $arHash[] = $secret;
  154. $signature = strtoupper(hash('sha256', implode(':', $arHash)));
  155. if ($signature == $request->m_sign && $request->m_status == 'success'){
  156. $this->creditUser((int) $request->m_orderid);
  157. return $request->m_orderid.'|success';
  158. } else {
  159. $transaction = Transaction::find($request->orderid);
  160. $transaction->status = 'error';
  161. $transaction->save();
  162. return $request->m_orderid.'|error';
  163. }
  164. }
  165. //Credits the user of a given transaction id
  166. public function creditUser($transaction_id) {
  167. $transaction = Transaction::find($transaction_id);
  168. if ($transaction->completed) {
  169. abort(422, 'Bad transaction ID');
  170. }
  171. $user = $transaction->user;
  172. $user->credits = $user->credits + $transaction->credits +
  173. $transaction->credits_extra;
  174. $transaction->status = 'completed';
  175. $transaction->completed = true;
  176. $user->save();
  177. $transaction->save();
  178. }
  179. public function pm(Request $request) {
  180. $user = Auth::user();
  181. $account = config('services.pm.account');
  182. $transaction = $this->attempt($request->packs);
  183. $total = $transaction->credits/100 +
  184. $transaction->credits_extra/100;
  185. $description = "You will receive $total credits.";
  186. $user->paying = true; $user->save();
  187. return ['account' => $account, 'transaction' => $transaction->id,
  188. 'amount' => $transaction->charge/100, 'description' => $description];
  189. }
  190. //Handler run after PM payment succeds
  191. public function processPM(Request $request) {
  192. $allowed = ['77.109.141.170', '91.205.41.208', '94.242.216.60',
  193. '78.41.203.75'];
  194. $transaction = Transaction::find($request->PAYMENT_ID);
  195. $secret = config('services.pm.secret');
  196. //Check that sender is PM and account the amount was paid to is mine.
  197. if (!in_array($request->ip(), $allowed)){
  198. abort(401);
  199. } else if ($request->PAYEE_ACCOUNT != config('services.pm.account')) {
  200. abort(422);
  201. } else if (!$transaction->complete) {
  202. abort(422);
  203. }
  204. Log::debug('Processing PM payment');
  205. Log::debug($request);
  206. //Would need to be changed if baggage fields are used
  207. $arHash = [$request->PAYMENT_ID,
  208. $request->PAYEE_ACCOUNT,
  209. $request->PAYMENT_AMOUNT,
  210. $request->PAYMENT_UNITS,
  211. $request->PAYMENT_BATCH_NUMBER,
  212. $request->PAYER_ACCOUNT,
  213. strtoupper(md5($secret)),
  214. $request->TIMESTAMPGMT,
  215. ];
  216. $signature = strtoupper(md5(implode(':', $arHash)));
  217. if ($signature == $request->V2_HASH){
  218. $this->creditUser((int) $transaction->id);
  219. } else {
  220. abort(422, 'Bad hash');
  221. }
  222. }
  223. public function completePM(Request $request) {
  224. return redirect('/panel/#transaction-complete');
  225. }
  226. public function failPM(Request $request) {
  227. return redirect('/panel/#transaction-failed');
  228. }
  229. }