PunishmentOrderController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. *
  4. *
  5. * @category xxx
  6. * @package PSR
  7. * @subpackage Documentation\API
  8. * @author xxx <xxx@xxx.com>
  9. * @license GPL https://xxx.com
  10. * @link https://xxx.com
  11. * @ctime: 2020/5/19 9:33
  12. */
  13. namespace App\Http\Controllers\V1;
  14. use App\Http\Requests\PunishmentOrderRequest;
  15. use App\Models\Auth;
  16. use App\Models\PunishmentOrder;
  17. use App\Repositories\PunishmentOrderRepository;
  18. use App\Transformers\PunishmentOrdersTransformer;
  19. use App\Transformers\PunishmentOrderTransformer;
  20. use EasyWeChat\Factory;
  21. use Illuminate\Support\Facades\Log;
  22. use function EasyWeChat\Kernel\Support\generate_sign;
  23. class PunishmentOrderController extends BaseController
  24. {
  25. public function index(PunishmentOrderRepository $punishmentOrderRepository)
  26. {
  27. try {
  28. $user_id = $this->user->id;
  29. $punishmentOrders = $punishmentOrderRepository->getAllByUserId($user_id);
  30. return $this->response->paginator($punishmentOrders, PunishmentOrdersTransformer::class);
  31. } catch (\Exception $exception) {
  32. return $this->exception($exception);
  33. }
  34. }
  35. public function show(PunishmentOrderRequest $punishmentOrderRequest, PunishmentOrderRepository $punishmentOrderRepository)
  36. {
  37. try {
  38. $no = $punishmentOrderRequest->get('no');
  39. $punishmentOrder = $punishmentOrderRepository->getModelByNo($no);
  40. if (empty($punishmentOrder)) return $this->errorNoValidation('找不到该罚单');
  41. return $this->response->item($punishmentOrder, PunishmentOrderTransformer::class);
  42. } catch (\Exception $exception) {
  43. return $this->exception($exception);
  44. }
  45. }
  46. public function pay(PunishmentOrderRequest $punishmentOrderRequest, PunishmentOrderRepository $punishmentOrderRepository)
  47. {
  48. try {
  49. $no = $punishmentOrderRequest->get('no');
  50. $punishmentOrder = $punishmentOrderRepository->getModelByNo($no);
  51. if (empty($punishmentOrder)) return $this->errorNoValidation('找不到该罚单');
  52. $payOk = $punishmentOrderRepository->isPay($no);
  53. if (!empty($payOk)) return $this->errorNoValidation('此罚单已支付');
  54. $user_id = $this->user->id;
  55. $userauth = userAuthinfo(self::$MERCHANT, $user_id);
  56. if ($userauth['code'] != 1) {
  57. return $this->errorNoValidation('用户信息有误');
  58. }
  59. switch (self::$SOURCE_TYPE) {
  60. case self::SOURCE_TYPE_WECHAT:
  61. $payment = Factory::payment(wechat_pay_config(self::$MERCHANT)); // 微信支付
  62. $result = $payment->order->unify([
  63. 'body' => "用户支付罚单-" . self::$MERCHANT['wxapp_name'],
  64. 'out_trade_no' => $punishmentOrder->no,
  65. 'trade_type' => 'JSAPI', // 必须为JSAPI
  66. 'openid' => $userauth['data']['credential'], // 这里的openid为付款人的openid
  67. 'total_fee' => wechat_fee($punishmentOrder->pay_money), // 总价
  68. 'attach' => makeNoTag(PunishmentOrder::NO_TAG)
  69. ]);
  70. if ($result['return_code'] === 'FAIL') return $this->errorNoValidation('下单失败');
  71. // 如果成功生成统一下单的订单,那么进行二次签名
  72. if ($result['result_code'] === 'FAIL') {
  73. //判断是否重复下单
  74. if ($result['err_code'] === 'INVALID_REQUEST') {
  75. $punishmentOrder->no = PunishmentOrder::makeNo(self::$ORDER_TAG);
  76. $punishmentOrder->save();
  77. $result = $payment->order->unify([
  78. 'body' => "用户支付罚单-" . self::$MERCHANT['wxapp_name'],
  79. 'out_trade_no' => $punishmentOrder->no,
  80. 'trade_type' => 'JSAPI', // 必须为JSAPI
  81. 'openid' => $userauth['data']['credential'], // 这里的openid为付款人的openid
  82. 'total_fee' => wechat_fee($punishmentOrder->pay_money), // 总价
  83. 'attach' => makeNoTag(PunishmentOrder::NO_TAG)
  84. ]);
  85. } else {
  86. return $this->errorNoValidation('下单失败');
  87. }
  88. }
  89. // 二次签名的参数必须与下面相同
  90. $params = [
  91. 'appId' => $userauth['data']['identifier'],
  92. 'timeStamp' => time(),
  93. 'nonceStr' => $result['nonce_str'],
  94. 'package' => 'prepay_id=' . $result['prepay_id'],
  95. 'signType' => 'MD5',
  96. ];
  97. // self::$MERCHANT['pay_key']为商户的key
  98. $params['paySign'] = generate_sign($params, self::$MERCHANT['pay_key']);
  99. $response = $params;
  100. $response['order_no'] = $punishmentOrder->no;
  101. return $this->response->array($response);
  102. break;
  103. case self::SOURCE_TYPE_ALIPAY:
  104. $result = alipay_mini_pay(self::$MERCHANT, "用户支付罚单-" . self::$MERCHANT['wxapp_name'], $punishmentOrder->no, $punishmentOrder->pay_money, $userauth['data']['credential']);
  105. if ($result['code'] != 1) {
  106. //客服修改支付价格后重新生成订单
  107. if($result['data']->subCode=='ACQ.CONTEXT_INCONSISTENT'){ //信息被篡改判断
  108. $punishmentOrder->no = PunishmentOrder::makeNo(self::$ORDER_TAG);
  109. $punishmentOrder->save();
  110. $result = alipay_mini_pay(self::$MERCHANT, "用户支付罚单-" . self::$MERCHANT['wxapp_name'], $punishmentOrder->no, $punishmentOrder->pay_money, $userauth['data']['credential']);
  111. if ($result['code'] != 1) {
  112. return $this->errorNoValidation($result['msg']);
  113. }
  114. }else{
  115. return $this->errorNoValidation("调用失败,原因:" . $result->msg . "," . $result->subMsg);
  116. }
  117. }
  118. $response['tradeNo'] = $result['data']->tradeNo; //获取支付宝订单号
  119. return $this->response->array($response);
  120. break;
  121. default:
  122. return $this->errorNoValidation('支付方式错误');
  123. }
  124. } catch (\Exception $exception) {
  125. return $this->exception($exception);
  126. }
  127. }
  128. }