OrderController.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\OrderSave;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\Order;
  7. use App\Models\Plan;
  8. use App\Models\User;
  9. use App\Utils\Helper;
  10. use Omnipay\Omnipay;
  11. use Stripe\Stripe;
  12. use Stripe\Source;
  13. class OrderController extends Controller
  14. {
  15. public function index (Request $request) {
  16. $order = Order::where('user_id', $request->session()->get('id'))
  17. ->orderBy('created_at', 'DESC')
  18. ->get();
  19. $plan = Plan::get();
  20. for($i = 0; $i < count($order); $i++) {
  21. for($x = 0; $x < count($plan); $x++) {
  22. if ($order[$i]['plan_id'] === $plan[$x]['id']) {
  23. $order[$i]['plan'] = $plan[$x];
  24. }
  25. }
  26. }
  27. return response([
  28. 'data' => $order
  29. ]);
  30. }
  31. public function details (Request $request) {
  32. $order = Order::where('user_id', $request->session()->get('id'))
  33. ->where('trade_no', $request->input('trade_no'))
  34. ->first();
  35. if (!$order) {
  36. abort(500, '订单不存在');
  37. }
  38. $order['plan'] = Plan::find($order->plan_id);
  39. $order['update_fee'] = config('v2board.plan_update_fee', 0.5);
  40. if (!$order['plan']) {
  41. abort(500, '订阅不存在');
  42. }
  43. return response([
  44. 'data' => $order
  45. ]);
  46. }
  47. public function save (OrderSave $request) {
  48. $plan = Plan::find($request->input('plan_id'));
  49. $user = User::find($request->session()->get('id'));
  50. if (!$plan) {
  51. abort(500, '该订阅不存在');
  52. }
  53. if (!($plan->show || $user->plan_id == $plan->id)) {
  54. abort(500, '该订阅已售罄');
  55. }
  56. if (!$plan->show && !$plan->renew) {
  57. abort(500, '该订阅无法续费,请更换其他订阅');
  58. }
  59. if (!(int)$plan[$request->input('cycle')]) {
  60. abort(500, '该订阅周期无法进行购买,请选择其他周期');
  61. }
  62. $order = new Order();
  63. $order->user_id = $request->session()->get('id');
  64. $order->plan_id = $plan->id;
  65. $order->cycle = $request->input('cycle');
  66. $order->trade_no = Helper::guid();
  67. $order->total_amount = $plan[$request->input('cycle')];
  68. if ($user->expired_at > time() && $order->plan_id !== $user->plan_id) {
  69. $order->type = 3;
  70. if (!(int)config('v2board.plan_is_update', 1)) abort(500, '目前不允许更改订阅,请联系管理员');
  71. $order->total_amount = $order->total_amount + (ceil(($user->expired_at - time()) / 86400) * config('v2board.plan_update_fee', 0.5) * 100);
  72. } else if ($user->expired_at > time() && $order->plan_id == $user->plan_id) {
  73. $order->type = 2;
  74. } else {
  75. $order->type = 1;
  76. }
  77. if ($user->invite_user_id) {
  78. $order->invite_user_id = $user->invite_user_id;
  79. $inviter = User::find($user->invite_user_id);
  80. if ($inviter) {
  81. if ($inviter->commission_rate) {
  82. $order->commission_balance = $order->total_amount * ($inviter->commission_rate / 100);
  83. } else {
  84. $order->commission_balance = $order->total_amount * (config('v2board.invite_commission', 10) / 100);
  85. }
  86. }
  87. }
  88. if (!$order->save()) {
  89. abort(500, '订单创建失败');
  90. }
  91. return response([
  92. 'data' => $order->trade_no
  93. ]);
  94. }
  95. public function checkout (Request $request) {
  96. $tradeNo = $request->input('trade_no');
  97. $method = $request->input('method');
  98. $order = Order::where('trade_no', $tradeNo)
  99. ->where('user_id', $request->session()->get('id'))
  100. ->where('status', 0)
  101. ->first();
  102. if (!$order) {
  103. abort(500, '订单不存在或以支付');
  104. }
  105. switch ($method) {
  106. // return type => 0: QRCode / 1: URL
  107. case 0:
  108. // alipayF2F
  109. if (!(int)config('v2board.alipay_enable')) {
  110. abort(500, '支付方式不可用');
  111. }
  112. return response([
  113. 'type' => 0,
  114. 'data' => $this->alipayF2F($tradeNo, $order->total_amount)
  115. ]);
  116. case 2:
  117. // stripeAlipay
  118. if (!(int)config('v2board.stripe_alipay_enable')) {
  119. abort(500, '支付方式不可用');
  120. }
  121. return response([
  122. 'type' => 1,
  123. 'data' => $this->stripeAlipay($order)
  124. ]);
  125. case 3:
  126. // stripeWepay
  127. if (!(int)config('v2board.stripe_wepay_enable')) {
  128. abort(500, '支付方式不可用');
  129. }
  130. return response([
  131. 'type' => 0,
  132. 'data' => $this->stripeWepay($order)
  133. ]);
  134. default:
  135. abort(500, '支付方式不存在');
  136. }
  137. }
  138. public function check (Request $request) {
  139. $tradeNo = $request->input('trade_no');
  140. $order = Order::where('trade_no', $tradeNo)
  141. ->where('user_id', $request->session()->get('id'))
  142. ->first();
  143. if (!$order) {
  144. abort(500, '订单不存在');
  145. }
  146. return response([
  147. 'data' => $order->status
  148. ]);
  149. }
  150. public function getPaymentMethod () {
  151. $data = [];
  152. if ((int)config('v2board.alipay_enable')) {
  153. $alipayF2F = new \StdClass();
  154. $alipayF2F->name = '支付宝';
  155. $alipayF2F->method = 0;
  156. $alipayF2F->icon = 'alipay';
  157. array_push($data, $alipayF2F);
  158. }
  159. if ((int)config('v2board.stripe_alipay_enable')) {
  160. $stripeAlipay = new \StdClass();
  161. $stripeAlipay->name = '支付宝';
  162. $stripeAlipay->method = 2;
  163. $stripeAlipay->icon = 'alipay';
  164. array_push($data, $stripeAlipay);
  165. }
  166. if ((int)config('v2board.stripe_wepay_enable')) {
  167. $stripeWepay = new \StdClass();
  168. $stripeWepay->name = '微信';
  169. $stripeWepay->method = 3;
  170. $stripeWepay->icon = 'wechat';
  171. array_push($data, $stripeWepay);
  172. }
  173. return response([
  174. 'data' => $data
  175. ]);
  176. }
  177. private function alipayF2F ($tradeNo, $totalAmount) {
  178. $gateway = Omnipay::create('Alipay_AopF2F');
  179. $gateway->setSignType('RSA2'); //RSA/RSA2
  180. $gateway->setAppId(config('v2board.alipay_appid'));
  181. $gateway->setPrivateKey(config('v2board.alipay_privkey')); // 可以是路径,也可以是密钥内容
  182. $gateway->setAlipayPublicKey(config('v2board.alipay_pubkey')); // 可以是路径,也可以是密钥内容
  183. $gateway->setNotifyUrl(config('v2board.app_url', env('APP_URL')) . '/api/v1/guest/order/alipayNotify');
  184. $request = $gateway->purchase();
  185. $request->setBizContent([
  186. 'subject' => config('v2board.app_name', 'V2Board') . ' - 订阅',
  187. 'out_trade_no' => $tradeNo,
  188. 'total_amount' => $totalAmount / 100
  189. ]);
  190. /** @var \Omnipay\Alipay\Responses\AopTradePreCreateResponse $response */
  191. $response = $request->send();
  192. $result = $response->getAlipayResponse();
  193. if ($result['code'] !== '10000') {
  194. abort(500, $result['sub_msg']);
  195. }
  196. // 获取收款二维码内容
  197. return $response->getQrCode();
  198. }
  199. private function stripeAlipay ($order) {
  200. $exchange = Helper::exchange('CNY', 'HKD');
  201. if (!$exchange) {
  202. abort(500, '货币转换超时,请稍后再试');
  203. }
  204. Stripe::setApiKey(config('v2board.stripe_sk_live'));
  205. $source = Source::create([
  206. 'amount' => floor($order->total_amount * $exchange),
  207. 'currency' => 'hkd',
  208. 'type' => 'alipay',
  209. 'redirect' => [
  210. 'return_url' => config('v2board.app_url', env('APP_URL')) . '/api/v1/guest/order/stripeReturn'
  211. ]
  212. ]);
  213. if (!$source['redirect']['url']) {
  214. abort(500, '支付网关请求失败');
  215. }
  216. $order->callback_no = $source['id'];
  217. if (!$order->save()) {
  218. abort(500, '订单更新失败');
  219. }
  220. return $source['redirect']['url'];
  221. }
  222. private function stripeWepay ($order) {
  223. $exchange = Helper::exchange('CNY', 'HKD');
  224. if (!$exchange) {
  225. abort(500, '货币转换超时,请稍后再试');
  226. }
  227. Stripe::setApiKey(config('v2board.stripe_sk_live'));
  228. $source = Source::create([
  229. 'amount' => floor($order->total_amount * $exchange),
  230. 'currency' => 'hkd',
  231. 'type' => 'wechat',
  232. 'redirect' => [
  233. 'return_url' => config('v2board.app_url', env('APP_URL')) . '/api/v1/guest/order/stripeReturn'
  234. ]
  235. ]);
  236. if (!$source['wechat']['qr_code_url']) {
  237. abort(500, '支付网关请求失败');
  238. }
  239. $order->callback_no = $source['id'];
  240. if (!$order->save()) {
  241. abort(500, '订单更新失败');
  242. }
  243. return $source['wechat']['qr_code_url'];
  244. }
  245. }