basePath . "/utils/Alipay/alipaySdk/aop/AopClient.php"; class AlipayThirdOrderForm extends Model { public $goods_id; public $order_id; public $mini_id; public $base_info; public function rules() { return [ [['goods_id', 'order_id'], 'integer'] ]; } public function __construct($config = []) { parent::__construct($config); //三方平台Appid $this->base_info['appid'] = Option::get("alipay_appid", 0, 'saas')['value']; //获取当前时间戳 $this->base_info['timestamp'] = date("Y-m-d H:i:s"); //设置加解密方式 $this->base_info['sign_type'] = "RSA2"; //获取私钥 $this->base_info['private_key'] = Option::get("alipay_app_private_key", 0, 'saas')['value']; //获取公钥 $this->base_info['public_key'] = Option::get("alipay_public_key", 0, 'saas')['value']; //获取ase_key,加签方式 $this->base_info['alipay_aes_key'] = Option::get("alipay_aes_key", 0, 'saas')['value']; } public function miniCommon($request, $biz_content = null, $is_hidden_token = 0, $token = '') { try { //请求的基础信息 $base_info = $this->base_info; //小程序ID $mini_id = $this->mini_id ?: get_mini_id(); if (!empty($mini_id)) { $store_mini = StoreAliMini::findOne($mini_id); //如果小程序ID存在,则将token从模板转成商家小程序的token if (!empty($store_mini->auth_token)) { $auth_token = $store_mini->auth_token; } } else { throw new \Exception("获取不到token"); } //设置基础请求数据 $aop = new AopClient(); //设置appid $aop->appId = $base_info['appid']; //设置私钥 $aop->rsaPrivateKey = $base_info['private_key']; //设置公钥 $aop->alipayrsaPublicKey = $base_info['public_key']; //设置ase_key,加签方式 $aop->encryptKey = $base_info['alipay_aes_key']; //设置加解密方式 $aop->signType = $base_info['sign_type']; //处理没有biz_content情况 if ($biz_content) { $request->setBizContent($biz_content); } //处理不需要token情况 if ($is_hidden_token === 0) { //开始请求 $result = $aop->execute($request, null, $auth_token); } else { //开始请求 $result = $aop->execute($request); } //请求后的数据处理 $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; return $result->$responseNode; } catch (\Exception $e) { return [ 'sub_msg' => $e->getMessage() ]; } } //保存商品素材 public function updateGoodsFile($goods_id) { try { if (empty($goods_id)) { $goods_id = $this->goods_id; } $goods = Goods::findOne($goods_id); if (!empty($goods->cover_pic)) { $url_path = $this->saveTempImage($goods->cover_pic); $request = new AlipayMerchantItemFileUploadRequest(); $request->setScene("SYNC_ORDER"); $request->setFileContent('@' . $url_path); $form = new \app\modules\admin\models\AlipayThirdForm(); $form->store_id = $goods->store_id; $result = $form->miniCommon($request, 1); // $result = $this->miniCommon($request); if (!empty($result->code) && $result->code == 10000) { $goods->material_id = $result->material_id; $goods->material_key = $result->material_key; if (!$goods->save()) { throw new \Exception($goods->errors); } return [ 'code' => 0, 'msg' => "添加商品素材成功", 'data' => $result ]; } throw new \Exception($result->sub_msg); } throw new \Exception("文件信息不存在"); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //保存订单信息 public function updateOrderInfo($order_id, $model = null) { try { if (empty($order_id)) { throw new \Exception("获取订单信息失败"); } //假设多种订单类型 if (empty($model)) { $model = Order::find(); } $order = $model->where(['id' => $order_id])->asArray()->one(); if (empty($order)) { throw new \Exception("订单信息不存在"); } $store_id = $order['store_id']; $form = new \app\modules\admin\models\AlipayThirdForm(['store_id' => $store_id]); //更换订单状态 $status = $order['trade_status']; switch ($status) {//0:待发货,1:已取消,2:已发货,3:已确认 case 0: $status = "TO_SEND_GOODS"; break; case 1: $status = "REFUNDING"; break; case 2: $status = "IN_DELIVERY"; $form->AlipayOpenMiniOrderDeliverySend($order); break; case 3: $status = "CLOSED"; $form->AlipayOpenMiniOrderDeliveryReceive($order); break; } $refund = \app\models\OrderRefund::find()->where(['order_id' => $order['id'], 'is_delete' => 0, 'status' => 1, 'store_id' => $order['store_id']])->orderBy('id DESC')->one(); if($refund){ $status = "REFUNDED"; } $order_detail = OrderDetail::find()->alias('od')->where(['od.order_id' => $order['id']]) ->leftJoin(['g' => Goods::tableName()], 'od.goods_id = g.id') ->select('od.id, od.order_id, od.num, od.goods_name, g.material_id, g.price, od.goods_id')->asArray()->all(); $goods_data = []; if(is_open_platform()){ $mini = StoreAliMini::findOne(['store_id' => $store_id, 'is_cancel' => 0, 'is_use' => 1]); $app_id = $mini->auth_app_id; $this->mini_id = $mini->id; }else{ $value = Option::get(Option::OPTOPN_KEY, $store_id, 'alipay')['value']; $conf = json_decode($value, true); $app_id = $conf['app_id']; } $user = User::findOne($order['user_id']); foreach ($order_detail as $index => $item) { //获取素材id $material_id = $item['material_id']; if (empty($item['material_id'])) { $result = $this->updateGoodsFile($item['goods_id']); if ($result['code'] === 0) { $material_id = $result['data']->material_id; } } $goods_data[$index]['item_name'] = $item['goods_name']; $goods_data[$index]['quantity'] = $item['num']; $goods_data[$index]['unit_price'] = $item['price']; $goods_data[$index]['ext_info'] = [ 'ext_key' => 'image_material_id', 'ext_value' => $material_id ]; } $data = json_encode([ 'amount' => $order['pay_price'], 'order_modified_time' => date("Y-m-d H:i:s") . '.000', 'order_create_time' => date("Y-m-d H:i:s", $order['created_at']), 'pay_amount' => $order['pay_price'], 'item_order_list' => $goods_data, 'trade_no' => $order['alipay_trade_no'], 'buyer_id' => $user->alipay_open_id, 'out_biz_no' => $order['order_no'], 'ext_info' => [ [ 'ext_key' => 'merchant_order_status', 'ext_value' => $status ], [ 'ext_key' => 'merchant_biz_type', 'ext_value' => "KX_SHOPPING" ], [ 'ext_key' => 'merchant_order_link_page', 'ext_value' => "/order/order-detail/order-detail?id=" . $order_id ], [ 'ext_key' => 'tiny_app_id', 'ext_value' => $app_id ] ] ]); $query = new AlipayMerchantOrderSyncRequest(); $form->store_id = $store_id; $form->biz_content = $data; $result = $form->miniCommon($query); // $result = $this->miniCommon($query, $data); $file_name = \Yii::$app->runtimePath . '/logs/app_alipay.log'; file_put_contents($file_name, "\r\n" . '[订单信息回传][' . date('Y-m-d H:i:s') . ']' . json_encode($result), FILE_APPEND); if (!empty($result->code) && $result->code == 10000) { return [ 'code' => 0, 'msg' => "success", 'data' => $result ]; } throw new \Exception(json_encode($result)); } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //获取网络图片到临时目录 public function saveTempImage($url) { if (strpos($url, 'http') === false) { $url = 'http:' . trim($url); } if (!is_dir(\Yii::$app->runtimePath . '/image')) { mkdir(\Yii::$app->runtimePath . '/image'); } $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg'; CurlHelper::download($url, $save_path); return $save_path; } }