NotifyUrlController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace App\Http\Api;
  3. use Alipay\EasySDK\Kernel\Factory as Alipay;
  4. use EasyWeChat\Factory;
  5. use EasyWeChat\Kernel\Exceptions\Exception;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Facades\Request;
  9. use Illuminate\Support\Str;
  10. class NotifyUrlController extends HttpBaseController
  11. {
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. $this->setWebsite(__NAMESPACE__);
  16. }
  17. public function alipay()
  18. {
  19. Log::info('支付宝支付---通知开始');
  20. //1. 设置参数(全局只需设置一次)
  21. Alipay::setOptions($this->getOptions());
  22. //2. 发起API调用
  23. $parameters = Request::all();
  24. try {
  25. if (Alipay::payment()->common()->verifyNotify($parameters)) {
  26. //更新订单
  27. $orderData = [
  28. 'order_status' => 2,
  29. 'pay_amount_total' => $parameters['total_amount'], //实际付款金额 支付接口,返回的支付金额,仅做参考
  30. 'pay_channel' => 3, //订单支付渠道 1 微信小程序 2 微信公众号 3支付宝
  31. 'out_trade_no' => $parameters['out_trade_no'], //第三方支付流水号
  32. 'pay_time' => time(), //付款时间
  33. 'pay_info' => json_encode($parameters),
  34. ];
  35. return $this->updateOrder($orderData['out_trade_no'], $orderData);
  36. } else {
  37. Log::info('支付宝支付---失败,签名验证失败!');
  38. return 'fail';
  39. }
  40. } catch (\Exception $e) {
  41. Log::info('支付宝支付---失败!' . $e->getMessage());
  42. return 'fail';
  43. }
  44. }
  45. /**
  46. * 微信公众号-支付成功后的回调
  47. */
  48. public function wechatpay()
  49. {
  50. Log::info('微信支付---通知开始');
  51. $config = config('easywechat.pay.default');
  52. $app = Factory::payment($config);
  53. try {
  54. $response = $app->handlePaidNotify(function ($message, $fail) {
  55. //判断该订单是否存在
  56. if ($message['result_code'] == 'SUCCESS' && $message['return_code'] == 'SUCCESS') {
  57. //更新订单
  58. $orderData = [
  59. 'order_status' => 2,
  60. 'pay_amount_total' => $message['total_fee'], //实际付款金额 支付接口,返回的支付金额,仅做参考
  61. 'pay_channel' => 2, //订单支付渠道 1 微信小程序 2 微信公众号 3支付宝
  62. 'out_trade_no' => $message['transaction_id'], //第三方支付流水号
  63. 'pay_time' => time(), //付款时间
  64. 'pay_info' => json_encode($message),
  65. ];
  66. return $this->updateOrder($message['out_trade_no'], $orderData);
  67. } else {
  68. return $fail('通信失败,请稍后再通知我');
  69. }
  70. });
  71. return $response->send();
  72. } catch (Exception $e) {
  73. Log::error('微信支付---支付失败 ==' . $e->getMessage());
  74. return 'failed';
  75. }
  76. }
  77. /**
  78. * 微信小程序-支付成功后的回调
  79. */
  80. public function wxpay()
  81. {
  82. Log::info('微信小程序支付回调开始了!');
  83. $config = config('wechat.payment.default');
  84. $app = Factory::payment($config);
  85. $response = $app->handlePaidNotify(function ($message, $fail) {
  86. //判断该订单是否存在
  87. if ($message['result_code'] == 'SUCCESS' && $message['return_code'] == 'SUCCESS') {
  88. Log::info('微信小程序支付回调成功!', $message);
  89. //更新订单
  90. $orderData = [
  91. 'order_status' => 2,
  92. 'pay_amount_total' => $message['total_fee'], //实际付款金额 支付接口,返回的支付金额,仅做参考
  93. 'pay_channel' => 1, //订单支付渠道 1 微信小程序 2 微信公众号 3支付宝
  94. 'out_trade_no' => $message['transaction_id'], //第三方支付流水号
  95. 'pay_time' => time(), //付款时间
  96. 'pay_info' => json_encode($message),
  97. ];
  98. return $this->updateOrder($message['out_trade_no'], $orderData);
  99. } else {
  100. Log::error(__FUNCTION__ . '微信小程序支付回调失败!', []);
  101. return $fail('通信失败,请稍后再通知我');
  102. }
  103. });
  104. return $response->send();
  105. }
  106. ############################################################################################################
  107. ############################################################################################################
  108. ############################################################################################################
  109. /**
  110. * @param $out_trade_no
  111. * @param $orderData
  112. * @return string|null
  113. */
  114. private function updateOrder($out_trade_no, $orderData): ?string
  115. {
  116. $logic_sign = substr($out_trade_no, 0, 2);
  117. switch ($logic_sign) {
  118. case 'GO': // 服务商品订单
  119. return $this->updateOrderTable($out_trade_no, $orderData);
  120. case 'CO': // 服务套餐订单
  121. return $this->updateComboOrder($out_trade_no, $orderData);
  122. default:
  123. Log::error("======= 产品类型对应的订单服务不存在 ========");
  124. return null;
  125. }
  126. }
  127. /**
  128. * @param $out_trade_no
  129. * @param $orderData
  130. * @return string
  131. */
  132. private function updateOrderTable($out_trade_no, $orderData): string
  133. {
  134. $orderInfo = DB::table('order_product')->where('order_no', $out_trade_no)->where("order_status", 1)->first();
  135. if ($orderInfo) {
  136. //更新订单状态信息
  137. $is_success = DB::transaction(function () use ($orderInfo, $orderData) {
  138. $is_success = DB::table('order_product')->where('id', $orderInfo->id)->update($orderData);
  139. if ($is_success) {
  140. // 更新产品
  141. $this->updateUserProduct($orderInfo->user_id, $orderInfo->product_id, $orderInfo->product_sku);
  142. Log::info('回调-服务商品订单支付成功!');
  143. return true;
  144. } else {
  145. Log::info('回调-电商订单支付失败!');
  146. return false;
  147. }
  148. });
  149. if ($is_success) {
  150. return 'success';
  151. } else {
  152. return 'fail';
  153. }
  154. } else {
  155. Log::info('回调-订单不存在!-订单号:' . $out_trade_no);
  156. return 'fail';
  157. }
  158. }
  159. /**
  160. * @param $out_trade_no
  161. * @param $orderData
  162. * @return string
  163. */
  164. private function updateComboOrder($out_trade_no, $orderData): string
  165. {
  166. $orderInfo = DB::table('order_combo')->where('order_no', $out_trade_no)->where("order_status", 1)->first();
  167. if ($orderInfo) {
  168. // 有效期
  169. $orderData['expiry_time'] = time() + $orderInfo->expiry_time * 24 * 3600;
  170. //更新订单信息
  171. $is_success = DB::transaction(function () use ($orderInfo, $orderData) {
  172. $is_success = DB::table('order_combo')->where('id', $orderInfo['id'])->update($orderData);
  173. if ($is_success) {
  174. $productIds = $orderInfo['product_ids'];
  175. foreach ($productIds as $productId) {
  176. // 更新产品
  177. $this->updateUserProduct($orderInfo['user_id'], $productId, 4);
  178. }
  179. Log::info('回调-服务商品订单支付成功!');
  180. return true;
  181. } else {
  182. Log::info('回调-电商订单支付失败!');
  183. return false;
  184. }
  185. });
  186. if ($is_success) {
  187. return 'success';
  188. } else {
  189. return 'fail';
  190. }
  191. } else {
  192. Log::info('回调-订单不存在!-订单号:' . $out_trade_no);
  193. return 'fail';
  194. }
  195. }
  196. /**
  197. * @param $userId
  198. * @param $productId
  199. * @param $product_sku
  200. * @return void
  201. */
  202. private function updateUserProduct($userId, $productId, $product_sku): void
  203. {
  204. $validity_type = 1;
  205. if ($product_sku == 1) { // 月价格
  206. $endTime = 90 * 24 * 3600;
  207. } elseif ($product_sku == 2) { // 半年价格
  208. $endTime = 183 * 24 * 3600;
  209. } elseif ($product_sku == 3) { // 一年价格
  210. $endTime = 365 * 24 * 3600;
  211. } elseif ($product_sku == 4) { // 永久有效
  212. $validity_type = 2;
  213. $endTime = 0;
  214. } else if ($product_sku == 5) { // 7 天有效
  215. $endTime = 7 * 24 * 3600;
  216. } else {
  217. $endTime = 0;
  218. }
  219. // 添加商品
  220. $isExist = DB::table('user_buy_bill')->where(['user_id' => $userId, 'product_id' => $productId])->first();
  221. if ($isExist) {
  222. // validity_type 1 时间限制 2 永久有效
  223. if ($isExist->validity_type == 2) {
  224. // 如果原来已经是永久会员,则不变
  225. // $validity_type = 2;
  226. // $endTime = 0;
  227. } else {
  228. $validity_end_time = $isExist->validity_end_time;
  229. if ($validity_end_time < time()) {
  230. $validity_end_time = time();
  231. }
  232. $endTime = $endTime + $validity_end_time;
  233. DB::table('user_buy_bill')->where('id', $isExist->id)->update(['validity_type' => $validity_type, 'validity_end_time' => $endTime]);
  234. }
  235. } else {
  236. if ($validity_type == 1) {
  237. $endTime = $endTime + time();
  238. } else {
  239. $endTime = 0;
  240. }
  241. DB::table('user_buy_bill')->insert([
  242. 'user_id' => $userId,
  243. 'product_id' => $productId,
  244. 'validity_type' => $validity_type,
  245. 'validity_end_time' => $endTime,
  246. 'mid' => Str::random(12),
  247. 'created_at' => time(),
  248. 'updated_at' => time(),
  249. ]);
  250. }
  251. }
  252. }