cxgj/models/common/payment/PaymentActionForm.php
2023-11-27 09:45:13 +08:00

153 lines
4.4 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年8月13日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common\payment;
use app\components\SysConst;
use app\components\wechat\aes\WXBizDataCrypt;
use app\models\Model;
use app\models\UserOauth;
class PaymentActionForm extends Model {
public $pay_id;
public $pay_type;
public $cx_mch_id;
public $wechat_mp;
public $code;
public function rules()
{
return [
[['pay_id', 'cx_mch_id'], 'integer'],
[['pay_type','code'], 'string'],
[['pay_type', 'pay_id'], 'required', 'on' => 'pay_data'],
[['pay_id'], 'required', 'on' => 'balance_pay'],
];
}
public function payData()
{
if(!$this->validate()){
return $this->getModelError();
}
try{
if(empty($this->code)){
$openid = '';
}else{
$res = $this->code2session($this->code);
if($res['code'] != 0){
$openid = '';
}else{
$openid = $res['data']['openid'];
}
$user_id = \Yii::$app->user->identity->id;
if(!empty($user_id) && !empty($openid)){
$user_oauth = UserOauth::findOne(['user_id' => $user_id,'is_delete' => 0,'type' => SysConst::$cxOauthProviderWxmp]);
if($user_oauth != null){
$user_oauth->openid = $openid;
$user_oauth->created_at = time();
$user_oauth->save();
}
}
}
$payment = new Payment();
$data = $payment->getPayData($this->pay_id, $this->pay_type, $this->cx_mch_id,$openid);
} catch (\Exception $ex){
$data = $this->apiReturnError($ex->getMessage());
} catch (\yii\base\ErrorException $ex){
$data = $this->apiReturnError($ex->getMessage());
}
return $data;
}
public function balancePay()
{
if(!$this->validate()){
return $this->getModelError();
}
try{
$payment = new Payment();
$data = $payment->payBuyBalance($this->pay_id);
} catch (\Exception $ex){
$data = $this->apiReturnError($ex->getMessage());
}
return $data;
}
public function integralPay()
{
if(!$this->validate()){
return $this->getModelError();
}
try{
$payment = new Payment();
$data = $payment->payBuyIntegral($this->pay_id);
} catch (\Exception $ex){
$data = $this->apiReturnError($ex->getMessage());
}
return $data;
}
private function code2session($code)
{
$api = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->wechat_mp->appId}&secret={$this->wechat_mp->appSecret}&js_code={$code}&grant_type=authorization_code";
$this->wechat_mp->curl->get($api);
if($this->wechat_mp->curl->error_code != 0){
return [
'code' => 1,
'msg' => "err_code:{$this->wechat_mp->curl->error_code}err_msg:{$this->wechat_mp->curl->error_msg}"
];
}
$resp = $this->wechat_mp->curl->response;
$res = json_decode($resp, true);
if(!isset($res['openid'])){
return [
'code' => 1,
'msg' => isset($res['errmsg']) ? $res['errmsg'] : 'error'
];
}
return [
'code' => 0,
'msg' => 'ok',
'data' => $res
];
}
/***
* 用户数据解密
*/
private function decrypted_data($session_key){
$pc = new WxBizDataCrypt($this->wechat_mp->appId, $session_key);
$errCode = $pc->decryptData($this->encrypted_data, $this->iv, $data );
if ($errCode == 0) {
return [
'code' => 0,
'msg' => 'success',
'data' => $data
];
} else {
return [
'code' => 1,
'msg' => $errCode,
];
}
}
}