getConfig($mall_id); } $this->config = $cfg; } /** * 汇聚支付:代付 * @param $paymentTransfer * @param $userInfo * @return bool * @throws Exception * @throws \yii\db\Exception */ protected function format($bank_account){ $s = $bank_account; $count = strlen($s); $bank_account = ''; $regex ="/^[0-9]+$/i"; for ($i=0;$i<$count;$i++){ if (preg_match($regex, $s[$i])) { $bank_account.=$s[$i]; } } return $bank_account; } public function transfer($paymentTransfer, $userInfo): bool { $cash_info = $paymentTransfer->getCashInfo(); $cash_extra = Json::decode($cash_info->extra, true); //使用提现表中的订单号: $order_no = $cash_info->order_no; $single_pay_log = PaidSinglePayLog::findOne(['order_no' => $order_no, 'status' => [PaidSinglePayLog::STATUS_SUCCESS, PaidSinglePayLog::STATUS_CHECKING]]); if ($single_pay_log) { //记录已存在 throw new Exception('记录已存在'); } $t = \Yii::$app->db->beginTransaction(); try { $model = new PaidSinglePayLog(); $model->order_no = $order_no; $model->mall_id = $paymentTransfer->mall_id; $model->type = 'BANK_PAY_DAILY_ORDER'; $model->receive_account = $this->format($cash_extra['bank_account']); $model->receive_name = $cash_extra['name']; $model->account_type = $cash_extra['bank_account_type'] == 1 ? 201 : 204; $model->receive_bank_chanel_no = (string) $cash_extra['receive_bank_chanel_no'] ?? ''; $model->amount = $paymentTransfer->amount; $model->currency = 201; $model->is_checked = 202; $model->paid_desc = $paymentTransfer->title; $model->paid_use = 201; $model->source = $paymentTransfer->source; $model->created_at = time(); $model->merchant_order_no = PaidSinglePayLog::createMerchantOrderNo(); if (!$model->save()) { throw new Exception(isset($model->errors) ? current($model->errors)[0] : '数据异常!'); } $res = $this->singlePay($model->merchant_order_no, $paymentTransfer->amount); if ($res) { //修改 $paymentTransfer->is_pay = 1; if (!$paymentTransfer->save()) { throw new Exception(isset($paymentTransfer->errors) ? current($paymentTransfer->errors)[0] : '数据异常!'); } } $t->commit(); return true; } catch (Exception $e) { $t->rollBack(); throw $e; } } /** * 单笔代付 * @param string $order_no * @param float $amount * @return bool * @throws */ public function singlePay(string $order_no, float $amount): bool { $single_pay_log = PaidSinglePayLog::findOne(['merchant_order_no' => $order_no]); if (!$single_pay_log) { return false; } if ($single_pay_log->amount != $amount) { return false; } // self::getConfig($single_pay_log->mall_id); // $productCode = [ // 'BANK_PAY_ORDINARY_ORDER'=>'普通代付', // 'BANK_PAY_DAILY_ORDER'=>'朝夕付', // 'BANK_PAY_MAT_ENDOWMENT_ORDER'=>'任意付', // 'BANK_PAY_COMPOSE_ORDER'=>'组合付', // ]; // $protocol = env('PAY_NOTIFY_PROTOCOL'); //$callbackUrl = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '?r=api/pay-notify/joinpay-single'; $api_host = \Yii::$app->services->setting->platform->get('system.api_url'); //https://api.qijianshigw.com/notify/joinpay-single $callbackUrl = $api_host.'/notify/joinpay-single'; // $callbackUrl = Url::toApi(['notify/joinpay-single'], true); \Yii::error('JoinpaySinglePay notify url:'.$callbackUrl); $request = [ 'userNo' => $this->config['joinpay_merchant_no'], //商户编号 'productCode' => $single_pay_log->type, //产品类型,选择:BANK_PAY_DAILY_ORDER 朝夕付 'requestTime' => date("Y-m-d H:i:s", time()), //交易请求时间 'merchantOrderNo' => $order_no, //商户订单号,必填 'receiverAccountNoEnc' => $single_pay_log->receive_account, //收款账户号,必填 'receiverNameEnc' => $single_pay_log->receive_name, //收款人,必填 'receiverAccountType' => $single_pay_log->account_type, //账户类型,必填 'receiverBankChannelNo' => $single_pay_log->receive_bank_chanel_no ?? '', //收款账户联行号 'paidAmount' => sprintf("%.2f", $amount), //交易金额 'currency' => $single_pay_log->currency, //币种:人民币201,美金202 'isChecked' => $single_pay_log->is_checked, //是否复核 'paidDesc' => $single_pay_log->paid_desc, //代付说明 'paidUse' => $single_pay_log->paid_use, //代付用途 'callbackUrl' => $callbackUrl, //商户通知地址 //'firstProductCode' => '', //优先使用产品,组合付必填,非组合付不用填写): ]; //商户密钥: $secret = $this->config['joinpay_merchant_secret']; $request['hmac'] = urlencode(RequestUtil::hmacRequest($request, $secret)); \Yii::error('JoinpaySinglePay post_data:'.json_encode($request)); $result = RequestUtil::http_post($this->single_pay_url, $request, true); //保存接口返回信息 $single_pay_log->result = $result; $single_pay_log->save(); \Yii::error('alternativePay result:' . $result); $result = Json::decode($result, true); if ($result['statusCode'] == 2001) { return true; } throw new Exception($result['message']); } /** * 查询代付订单状态 * @param $order_no * @return bool|mixed|string */ public function queryAlternativeOrder($order_no) { $request = [ 'userNo' => $this->config['joinpay_merchant_no'], //商户编号 'merchantOrderNo' => $order_no, //商户订单号,必填 ]; //商户密钥: $secret = $this->config['joinpay_merchant_secret']; $request['hmac'] = urlencode(RequestUtil::hmacRequest($request, $secret)); $result = RequestUtil::http_post($this->single_query_url, $request, true); \Yii::error('queryAlternativeOrder result:' . $result); return Json::decode($result, true); } /** * 获取支付配置 * @author zmz * @time 2021-05-08 */ private function getConfig($mall_id) { //$cfg = \Yii::$app->services->setting->mall->get($mall_id, 'pay', 'joinpay'); $cfg = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_JOIN_PAY, 'basic'); $this->config = $cfg; return $this->config; //$this->config = Json::decode($cfg['joinpay']); //return Json::decode($cfg['joinpay']); } /** * 修改代付状态 * @param PaidSinglePayLog $single_pay_log * @param $query_info * @return bool * @throws Exception */ public function updateSinglePayStatus($single_pay_log,$query_info) { if($query_info['statusCode'] !== 2001){ //受理失败 return ; } $single_pay_log->result = json_encode($query_info); $status = PaidSinglePayLog::STATUS_CHECKING; if(in_array($query_info['data']['status'],[self::STATUS_SUCCESS])){ //成功 $status = PaidSinglePayLog::STATUS_SUCCESS; $single_pay_log->error_msg = $query_info['data']['errorDesc'] ?? '交易成功'; }elseif (in_array($query_info['data']['status'],[self::STATUS_FAIL,self::STATUS_CANCEL,self::STATUS_NOTEXIST])){ //失败 $status = PaidSinglePayLog::STATUS_ERROR; $single_pay_log->error_msg = $query_info['data']['errorDesc'] ?? ''; }else{ //其他状态:处理中 } if($status == $single_pay_log->status){ return ; } $single_pay_log->status = $status; if(!$single_pay_log->save()){ throw new Exception($single_pay_log->getErrorMessage()); } switch ($single_pay_log->source){ case '2': if(MallAddons::isInstall($single_pay_log->mall_id,'Store')){ if($status == PaidSinglePayLog::STATUS_ERROR) { StoreWithdrawLog::remit_fail(StoreWithdrawLog::findOne(['order_no' => $single_pay_log->order_no]), $single_pay_log->error_msg); } } break; case '3': if(MallAddons::isInstall($single_pay_log->mall_id,'Mch')){ if($status == PaidSinglePayLog::STATUS_ERROR) { MchWithdrawLog::remit_fail(StoreWithdrawLog::findOne(['order_no' => $single_pay_log->order_no]), $single_pay_log->error_msg); } } break; default: if($status == PaidSinglePayLog::STATUS_ERROR) { UserWithdrawLog::remit_fail(UserWithdrawLog::findOne(['order_no' => $single_pay_log->order_no]), $single_pay_log->error_msg); } if($status == PaidSinglePayLog::STATUS_SUCCESS) { UserWithdrawLog::remit_success(UserWithdrawLog::findOne(['order_no' =>$single_pay_log->order_no ])); } } return true; } }