OrderController.php 8.5 KB

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