Exemple #1
0
 /**
  * 托管退款操作
  * @param $identity_id 用户标示符
  * @param $orig_outer_trade_no 需要退款的订单号
  * @param $refund_amount 需要退款金额
  * @param $summary 退款说明
  * @return array
  */
 public static function hostingRefund($identity_id, $orig_outer_trade_no, $refund_amount, $summary = '投资失败退款')
 {
     $invest = SinaInvest::findOne(['out_trade_no' => $orig_outer_trade_no, 'identity_id' => $identity_id, 'money' => $refund_amount, 'status' => SinaInvest::STATUS_SUCCESS]);
     if (!$invest) {
         $return = array('errorNum' => '1', 'errorMsg' => '订单信息错误', 'data' => null);
         return $return;
     }
     $sina = new sina();
     $out_trade_no = self::build_order_no();
     $refund = $sina->create_hosting_refund($out_trade_no, $orig_outer_trade_no, $refund_amount, $summary);
     if (!$refund) {
         $return = array('errorNum' => '1', 'errorMsg' => '新浪托管接口失败', 'data' => null);
         return $return;
     }
     if ($refund['response_code'] == "APPLY_SUCCESS") {
         //提交信息成功
         if ($refund['refund_status'] == 'SUCCESS' || $refund['refund_status'] == 'PAY_FINISHED') {
             //退款成功
             $invest->refund_out_trade_no = $out_trade_no;
             $invest->refund_amount = $refund_amount;
             $invest->refund_summary = '中间账户退款';
             $invest->status = SinaInvest::STATUS_REFUND_SUCCESS;
             $invest->msg = '退款';
             if ($invest->save()) {
                 $return = array('errorNum' => '0', 'errorMsg' => 'success', 'data' => null);
                 return $return;
             }
             $return = array('errorNum' => '1', 'errorMsg' => '数据记录失败', 'data' => null);
             return $return;
         } else {
             $invest->refund_out_trade_no = $out_trade_no;
             $invest->refund_amount = $refund_amount;
             $invest->refund_summary = '中间账户退款';
             //                $invest->status = SinaInvest::STATUS_REFUND_ERROR;
             $invest->msg = '退款失败';
             if ($invest->save()) {
                 $return = array('errorNum' => '1', 'errorMsg' => $refund['refund_status'], 'data' => null);
                 return $return;
             }
             $return = array('errorNum' => '1', 'errorMsg' => '数据记录失败', 'data' => null);
             return $return;
         }
     } else {
         $invest->refund_out_trade_no = $out_trade_no;
         $invest->refund_amount = $refund_amount;
         $invest->refund_summary = '中间账户退款';
         //            $invest->status = SinaInvest::STATUS_REFUND_ERROR;
         $invest->msg = $refund['response_message'];
         if ($invest->save()) {
             $return = array('errorNum' => '1', 'errorMsg' => $refund['response_message'], 'data' => null);
             return $return;
         }
         $return = array('errorNum' => '1', 'errorMsg' => '数据记录失败', 'data' => null);
         return $return;
     }
 }
 /**
  * 用户退款
  * @return string
  * @throws NotFoundHttpException
  */
 public function actionRefund()
 {
     if (\App::$app->request->post()) {
         $uid = \App::$app->request->post()['Code']['uid'];
         $orig_outer_trade_no = \App::$app->request->post()['Code']['out_trade_no'];
         $refund_amount = \App::$app->request->post()['Code']['money'];
         $summary = \App::$app->request->post()['Code']['msg'];
         //获取新浪的账户余额
         $sina_balance = sinapay::querySinaBalance($uid);
         if ($sina_balance['errorNum'] == '0') {
             $available_balance = $sina_balance['data']['available_balance'];
         }
         //获取网站的账户信息
         $balance = Info::findOne(['member_id' => $uid]);
         $site_balabce = $balance['balance'];
         //调用新浪退款接口
         $sina = new sina();
         $out_trade_no = date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8) . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
         $refund = $sina->create_hosting_refund($out_trade_no, $orig_outer_trade_no, $refund_amount, $summary);
         if (!$refund) {
             throw new NotFoundHttpException('新浪接口错误');
         }
         if ($refund['response_code'] == "APPLY_SUCCESS") {
             //提交信息成功
             if ($refund['refund_status'] == 'SUCCESS' || $refund['refund_status'] == 'PAY_FINISHED') {
                 //更新账户信息
                 $sina_balance = sinapay::querySinaBalance($uid);
                 if ($sina_balance['errorNum'] == '0') {
                     $available_balance_now = $sina_balance['data']['available_balance'];
                 }
                 //获取网站的账户信息
                 $balance = Info::findOne(['member_id' => $uid]);
                 $site_balabce_now = $balance['balance'];
                 //返回数据
                 $data = array('available_balance' => $available_balance, 'site_balabce' => $site_balabce, 'available_balance_now' => $available_balance_now, 'site_balabce_now' => $site_balabce_now);
                 return $this->render('import', compact("data"));
             } else {
                 throw new NotFoundHttpException($refund['response_message']);
             }
         } else {
             throw new NotFoundHttpException($refund['response_message']);
         }
     } else {
         $data = array('available_balance' => '-1', 'site_balabce' => '-2', 'available_balance_now' => '-3', 'site_balabce_now' => '-4');
         return $this->render('import', compact("data"));
     }
 }