Payment.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\api\service;
  10. use app\api\model\Wxapp as WxappModel;
  11. use app\api\model\WxappPrepayId as WxappPrepayIdModel;
  12. use app\common\library\wechat\WxPay;
  13. use app\common\enum\OrderType as OrderTypeEnum;
  14. use app\common\enum\order\PayType as PayTypeEnum;
  15. class Payment
  16. {
  17. /**
  18. * 构建订单支付参数
  19. * @param $user
  20. * @param $order
  21. * @param $payType
  22. * @return array
  23. * @throws \app\common\exception\BaseException
  24. * @throws \think\exception\DbException
  25. */
  26. public static function orderPayment($user, $order, $payType)
  27. {
  28. if ($payType == PayTypeEnum::WECHAT) {
  29. return self::wechat(
  30. $user,
  31. $order['order_id'],
  32. $order['order_no'],
  33. $order['pay_price'],
  34. OrderTypeEnum::MASTER
  35. );
  36. }
  37. return [];
  38. }
  39. /**
  40. * 构建微信支付
  41. * @param \app\api\model\User $user
  42. * @param $orderId
  43. * @param $orderNo
  44. * @param $payPrice
  45. * @param int $orderType
  46. * @return array
  47. * @throws \app\common\exception\BaseException
  48. * @throws \think\exception\DbException
  49. */
  50. public static function wechat(
  51. $user,
  52. $orderId,
  53. $orderNo,
  54. $payPrice,
  55. $orderType = OrderTypeEnum::MASTER
  56. )
  57. {
  58. // 统一下单API
  59. $wxConfig = WxappModel::getWxappCache($user['wxapp_id']);
  60. $WxPay = new WxPay($wxConfig);
  61. $payment = $WxPay->unifiedorder($orderNo, $user['open_id'], $payPrice, $orderType);
  62. // 记录prepay_id
  63. $model = new WxappPrepayIdModel;
  64. $model->add($payment['prepay_id'], $orderId, $user['user_id'], $orderType);
  65. return $payment;
  66. }
  67. }