NotifyUrlController.php 11 KB

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