NotifyUrlController.php 11 KB

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