NotifyUrlController.php 10 KB

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