/*微信小程序的配置信息
微信商戶信息
*/
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' => ' ', // 你也可以在下單時單獨設(shè)置來想覆蓋它
// 下面為可選項
// 指定 API 調(diào)用返回結(jié)果的類型:array(default)/collection/object/raw/自定義類名
'response_type' => 'array',
'log' => [
'level' => 'debug',
'file' => __DIR__.'/wechat.log',
],
];
}
/*拿到用戶code 后下訂單 創(chuàng)建訂單
拿到用戶加密信息后解析手機號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');//加密數(shù)據(jù)
$encryptedData = $this->getArg('encryptedData');//加密數(shù)據(jù)
$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'];
//解密數(shù)據(jù)
$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);
//根據(jù)資助信息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);
//調(diào)用微信統(tǒng)一下單接口
if($orderInfo>0){
$payContent=[
'body' => '商品名稱',
'out_trade_no' => $order_id,
'total_fee' => $fenmoney,
'spbill_create_ip' => '', // 可選,如不傳該參數(shù),SDK 將會自動獲取相應 IP 地址
'notify_url' => 'url', // 支付結(jié)果通知網(wǎng)址,如果不設(shè)置則會使用配置里的默認地址
'trade_type' => 'JSAPI', // 請對應換成你的支付方式對應的值類型
'openid' => $openid,
];
$payinfo=$this->Wxhelppay($payContent);
$this->returnJson(['code' =>200, 'msg' => '創(chuàng)建訂單信息成功', 'data' => $payinfo,'info' =>$decryptedData['phoneNumber']]);
}
else{
$this->returnJson(['code' =>401, 'msg' => '獲取訂單信息失敗', 'data' => '']);
}
}
else{
$this->returnJson(['code' => 0, 'msg' => '請求數(shù)據(jù)有誤,請核實', 'data' => ""]);
}
}
/* @todo 統(tǒng)一下單接口
*/
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變?yōu)榇髮?/span>
return $config;//(對應原理4)
}
else{
$this->returnJson(['code' => 0, 'msg' => '微信支付簽名失敗,data', 'data' => $result]);
}
}
/* * @todo 回調(diào)接口
*/
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("訂單信息不存在");
}
// 信息存在就判斷當前訂單的狀態(tài)
if ($orderInfo['pay_status'] == 1 ) {
}
// 查詢該筆訂單是否已經(jīng)支付過了
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']]);
// 更新訂單用戶數(shù)據(jù)
$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();
}