/*微信小程序的配置信息
微信商户信息
*/
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();
}

最新文章

  1. SharePoint 2013 通过审计获取文档下载次数
  2. C语言(4)
  3. 创建一个Table View
  4. sap透明表、结构、簇介绍以及查找表方法
  5. 3.发布Maven项目到nexus中
  6. [转] C#实现自动化Log日志
  7. Xcode 7 warnings: object file was built for newer iOS version than being linked
  8. 手机版和PC版识别
  9. 使用PowerDesigner创建mysql数据库表图
  10. 【转】SDWebImage实现分析
  11. 根据关键字获取高德地图poi信息
  12. 设计模式のStatePattern(状态模式)----行为模式
  13. java json 转换
  14. Codeforces Round #407 div2 题解【ABCDE】
  15. MySQL 的数据类型,有哪些?
  16. pipenv知识积累
  17. gradle 配置及设置本地仓库
  18. sql sever读取写入Excel总结
  19. django的queryset和objects对象
  20. ASCII 可打印字符与控制字符

热门文章

  1. SQL server数据库中nvarchar和varchar的区别
  2. satpy 处理卫星 FY4A 数据
  3. ref(代替id)
  4. QE11 / QE51N 界面太小问题
  5. input file 图片上传前预览
  6. Zookeeper ZAB协议-客户端源码解析
  7. 集成GIT仓库
  8. 多线程事务回滚sqlSession, spring-mybatis 开启事务
  9. django查询中values_list(flat=True) 是什么意思?
  10. 更改材质uv