$code, 'msg'=>$msg, 'data'=>$data, ]; } public static function getOptions($uniacid){ $set=ybConfig::getSystemSet('aliSet',$uniacid); if(Yii::$app->params['isDev']==true){ $url=Yii::$app->request->hostInfo.'/addons/yb_wm/index.php/channelApi/pay-notice/ali-notice'; }else{ $url=Yii::$app->request->hostInfo.'/index.php/channelApi/pay-notice/ali-notice'; } $options = new Config(); $options->protocol = 'https'; $options->gatewayHost = 'openapi.alipay.com'; $options->signType = 'RSA2'; $options->appId = $set['appId']; // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中 $options->merchantPrivateKey = str_replace(PHP_EOL, '', $set['privateKey']);//私钥 // $options->alipayCertPath = '<-- 请填写您的支付宝公钥证书文件路径,例如:/foo/alipayCertPublicKey_RSA2.crt -->'; // $options->alipayRootCertPath = '<-- 请填写您的支付宝根证书文件路径,例如:/foo/alipayRootCert.crt" -->'; // $options->merchantCertPath = '<-- 请填写您的应用公钥证书文件路径,例如:/foo/appCertPublicKey_2019051064521003.crt -->'; // 注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可 $options->alipayPublicKey = str_replace(PHP_EOL, '', $set['publicKey']);//公钥 //可设置异步通知接收服务地址(可选) $options->notifyUrl = $url; //可设置AES密钥,调用AES加解密相关接口时需要(可选) $options->encryptKey = $set['encryptKey']; return $options; } //登录 public static function login($uniacid,$code){ Factory::setOptions(self::getOptions($uniacid)); try { $result = Factory::base()->oauth()->getToken($code); $responseChecker = new ResponseChecker(); if ($responseChecker->success($result)) { $res=json_decode($result->httpBody,true); return self::result(1,'成功',$res['alipay_system_oauth_token_response']['user_id']); } else { return self::result(2,"调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL); } } catch (\Exception $e) { return self::result(2,"调用失败,". $e->getMessage(). PHP_EOL); } } //支付 public static function pay($uniacid,$outTradeNo, $money,$note,$openId,$orderType){ Factory::setOptions(self::getOptions($uniacid)); $set=ybConfig::getSystemSet('aliSet',$uniacid); $passback_params['uniacid']=$uniacid; $passback_params['orderType']=$orderType; $extend_params['sys_service_provider_id']=$set['rebate_pid'];//服务商id $extend_params['food_order_type']='home_delivery'; $business_params['tinyAppId']=$set['appId']; try { $result = Factory::payment()->common()->optional('extend_params',$extend_params)->optional('business_params',$business_params)->optional('passback_params',http_build_query($passback_params))->create($note, $outTradeNo, $money, $openId); $responseChecker = new ResponseChecker(); if ($responseChecker->success($result)) { return self::result(1,'成功',json_decode($result->httpBody,true)); } else { return self::result(2,"调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL); } } catch (\Exception $e) { return self::result(2,"调用失败,". $e->getMessage(). PHP_EOL); } } //字节跳动支付// public static function zjPay($uniacid,$outTradeNo, $money,$note,$orderType){ Factory::setOptions(self::getOptions($uniacid)); $passback_params['uniacid']=$uniacid; $passback_params['orderType']=$orderType; try { $result = Factory::payment()->app()->optional('passback_params',http_build_query($passback_params))->pay($note, $outTradeNo, $money); $responseChecker = new ResponseChecker(); if ($responseChecker->success($result)) { // $res=Factory::util()->generic()->execute("alipay.trade.create",'', $arr); // print_R(http_build_query(json_decode($result->httpBody,true)));die; return self::result(1,'成功',$result->body); } else { return self::result(2,"调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL); } } catch (\Exception $e) { return self::result(2,"调用失败,". $e->getMessage(). PHP_EOL); } } //同步订单 public static function sync($uniacid,$orderId,$type){ $goodsData=[]; if($type==1){ $order=(new \yii\db\Query()) ->from('{{%ybwm_takeout_order}}') ->where(['id'=>$orderId])->one(); $mobile=$order['receivedTel']; }else{ $order=(new \yii\db\Query()) ->from('{{%ybwm_instore_order}}') ->where(['id'=>$orderId])->one(); $user=(new \yii\db\Query()) ->select('userTel') ->from('{{%ybwm_member}}') ->where(['id'=>$order['userId']])->one(); $mobile=$user['userTel']; } $set=ybConfig::getSystemSet('aliSet',$uniacid); $store=(new \yii\db\Query()) ->select('isMain') ->from('{{%ybwm_store}}') ->where(['id'=>$order['storeId']])->one(); if($store['isMain']==1){ $shop_id=$set['shop_id']; }else{ $ali_store=(new \yii\db\Query()) ->select('shop_id') ->from('{{%ybwm_ali_store}}') ->where(['storeId'=>$order['storeId']])->one(); if(!$ali_store['shop_id']){ return true; } $shop_id=$ali_store['shop_id']; } $orderGoods=(new \yii\db\Query()) ->from('{{%ybwm_order_goods}}') ->where(['orderId'=>$orderId,'item'=>$type]) ->all(); for($i=0;$i'', 'item_id'=>'', 'item_name'=>$orderGoods[$i]['name'], 'unit_price'=>$orderGoods[$i]['money'], 'quantity'=>$orderGoods[$i]['num'], 'ext_info'=>[ [ 'ext_key'=>'dish_type', 'ext_value'=>'other', ] ] ]; } $user=(new \yii\db\Query()) ->select('openId') ->from('{{%ybwm_member}}') ->where(['id'=>$order['userId']])->one(); Factory::setOptions(self::getOptions($uniacid)); $data['out_biz_no']=$order['outTradeNo']; $data['trade_no']=$order['transaction_id']; $data['buyer_id']=$user['openId']; $data['amount']=$order['money']; $data['order_type']='TRADE_ORDER'; $data['partner_id']=$set['appId']; $data['seller_id']=$set['appId']; $data['item_order_list']=$goodsData; $data['ext_info']=[ [ 'ext_key'=>'tiny_app_id', 'ext_value'=>$set['appId'], ], [ 'ext_key'=>'merchant_order_status', 'ext_value'=>'MERCHANT_PAID', ], [ 'ext_key'=>'merchant_biz_type', 'ext_value'=>'qr_food_order', ], [ 'ext_key'=>'business_info', 'ext_value'=>['rebate_pid'=>$set['rebate_pid']],//服务商id ], ]; $data['shop_info']['merchant_shop_id']=$shop_id;//门店id $data['buyer_info']['mobile']=$mobile?:15337777777; try { $result = Factory::util()->generic()->execute('alipay.merchant.order.sync',[],$data); $responseChecker = new ResponseChecker(); if ($responseChecker->success($result)) { $res=json_decode($result->httpBody,true); return self::result(1,'成功',$res); } else { return self::result(2,"调用失败,原因:". $result->msg.",".$result->subMsg); } } catch (\Exception $e) { return self::result(2,"调用失败,". $e->getMessage()); } } //回调验签 public static function verifyNotify($uniacid,$data){ Factory::setOptions(self::getOptions($uniacid)); try { $result = Factory::payment()->common()->verifyNotify($data); $responseChecker = new ResponseChecker(); if ($responseChecker->success($result)) { return self::result(1,'成功'); } else { return self::result(2,"调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL); } } catch (\Exception $e) { return self::result(2,"调用失败,". $e->getMessage(). PHP_EOL); } } //支付宝解密 public static function decrypt($uniacid,$data){ Factory::setOptions(self::getOptions($uniacid)); try { $result = Factory::util()->aes()->decrypt($data); $responseChecker = new ResponseChecker(); if ($responseChecker->success($result)) { // $res=json_decode($result->httpBody,true); return self::result(1,'成功',$result); } else { return self::result(2,"调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL); } } catch (\Exception $e) { return self::result(2,"调用失败,". $e->getMessage(). PHP_EOL); } } //退款 public static function refund($uniacid,$outTradeNo,$money){ Factory::setOptions(self::getOptions($uniacid)); try { $result = Factory::payment()->common()->refund($outTradeNo,$money); $responseChecker = new ResponseChecker(); if ($responseChecker->success($result)) { return self::result(1,'成功'); } else { return self::result(2,"调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL); } } catch (\Exception $e) { return self::result(2,"调用失败,". $e->getMessage(). PHP_EOL); } } //模板消息 public static function template($uniacid,$openId,$templateId,$page,$data){ Factory::setOptions(self::getOptions($uniacid)); try { $result = Factory::marketing()->templatemessage()->send($openId,'',$templateId,$page,$data); $responseChecker = new ResponseChecker(); if ($responseChecker->success($result)) { return self::result(1,'成功'); } else { return self::result(2,"调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL); } } catch (\Exception $e) { return self::result(2,"调用失败,". $e->getMessage(). PHP_EOL); } } //小程序码 public static function qrcode($uniacid,$page,$scene,$describe='描述'){ Factory::setOptions(self::getOptions($uniacid)); try { $result = Factory::base()->qrcode()->create($page,'x='.$scene,$describe); $responseChecker = new ResponseChecker(); if ($responseChecker->success($result)) { $res=json_decode($result->httpBody,true); return self::result(1,'成功',$res['alipay_open_app_qrcode_create_response']['qr_code_url']); } else { return self::result(2,"调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL); } } catch (\Exception $e) { return self::result(2,"调用失败,". $e->getMessage(). PHP_EOL); } } //添加门店 public static function saveStore($uniacid,$storeData,$storeId){ $auth=(new \yii\db\Query()) ->select('merchant_no') ->from('{{%ybwm_ali_auth}}') ->where(['storeId'=>$storeId])->one(); if(!$auth['merchant_no']){ return '请先获取代运营授权'; } Factory::setOptions(self::getOptions($uniacid)); $data['business_address']['city_code']=$storeData['city_code']; $data['business_address']['district_code']=$storeData['district_code']; $data['business_address']['address']=$storeData['address']; $data['business_address']['province_code']=$storeData['province_code']; $data['shop_category']=$storeData['shop_category']; $data['store_id']=$storeData['store_no']; $data['shop_type']='01'; $data['ip_role_id']=$auth['merchant_no']; // $data['ip_role_id']=$set['rebate_pid']; $data['shop_name']=$storeData['shop_name']; $data['contact_mobile']=$storeData['contact_mobile']; try { $result = Factory::util()->generic()->execute('ant.merchant.expand.shop.create',[],$data); $responseChecker = new ResponseChecker(); if ($responseChecker->success($result)) { $res=json_decode($result->httpBody,true); return self::result(1,'成功',$res); } else { return self::result(2,"调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL); } } catch (\Exception $e) { return self::result(2,"调用失败,". $e->getMessage(). PHP_EOL); } } //修改门店 public static function modifyStore($uniacid,$storeData,$storeId){ $auth=(new \yii\db\Query()) ->select('merchant_no') ->from('{{%ybwm_ali_auth}}') ->where(['storeId'=>$storeId])->one(); if(!$auth['merchant_no']){ return '请先获取代运营授权'; } Factory::setOptions(self::getOptions($uniacid)); $data['business_address']['city_code']=$storeData['city_code']; $data['business_address']['district_code']=$storeData['district_code']; $data['business_address']['address']=$storeData['address']; $data['business_address']['province_code']=$storeData['province_code']; $data['shop_category']=$storeData['shop_category']; $data['shop_id']=$storeData['shop_id']; $data['shop_type']='01'; $data['shop_name']=$storeData['shop_name']; $data['contact_mobile']=$storeData['contact_mobile']; try { $result = Factory::util()->generic()->execute('ant.merchant.expand.shop.modify',[],$data); $responseChecker = new ResponseChecker(); if ($responseChecker->success($result)) { $res=json_decode($result->httpBody,true); return self::result(1,'成功',$res); } else { return self::result(2,"调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL); } } catch (\Exception $e) { return self::result(2,"调用失败,". $e->getMessage(). PHP_EOL); } } //授权 public static function authorization($uniacid,$account){ Factory::setOptions(self::getOptions($uniacid)); $data['out_biz_no']=date("YmdHis") . randomAESKey(6); $data['operate_type']='OPERATION_AUTH'; $data['access_product_code']='OPENAPI_AUTH_DEFAULT'; $data['alipay_account']=$account; try { $result = Factory::util()->generic()->execute('alipay.open.sp.operation.apply',[],$data); $responseChecker = new ResponseChecker(); if ($responseChecker->success($result)) { // print_R($result);die; $res=json_decode($result->httpBody,true); return self::result(1,'成功',$res['alipay_open_sp_operation_apply_response']['batch_no']); } else { return self::result(2,"调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL); } } catch (\Exception $e) { return self::result(2,"调用失败,". $e->getMessage(). PHP_EOL); } } //查看授权结果授权//2021050507785253 public static function getAuthorization($uniacid,$batch_no){ Factory::setOptions(self::getOptions($uniacid)); $data['batch_no']=$batch_no; $data['operate_type']='OPERATION_AUTH'; try { $result = Factory::util()->generic()->execute('alipay.open.sp.operation.result.query',[],$data); $responseChecker = new ResponseChecker(); if ($responseChecker->success($result)) { $res=json_decode($result->httpBody,true); return self::result(1,'成功',$res['alipay_open_sp_operation_result_query_response']); } else { return self::result(2,"调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL); } } catch (\Exception $e) { return self::result(2,"调用失败,". $e->getMessage(). PHP_EOL); } } // //添加素材 // public static function upload($uniacid,$url){ // $url='https://images.ddycms.cn/yb_wm/39/2021/03/17/202103171510524481.jpg'; // echo file_get_contents($url);die; // // print_r(self::getOptions($uniacid));die; //// header( "Content-type: image/jpeg"); // $url='/www/wwwroot/bkycms.com/addons/yb_wm/web/static/yb_wm/39/2020/10/13/202010131404482215.png'; //// $p_size = filesize($url); //// $img_binary = fread(fopen($url, "r"), $p_size); // // print_R($img_binary);die; // Factory::setOptions(self::getOptions($uniacid)); // //1. 设置参数(全局只需设置一次) // try { // //2. 发起API调用 // $result = Factory::util()->generic()->fileExecute('alipay.merchant.item.file.upload',['scene'=>'APP_ORDER'],null,['file_content'=>$url]); // $responseChecker = new ResponseChecker(); // //3. 处理响应或异常 // if ($responseChecker->success($result)) { // // print_R($result);die; // $res=json_decode($result->httpBody,true); // // return self::result(1,'成功',$res); // } else { // return self::result(2,"调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL); // } // } catch (Exception $e) { // return self::result(2,"调用失败,". $e->getMessage(). PHP_EOL); // } // } // //添加商品 // public static function saveGoods($uniacid,$goodsId){ // Factory::setOptions(self::getOptions($uniacid)); // //1. 设置参数(全局只需设置一次) // try { // //2. 发起API调用 // $result = Factory::util()->generic()->fileExecute('alipay.merchant.item.file.upload',['scene'=>'SYNC_ORDER','file_content'=>''],[],[]); // $responseChecker = new ResponseChecker(); // //3. 处理响应或异常 // if ($responseChecker->success($result)) { // // print_R($result);die; // $res=json_decode($result->httpBody,true); // // return self::result(1,'成功',$res); // } else { // return self::result(2,"调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL); // } // } catch (Exception $e) { // return self::result(2,"调用失败,". $e->getMessage(). PHP_EOL); // } // } // //查询订单 // public static function getOrder($uniacid){ // Factory::setOptions(self::getOptions($uniacid)); // //1. 设置参数(全局只需设置一次) // try {//query // //2. 发起API调用 // $result = Factory::payment()->common()->query('20210329173516159370'); // $responseChecker = new ResponseChecker(); // //3. 处理响应或异常 // if ($responseChecker->success($result)) { // // $res=json_decode($result->httpBody,true); // print_R($res);die; // return self::result(1,'成功',$res); // } else { // return self::result(2,"调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL); // } // } catch (Exception $e) { // return self::result(2,"调用失败,". $e->getMessage(). PHP_EOL); // } // } /** * 分账关系绑定 * @param $uniacid * @param $receiverList[type,account,name,memo] 支持二维数组 * type:分账方类型。userId:表示是支付宝账号对应的支付宝唯一用户号;loginName:表示是支付宝登录号,在分账关系查询接口中不返回此值。 * account:分账方帐号。当分账方类型是userId时,本参数为用户的支付宝账号对应的支付宝唯一用户号,以2088开头的纯16位数字;当分账方类型是loginName时,本参数为用户的支付宝登录号。 * name:分账方全称,新增分账关系时作为请求参数必填,解绑分账关系时作为请求参数可不填,分账关系查询时不作为返回结果返回 * memo:分账关系描述(可选) * @return \Alipay\EasySDK\Util\Generic\Models\AlipayOpenApiGenericResponse * @throws \Exception result_code :SUCCESS:分账关系绑定成功;FAIL:分账关系绑定失败。 */ public static function RelationBind($uniacid,$receiverList=[]){ if(empty($uniacid) || empty($receiverList)){ return self::result(2,'缺少参数'); } Factory::setOptions(self::getOptions($uniacid)); $data['out_request_no']=base64_encode(time()); $data['receiver_list']= $receiverList; $result = Factory::util()->generic()->execute('alipay.trade.royalty.relation.bind',[],$data); $responseChecker = new ResponseChecker(); if ($responseChecker->success($result)) { $res=json_decode($result->httpBody,true); return self::result(1,'成功',$res['alipay_trade_royalty_relation_batchquery_response']); } else { return self::result(2,"调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL); } } /** * 分账关系查询 * @param $uniacid * @param int $pagNum * @param int $pageSize * $pagNum 几页,起始页为 1。不填默认为 1。 * $pageSize 页面大小。每页记录数,取值范围是(0,100]。不填默认为20 */ public static function RelationBatchquery($uniacid,$pageNum=1,$pageSize=20){ Factory::setOptions(self::getOptions($uniacid)); $data['out_request_no']=base64_encode(time()); $data['page_num']=$pageNum; $data['page_size']=$pageSize; $result = Factory::util()->generic()->execute('alipay.trade.royalty.relation.batchquery',[],$data); $responseChecker = new ResponseChecker(); if ($responseChecker->success($result)) { $res=json_decode($result->httpBody,true); return self::result(1,'成功',$res['alipay_trade_royalty_relation_batchquery_response']); } else { return self::result(2,"调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL); } } /** * 分账关系解绑 * @param $uniacid * @param $receiverList[type,account,name,memo] 支持二维数组 * type:分账方类型。userId:表示是支付宝账号对应的支付宝唯一用户号;loginName:表示是支付宝登录号,在分账关系查询接口中不返回此值。 * account:分账方帐号。当分账方类型是userId时,本参数为用户的支付宝账号对应的支付宝唯一用户号,以2088开头的纯16位数字;当分账方类型是loginName时,本参数为用户的支付宝登录号。 * name:分账方全称,新增分账关系时作为请求参数必填,解绑分账关系时作为请求参数可不填,分账关系查询时不作为返回结果返回 * memo:分账关系描述(可选) * @return \Alipay\EasySDK\Util\Generic\Models\AlipayOpenApiGenericResponse * @throws \Exception result_code :SUCCESS:分账关系绑定成功;FAIL:分账关系绑定失败。 */ public static function RelationNnbind($uniacid,$receiverList=[]){ if(empty($uniacid) || empty($receiverList)){ return self::result(2,'缺少参数'); } Factory::setOptions(self::getOptions($uniacid)); $data['out_request_no']=base64_encode(time()); $data['receiver_list']= $receiverList; $result = Factory::util()->generic()->execute('alipay.trade.royalty.relation.unbind',[],$data); $responseChecker = new ResponseChecker(); if ($responseChecker->success($result)) { $res=json_decode($result->httpBody,true); return self::result(1,'成功',$res['alipay_trade_royalty_relation_batchquery_response']); } else { return self::result(2,"调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL); } } /** * 统一分账 * @param $uniacid * @param $tradeNo :支付宝订单号 * @param $royaltyParameters[[royalty_type,trans_out,trans_out_type,trans_in_type,trans_in,desc,royalty_scene]] :分账明细信息。 仅支持二维数组 * royalty_type:分账类型.普通分账为:transfer;补差为:replenish;为空默认为分账transfer; * trans_out:支出方账户。如果支出方账户类型为userId,本参数为支出方的支付宝账号对应的支付宝唯一用户号,以2088开头的纯16位数字;如果支出方类型为loginName,本参数为支出方的支付宝登录号。 泛金融类商户分账时,该字段不要上送。 * trans_out_type :支出方账户类型。userId表示是支付宝账号对应的支付宝唯一用户号;loginName表示是支付宝登录号; 泛金融类商户分账时,该字段不要上送。 * trans_in_type:收入方账户类型。userId表示是支付宝账号对应的支付宝唯一用户号;cardAliasNo表示是卡编号;loginName表示是支付宝登录号; * trans_in:收入方账户。如果收入方账户类型为userId,本参数为收入方的支付宝账号对应的支付宝唯一用户号,以2088开头的纯16位数字;如果收入方类型为cardAliasNo,本参数为收入方在支付宝绑定的卡编号;如果收入方类型为loginName,本参数为收入方的支付宝登录号; * amount:分账的金额,单位为元 * desc:分账描述(可选) * royalty_scene: 可选值:达人佣金、平台服务费、技术服务费、其他(可选) * operator_id:操作员 ID,商家自定义操作员编号。(可选) * @return array * @throws \Exception */ public static function OrderSettle($uniacid,$tradeNo='',$royaltyParameters=[]){ if(empty($tradeNo) || empty($uniacid) || empty($royaltyParameters)){ return self::result(2,'缺少参数'); } if(!is_array($royaltyParameters) || !isset($royaltyParameters[0]) || !is_array($royaltyParameters[0]) ){ return self::result(2,'分账用户信息错误'); } $data['out_request_no'] = time(); $data['trade_no']= $tradeNo; $data['royalty_parameters']= $royaltyParameters; Factory::setOptions(self::getOptions($uniacid)); $result = Factory::util()->generic()->execute('alipay.trade.order.settle','',$data); $responseChecker = new ResponseChecker(); $res=json_decode($result->httpBody,true); if ($responseChecker->success($result)) { return self::result(1,'成功',$res['alipay_trade_order_settle_response']); } else { return self::result(2,$res); } } }