1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| public function actionIndex() { try { $user_id = FsRequest::userId('true');
if (!$user_id) { throw new Exception('请先登录', [], 201); }
$data = \Yii::$app->request->post(); if (empty($data)) { throw new Exception('参数不全', [], 201); } $openId = WeixinUser::find()->where(['ecuid' => $user_id])->select(['dingdang_mini'])->one(); if (!$openId) { throw new Exception('未绑定小程序', [], 201); } $payParams = [ 'appid' => $appid, 'mch_id' => $mch_id, 'nonce_str' => WxHelper::createNoncestr(32), 'sign' => '', 'body' => $data['body'], 'out_trade_no' => $data['orderId'], 'total_fee' => (int)($data['price'] * 100), 'openid' => $openId->dingdang_mini, 'spbill_create_ip' => $_SERVER['REMOTE_ADDR'], 'notify_url' => '支付回调', 'trade_type' => 'JSAPI', ];
$payParams['sign'] = WxHelper::paySign($payParams, $key); $xml = WxHelper::arrayToXml($payParams); $payUrl = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
$curl = new Curl(); $curl->setHeader('Content-Type', 'text/xml; charset=utf-8'); $curl->post($payUrl, $xml); if ($curl->error) { throw new Exception($curl->errorMessage, [], 201); } else { $result = WxHelper::xmlToArray($curl->response); if ($result['result_code'] == 'FAIL') { throw new Exception($result['err_code_des'], [], 201); } $payParamsResult = [ 'appId' => $this->appid, 'nonceStr' => WxHelper::createNoncestr(32), 'package' => 'prepay_id=' . $result['prepay_id'], 'paySign' => '', 'signType' => 'MD5', 'timeStamp' => (string)time() ]; $payParamsResult['paySign'] = WxHelper::paySign($payParamsResult, $this->key); FsResponse::jsonSuccess($payParamsResult); } } catch (Exception $e) { return FsResponse::jsonFaild($e->getCode(), $e->getMessage()); } }
|