OrderController.php 8.7 KB

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