/** * 输出还款html代码 * * @param string $order_id 订单编号 * @param number $issue 期号,0表示支付首付,其它表示指定账单,仅配合month=1使用 * @param number $month 还款月份,0表示一次性还清,其它表示还款的期数 */ public function alipay($order_id, $issue = 0, $month = 1) { // 获取并验证订单 $user = get_user() or $this->error('请先登录!', '/login'); $orderModel = new \Loan\Model\OrderModel(); $billModel = new \Loan\Model\BillModel(); $order = $orderModel->field('status,first_money,is_allow_prepayment')->where(['uid' => $user['uid'], 'id' => $order_id])->find() or $this->error('找不到此订单信息!'); $issue = intval($issue); $month = intval($month); if ($month == 1) { if ($issue == 0) { // 正常支付首付 $order['status'] == $orderModel::STATUS_UNPAY or $this->error('此订单状态当前不允许支付首付!'); $subject = '指尖分期-支付首付'; $money = $order['first_money']; } else { // 正常支付指定账单 if ($order['status'] == $orderModel::STATUS_UNPAY) { $this->error('请先支付首付!'); } in_array($order['status'], $orderModel->allowRepayStatus()) or $this->error('当前订单状态不允许执行还款!'); // 获取并验证账单信息 $bill = M('loan_bill')->field('issue,money_total,return_money,status')->where(['order_id' => $order_id, 'issue' => $issue])->find() or $this->error('找不到此账单信息!'); $bill['status'] == 0 or $this->error('此账单当前不为待付款状态!'); $bill['money_total'] > 0 or $this->error('此账单无需支付!'); $subject = '指尖分期-' . "支付第{$issue}期"; $money = $bill['money_total'] - $bill['return_money']; } } elseif ($month == 0) { // 一次性还清 $subject = '指尖分期-' . "一次性还款"; $repay = $billModel->getRepay($order_id) or $this->error($billModel->getError()); $money = $repay['money_total']; } elseif ($month > 1) { // 提前还$month期 $subject = '指尖分期-' . "提前还{$month}期"; $repay = $billModel->getRepay($order_id, $month) or $this->error($billModel->getError()); $money = $repay['money_total']; } else { $this->error('还款期数异常!'); } $alipayModel = M('loan_pay_alipay'); $pay_id = $alipayModel->add(['created' => time(), 'order_id' => $order_id, 'issue' => $issue, 'month' => $month, 'money' => $money, 'is_notify' => 0, 'is_action' => 0]); // 确认生成支付表单 $body = '订单号:' . $order_id; $notify_url = U('/pay/alipay_notify/' . $pay_id, null, true, $_SERVER['HTTP_HOST']); $return_url = U('/pay/alipay_return/' . $pay_id, null, true, $_SERVER['HTTP_HOST']); $show_url = U('/user/order/' . $order_id . '@fenqi'); $alipay = new \Common\Util\Alipay\Alipay(); header('Location: ' . $alipay->pay($notify_url, $return_url, $order_id . '-' . $pay_id, $subject, $body, $show_url, $money)); }
/** * 自动还款(从个人钱包扣款) * * @param int $uid 用户编号 * @param number $money 最大还款金额,默认为null,表示返回总共所需还款金额 * * @return 实际还款金额 */ public function autoRepay($uid, $money = null, $trade_no = '') { $orderModel = new \Loan\Model\OrderModel(); $where_order_status = implode(',', $orderModel->allowRepayStatus(true)); $where_order = 'is_support_job=1 and uid =' . intval($uid) . ' and status in (' . $where_order_status . ')'; $bills = $this->where(['order_id' => ['exp', ' in ( select id from zj_loan_order where ' . $where_order . ')'], 'issue' => ['gt', 0], 'status' => 0])->order('return_limit_time ASC')->select(); // 返回总共所需的金额 if ($money === null) { $money = 0; foreach ($bills as $bill) { $money += $bill['money_total'] - $bill['return_money']; } return $money; } $total_pay_money = 0; $money_remain = $money; // 处理订单 foreach ($bills as $bill) { if ($money_remain <= 0) { break; } // 验证统计字段是否正常 if (bccomp($bill['money_total'], $bill['money_principal'] + $bill['money_fee'] + $bill['money_delay'], 2) != 0) { system_warn('检测到账单money_total字段异常!'); continue; } $pay_money = min($money_remain, $bill['money_total'] - $bill['return_money']); $ret = $this->repay($bill['order_id'], $bill['issue'], $pay_money, 3, $trade_no); if (!$ret) { system_warn('自动还款失败:' . $this->error); continue; } $money_remain -= $pay_money; $total_pay_money += $pay_money; } return $total_pay_money; }