Payment.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\api\service;
  13. use app\api\model\Wxapp as WxappModel;
  14. use app\api\service\User as UserService;
  15. use app\common\enum\OrderType as OrderTypeEnum;
  16. use app\common\enum\order\PayType as OrderPayTypeEnum;
  17. use app\common\library\wechat\WxPay;
  18. use app\common\exception\BaseException;
  19. use app\common\service\BaseService;
  20. /**
  21. * 订单支付服务类
  22. * Class Payment
  23. * @package app\api\service
  24. */
  25. class Payment extends BaseService
  26. {
  27. /**
  28. * 构建订单支付参数
  29. * @param $order
  30. * @param $payType
  31. * @return array
  32. * @throws BaseException
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public static function orderPayment($order, $payType)
  38. {
  39. if ($payType == OrderPayTypeEnum::WECHAT) {
  40. return self::wechat(
  41. $order['order_id'],
  42. $order['order_no'],
  43. $order['pay_price'],
  44. OrderTypeEnum::ORDER
  45. );
  46. }
  47. return [];
  48. }
  49. /**
  50. * 构建微信支付
  51. * @param $orderId
  52. * @param $orderNo
  53. * @param $payPrice
  54. * @param $orderType
  55. * @return array
  56. * @throws BaseException
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\DbException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. */
  61. public static function wechat(
  62. $orderId,
  63. $orderNo,
  64. $payPrice,
  65. $orderType = OrderTypeEnum::ORDER
  66. )
  67. {
  68. // 获取当前用户信息
  69. $userInfo = UserService::getCurrentLoginUser(true);
  70. // 获取第三方用户信息(微信)
  71. $oauth = UserService::getOauth($userInfo['user_id'], 'MP-WEIXIN');
  72. empty($oauth) && throwError('没有找到第三方用户信息oauth');
  73. // 统一下单API
  74. $WxPay = new WxPay(static::getWxConfig());
  75. return $WxPay->unifiedorder($orderNo, $oauth['oauth_id'], $payPrice, $orderType);
  76. }
  77. /**
  78. * 获取微信支付配置
  79. * @return array
  80. * @throws BaseException
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\DbException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. */
  85. private static function getWxConfig()
  86. {
  87. return WxappModel::getWxappCache(getStoreId());
  88. }
  89. }