| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- <?php
- namespace common\components\payment;
- use addons\Mch\common\models\MchWithdrawLog;
- use addons\Store\common\models\StoreWithdrawLog;
- use common\components\payment\joinpay\util\RequestUtil;
- use common\enums\AddonsEnum;
- use common\helpers\Url;
- use common\models\common\PaidSinglePayLog;
- use common\models\mall\MallAddons;
- use common\models\user\UserWithdrawLog;
- use Yii;
- use Exception;
- use yii\helpers\Json;
- /**
- * 汇聚支付:代付
- * Class AlternativePay
- * @package app\lib\payment\AlternativePay
- */
- class AlternativePay
- {
- public $config = [];
- //protected $pay_code = 'joinpay_scan_pay';
- //单笔代付
- public $single_pay_url = 'https://www.joinpay.com/payment/pay/singlePay';
- //单笔代付查询接口
- public $single_query_url = 'https://www.joinpay.com/payment/pay/singlePayQuery';
- //代付接口状态
- const STATUS_CREATE = 201; // 201,订单已创建,汇聚受理并创建代付订单的初始状态
- const STATUS_WAIT = 202; //202,待商户审核,代付订单处在等待商户审核的阶段
- const STATUS_CHECKING = 203; //203,交易处理中,代付订单处理中状态
- const STATUS_FAIL = 204; //204,交易失败,明确交易失败的状态码
- const STATUS_SUCCESS = 205; //205,交易成功,明确交易成功的状态码
- const STATUS_CANCEL = 208; //208,订单已取消,代付订单被商户审核为拒绝付款
- const STATUS_FROZEN = 210; //210,账务冻结中,确认出款后对商户的账户余额进行冻结处理
- const STATUS_UNFROZEN = 211; //211,账务解冻中,确认失败后对商户的账户余额进行解冻处理
- const STATUS_CANCELING = 212; // 212,订单取消中,商户审核为拒绝付款的中间状态
- const STATUS_DEDUCTING = 213; //213,账务扣款中,确认成功后对商户的账户冻结款的扣款处理
- const STATUS_NOTEXIST = 214; //214,订单不存在,汇聚未受理该笔代付请求,找不到该笔订单,明确失败
- public function __construct($cfg = [], $mall_id)
- {
- if (empty($cfg)) {
- $cfg = $this->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;
- }
- }
|