OrderController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. $order->commission_balance = $order->total_amount * (config('v2board.invite_commission', 10) / 100);
  80. }
  81. if (!$order->save()) {
  82. abort(500, '订单创建失败');
  83. }
  84. return response([
  85. 'data' => $order->trade_no
  86. ]);
  87. }
  88. public function checkout (Request $request) {
  89. $tradeNo = $request->input('trade_no');
  90. $method = $request->input('method');
  91. $order = Order::where('trade_no', $tradeNo)
  92. ->where('user_id', $request->session()->get('id'))
  93. ->where('status', 0)
  94. ->first();
  95. if (!$order) {
  96. abort(500, '订单不存在或以支付');
  97. }
  98. switch ($method) {
  99. // return type => 0: QRCode / 1: URL
  100. case 0:
  101. // alipayF2F
  102. if (!(int)config('v2board.alipay_enable')) {
  103. abort(500, '支付方式不可用');
  104. }
  105. return response([
  106. 'type' => 0,
  107. 'data' => $this->alipayF2F($tradeNo, $order->total_amount)
  108. ]);
  109. case 2:
  110. // stripeAlipay
  111. if (!(int)config('v2board.stripe_alipay_enable')) {
  112. abort(500, '支付方式不可用');
  113. }
  114. return response([
  115. 'type' => 1,
  116. 'data' => $this->stripeAlipay($order)
  117. ]);
  118. case 3:
  119. // stripeWepay
  120. if (!(int)config('v2board.stripe_wepay_enable')) {
  121. abort(500, '支付方式不可用');
  122. }
  123. return response([
  124. 'type' => 0,
  125. 'data' => $this->stripeWepay($order)
  126. ]);
  127. default:
  128. abort(500, '支付方式不存在');
  129. }
  130. }
  131. public function check (Request $request) {
  132. $tradeNo = $request->input('trade_no');
  133. $order = Order::where('trade_no', $tradeNo)
  134. ->where('user_id', $request->session()->get('id'))
  135. ->first();
  136. if (!$order) {
  137. abort(500, '订单不存在');
  138. }
  139. return response([
  140. 'data' => $order->status
  141. ]);
  142. }
  143. public function getPaymentMethod () {
  144. $data = [];
  145. if ((int)config('v2board.alipay_enable')) {
  146. $alipayF2F = new \StdClass();
  147. $alipayF2F->name = '支付宝';
  148. $alipayF2F->method = 0;
  149. $alipayF2F->icon = 'alipay';
  150. array_push($data, $alipayF2F);
  151. }
  152. if ((int)config('v2board.stripe_alipay_enable')) {
  153. $stripeAlipay = new \StdClass();
  154. $stripeAlipay->name = '支付宝';
  155. $stripeAlipay->method = 2;
  156. $stripeAlipay->icon = 'alipay';
  157. array_push($data, $stripeAlipay);
  158. }
  159. if ((int)config('v2board.stripe_wepay_enable')) {
  160. $stripeWepay = new \StdClass();
  161. $stripeWepay->name = '微信';
  162. $stripeWepay->method = 3;
  163. $stripeWepay->icon = 'wechat';
  164. array_push($data, $stripeWepay);
  165. }
  166. return response([
  167. 'data' => $data
  168. ]);
  169. }
  170. private function alipayF2F ($tradeNo, $totalAmount) {
  171. $gateway = Omnipay::create('Alipay_AopF2F');
  172. $gateway->setSignType('RSA2'); //RSA/RSA2
  173. $gateway->setAppId(config('v2board.alipay_appid'));
  174. $gateway->setPrivateKey(config('v2board.alipay_privkey')); // 可以是路径,也可以是密钥内容
  175. $gateway->setAlipayPublicKey(config('v2board.alipay_pubkey')); // 可以是路径,也可以是密钥内容
  176. $gateway->setNotifyUrl(config('v2board.app_url', env('APP_URL')) . '/api/v1/guest/order/alipayNotify');
  177. $request = $gateway->purchase();
  178. $request->setBizContent([
  179. 'subject' => config('v2board.app_name', 'V2Board') . ' - 订阅',
  180. 'out_trade_no' => $tradeNo,
  181. 'total_amount' => $totalAmount / 100
  182. ]);
  183. /** @var \Omnipay\Alipay\Responses\AopTradePreCreateResponse $response */
  184. $response = $request->send();
  185. $result = $response->getAlipayResponse();
  186. if ($result['code'] !== '10000') {
  187. abort(500, $result['sub_msg']);
  188. }
  189. // 获取收款二维码内容
  190. return $response->getQrCode();
  191. }
  192. private function stripeAlipay ($order) {
  193. $exchange = Helper::exchange('CNY', 'HKD');
  194. if (!$exchange) {
  195. abort(500, '货币转换超时,请稍后再试');
  196. }
  197. Stripe::setApiKey(config('v2board.stripe_sk_live'));
  198. $source = Source::create([
  199. 'amount' => floor($order->total_amount * $exchange),
  200. 'currency' => 'hkd',
  201. 'type' => 'alipay',
  202. 'redirect' => [
  203. 'return_url' => config('v2board.app_url', env('APP_URL')) . '/api/v1/guest/order/stripeReturn'
  204. ]
  205. ]);
  206. if (!$source['redirect']['url']) {
  207. abort(500, '支付网关请求失败');
  208. }
  209. $order->callback_no = $source['id'];
  210. if (!$order->save()) {
  211. abort(500, '订单更新失败');
  212. }
  213. return $source['redirect']['url'];
  214. }
  215. private function stripeWepay ($order) {
  216. $exchange = Helper::exchange('CNY', 'HKD');
  217. if (!$exchange) {
  218. abort(500, '货币转换超时,请稍后再试');
  219. }
  220. Stripe::setApiKey(config('v2board.stripe_sk_live'));
  221. $source = Source::create([
  222. 'amount' => floor($order->total_amount * $exchange),
  223. 'currency' => 'hkd',
  224. 'type' => 'wechat',
  225. 'redirect' => [
  226. 'return_url' => config('v2board.app_url', env('APP_URL')) . '/api/v1/guest/order/stripeReturn'
  227. ]
  228. ]);
  229. if (!$source['wechat']['qr_code_url']) {
  230. abort(500, '支付网关请求失败');
  231. }
  232. $order->callback_no = $source['id'];
  233. if (!$order->save()) {
  234. abort(500, '订单更新失败');
  235. }
  236. return $source['wechat']['qr_code_url'];
  237. }
  238. }