123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- /**
- *
- *
- * @category xxx
- * @package PSR
- * @subpackage Documentation\API
- * @author xxx <xxx@xxx.com>
- * @license GPL https://xxx.com
- * @link https://xxx.com
- * @ctime: 2020/5/19 9:33
- */
- namespace App\Http\Controllers\V1;
- use App\Http\Requests\PunishmentOrderRequest;
- use App\Models\Auth;
- use App\Models\PunishmentOrder;
- use App\Repositories\PunishmentOrderRepository;
- use App\Transformers\PunishmentOrdersTransformer;
- use App\Transformers\PunishmentOrderTransformer;
- use EasyWeChat\Factory;
- use Illuminate\Support\Facades\Log;
- use function EasyWeChat\Kernel\Support\generate_sign;
- class PunishmentOrderController extends BaseController
- {
- public function index(PunishmentOrderRepository $punishmentOrderRepository)
- {
- try {
- $user_id = $this->user->id;
- $punishmentOrders = $punishmentOrderRepository->getAllByUserId($user_id);
- return $this->response->paginator($punishmentOrders, PunishmentOrdersTransformer::class);
- } catch (\Exception $exception) {
- return $this->exception($exception);
- }
- }
- public function show(PunishmentOrderRequest $punishmentOrderRequest, PunishmentOrderRepository $punishmentOrderRepository)
- {
- try {
- $no = $punishmentOrderRequest->get('no');
- $punishmentOrder = $punishmentOrderRepository->getModelByNo($no);
- if (empty($punishmentOrder)) return $this->errorNoValidation('找不到该罚单');
- return $this->response->item($punishmentOrder, PunishmentOrderTransformer::class);
- } catch (\Exception $exception) {
- return $this->exception($exception);
- }
- }
- public function pay(PunishmentOrderRequest $punishmentOrderRequest, PunishmentOrderRepository $punishmentOrderRepository)
- {
- try {
- $no = $punishmentOrderRequest->get('no');
- $punishmentOrder = $punishmentOrderRepository->getModelByNo($no);
- if (empty($punishmentOrder)) return $this->errorNoValidation('找不到该罚单');
- $payOk = $punishmentOrderRepository->isPay($no);
- if (!empty($payOk)) return $this->errorNoValidation('此罚单已支付');
- $user_id = $this->user->id;
- $userauth = userAuthinfo(self::$MERCHANT, $user_id);
- if ($userauth['code'] != 1) {
- return $this->errorNoValidation('用户信息有误');
- }
- switch (self::$SOURCE_TYPE) {
- case self::SOURCE_TYPE_WECHAT:
- $payment = Factory::payment(wechat_pay_config(self::$MERCHANT)); // 微信支付
- $result = $payment->order->unify([
- 'body' => "用户支付罚单-" . self::$MERCHANT['wxapp_name'],
- 'out_trade_no' => $punishmentOrder->no,
- 'trade_type' => 'JSAPI', // 必须为JSAPI
- 'openid' => $userauth['data']['credential'], // 这里的openid为付款人的openid
- 'total_fee' => wechat_fee($punishmentOrder->pay_money), // 总价
- 'attach' => makeNoTag(PunishmentOrder::NO_TAG)
- ]);
- if ($result['return_code'] === 'FAIL') return $this->errorNoValidation('下单失败');
- // 如果成功生成统一下单的订单,那么进行二次签名
- if ($result['result_code'] === 'FAIL') {
- //判断是否重复下单
- if ($result['err_code'] === 'INVALID_REQUEST') {
- $punishmentOrder->no = PunishmentOrder::makeNo(self::$ORDER_TAG);
- $punishmentOrder->save();
- $result = $payment->order->unify([
- 'body' => "用户支付罚单-" . self::$MERCHANT['wxapp_name'],
- 'out_trade_no' => $punishmentOrder->no,
- 'trade_type' => 'JSAPI', // 必须为JSAPI
- 'openid' => $userauth['data']['credential'], // 这里的openid为付款人的openid
- 'total_fee' => wechat_fee($punishmentOrder->pay_money), // 总价
- 'attach' => makeNoTag(PunishmentOrder::NO_TAG)
- ]);
- } else {
- return $this->errorNoValidation('下单失败');
- }
- }
- // 二次签名的参数必须与下面相同
- $params = [
- 'appId' => $userauth['data']['identifier'],
- 'timeStamp' => time(),
- 'nonceStr' => $result['nonce_str'],
- 'package' => 'prepay_id=' . $result['prepay_id'],
- 'signType' => 'MD5',
- ];
- // self::$MERCHANT['pay_key']为商户的key
- $params['paySign'] = generate_sign($params, self::$MERCHANT['pay_key']);
- $response = $params;
- $response['order_no'] = $punishmentOrder->no;
- return $this->response->array($response);
- break;
- case self::SOURCE_TYPE_ALIPAY:
- $result = alipay_mini_pay(self::$MERCHANT, "用户支付罚单-" . self::$MERCHANT['wxapp_name'], $punishmentOrder->no, $punishmentOrder->pay_money, $userauth['data']['credential']);
- if ($result['code'] != 1) {
- //客服修改支付价格后重新生成订单
- if($result['data']->subCode=='ACQ.CONTEXT_INCONSISTENT'){ //信息被篡改判断
- $punishmentOrder->no = PunishmentOrder::makeNo(self::$ORDER_TAG);
- $punishmentOrder->save();
- $result = alipay_mini_pay(self::$MERCHANT, "用户支付罚单-" . self::$MERCHANT['wxapp_name'], $punishmentOrder->no, $punishmentOrder->pay_money, $userauth['data']['credential']);
- if ($result['code'] != 1) {
- return $this->errorNoValidation($result['msg']);
- }
- }else{
- return $this->errorNoValidation("调用失败,原因:" . $result->msg . "," . $result->subMsg);
- }
- }
- $response['tradeNo'] = $result['data']->tradeNo; //获取支付宝订单号
- return $this->response->array($response);
- break;
- default:
- return $this->errorNoValidation('支付方式错误');
- }
- } catch (\Exception $exception) {
- return $this->exception($exception);
- }
- }
- }
|