首页 > 微信 > 详细

微信支付 easy wechat 使用

时间:2021-08-13 17:31:06      阅读:31      评论:0      收藏:0      [点我收藏+]
/*微信小程序的配置信息
微信商户信息
*/
public function __construct()
{
parent::__construct();
$this->OrderModel = new OrderModel();
$this->config = [
‘app_id‘ => config(‘wechat.cmf_wechat_appid‘),
‘secret‘ =>config(‘wechat.cmf_wechat_appsecret‘),
‘mch_id‘ => ‘ ‘, //商户号
‘key‘ => ‘ ‘, // API 密钥
‘notify_url‘ => ‘ ‘, // 你也可以在下单时单独设置来想覆盖它
// 下面为可选项
// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
‘response_type‘ => ‘array‘,
‘log‘ => [
‘level‘ => ‘debug‘,
‘file‘ => __DIR__.‘/wechat.log‘,
],
];

}
/*拿到用户code 后下订单 创建订单
拿到用户加密信息后解析手机号phone
*/

public function creatOrder(){
$id = $this->getArg(‘id‘,1,‘intval‘);
$code = $this->getArg(‘code‘);
//接收用户信息
$nickname = $this->getArg(‘nickname‘);
$headLogo = $this->getArg(‘headLogo‘);
$iv = $this->getArg(‘iv‘);//加密数据
$encryptedData = $this->getArg(‘encryptedData‘);//加密数据
$money = $this->getArg(‘amount‘,0.01,‘float‘);
$fenmoney = 100*$money;
//获取opeinid
$app = Factory::miniProgram($this->config);
$userinfo=$app->auth->session($code);
if(array_key_exists(‘openid‘,$userinfo)){
$openid = $userinfo[‘openid‘];
$session= $userinfo[‘session_key‘];
//解密数据
$decryptedData = $app->encryptor->decryptData($session, $iv, $encryptedData);
//mydebug($decryptedData);
if(array_key_exists(‘phoneNumber‘,$decryptedData)){
$valData[‘phone‘]=$decryptedData[‘phoneNumber‘];
}
else{
$decryptedData[‘phoneNumber‘]=‘111111‘;
}

}

else{
$this->returnJson([‘code‘ =>401, ‘msg‘ => ‘获取用户信息失败‘, ‘data‘ => $userinfo]);
}

$order_id=date("YmdHis").rand(1000,9999);
//根据资助信息id 查看资助信息是否需要支付
$helpInfo=Children::getInstance()->getById($id);
if($helpInfo[‘data‘][‘help_status‘]==0){
$valData[‘order_id‘]=$order_id;
$valData[‘open_id‘]=$openid;
$valData[‘help_id‘]=$helpInfo[‘data‘][‘id‘];
$valData[‘createtime‘]=time();
$valData[‘nickname‘]=$nickname;
$valData[‘money‘]=$money;
$valData[‘wx_headlogo‘]=$headLogo;
//添加支付订单信息
$orderInfo=db(‘order‘)->insertGetId($valData);
// $orderInfo=$this->OrderModel->insertGetId($valData);
//调用微信统一下单接口
if($orderInfo>0){
$payContent=[
‘body‘ => ‘商品名称‘,
‘out_trade_no‘ => $order_id,
‘total_fee‘ => $fenmoney,
‘spbill_create_ip‘ => ‘‘, // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
‘notify_url‘ => ‘url‘, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
‘trade_type‘ => ‘JSAPI‘, // 请对应换成你的支付方式对应的值类型
‘openid‘ => $openid,
];
$payinfo=$this->Wxhelppay($payContent);
$this->returnJson([‘code‘ =>200, ‘msg‘ => ‘创建订单信息成功‘, ‘data‘ => $payinfo,‘info‘ =>$decryptedData[‘phoneNumber‘]]);
}
else{
$this->returnJson([‘code‘ =>401, ‘msg‘ => ‘获取订单信息失败‘, ‘data‘ => ‘‘]);
}
}
else{
$this->returnJson([‘code‘ => 0, ‘msg‘ => ‘请求数据有误,请核实‘, ‘data‘ => ""]);
}
}
 /* @todo 统一下单接口
*/
public function Wxhelppay($payContent){
$payment = Factory::payment($this->config);
$result = $payment->order->unify($payContent);
if ($result[‘result_code‘] == ‘SUCCESS‘ && $result[‘return_code‘] == ‘SUCCESS‘) {
$prepayId = $result[‘prepay_id‘];
$jssdk = $payment->jssdk;
$config = $jssdk->sdkConfig($prepayId);
$config[‘timeStamp‘] = $config[‘timestamp‘];//此处需要将小写s变为大写
return $config;//(对应原理4)
}
else{
$this->returnJson([‘code‘ => 0, ‘msg‘ => ‘微信支付签名失败,data‘, ‘data‘ => $result]);
}
}



/* * @todo 回调接口
*/
public function notify()
{
$payment = Factory::payment($this->config);
$response = $payment->handlePaidNotify(function ($message, $fail) {

if ($message[‘return_code‘] === ‘SUCCESS‘ && $message[‘result_code‘] === ‘SUCCESS‘) {
// 处理信息
$orderInfo = $this->OrderModel->where("order_id", $message[‘out_trade_no‘])->find();
// 如果信息不存在直接返回错误信息
if (!$orderInfo) {
return $fail("订单信息不存在");
}
// 信息存在就判断当前订单的状态
if ($orderInfo[‘pay_status‘] == 1 ) {
}
// 查询该笔订单是否已经支付过了
if ($message[‘return_code‘] === ‘SUCCESS‘) {
// 判断是否支付成功
if (isset($message[‘result_code‘]) && $message[‘result_code‘] === ‘SUCCESS‘) {
$this->OrderModel->startTrans();
try {
// 当前的订单支付成功
$this->OrderModel->where("order_id", $message[‘out_trade_no‘])->update([‘pay_status‘ => 1, ‘paytime‘ => time(), ‘description‘ => $message[‘transaction_id‘]]);
// 更新订单用户数据
$data[‘id‘]=$orderInfo[‘help_id‘];
$data[‘help_status‘]=1;
$data[‘asset_number‘]=$message[‘cash_fee‘];
$data[‘help_id‘]=$orderInfo[‘nickname‘];
$data[‘help_phone‘]=$orderInfo[‘phone‘];
$a=Children::getInstance()->update($data);
$this->OrderModel->commit();
return true;
} catch (\Exception $exception) {
$this->OrderModel->rollback();
return $fail($exception->getMessage());
}
}

} elseif ($message[‘result_code‘] === ‘FAIL‘) {
return true;
}
}
});
return $response->send();
}

微信支付 easy wechat 使用

原文:https://www.cnblogs.com/xiaoyueya/p/15137099.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!