NotifyUrlController.php 11 KB

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