Przeglądaj źródła

提交数据shopify对应的东西

lvhao 1 dzień temu
rodzic
commit
cd08b790fe

+ 403 - 0
core/CoreApp/models/Model_api.php

@@ -6,6 +6,7 @@ function __construct(){
 		$this->load->_model('Model_classid','classid');
 		$this->load->_model("Model_logic_ding","logic_ding");
 		$this->load->_model("Model_logic_match","logic_match");
+		$this->load->_model("Model_shopifyv1","shopifyv1");
 }
 
 	public function get_hq($name,$shopapi,$shop)
@@ -421,6 +422,264 @@ function __construct(){
 				}
 			
 			
+		}else if(in_array($name,['supernovahair','SuperNova'])){
+				$post['shopifyv1'] = "https://".$shop['shopadmin']."/admin/api/2022-10/orders.json".'?limit=100&fulfillment_status=unshipped&financial_status=paid&status=open&created_at_min='.date('c',time()-3600*24*2);;
+				//?status=any所有订单 &limit=200每页多少单  Afterpay (New)
+				//$post['shopify'] = $shopapi.'?limit=100&fulfillment_status=unshipped&financial_status=paid&status=open&gateway=Afterpay (New)';
+				//$post['shopifyv1'] = "https://".$shop['shopadmin']."/admin/api/2022-10/orders.json".'?status=any';
+				$token = $this->shopifyv1->getAccessToken($shop);
+				if(empty($token)){
+					return [];
+				}
+				$post['access_token'] = $token;
+				$ch = curl_init();
+				curl_setopt($ch,CURLOPT_URL,'https://edm.alipearl.net/soapapi.php');
+				curl_setopt($ch, CURLOPT_POST, 1);
+				curl_setopt($ch, CURLOPT_HEADER, 0);
+				curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
+				curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
+				curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
+				curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($post));
+				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+				curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,600);
+				$res = curl_exec($ch);
+				
+				//$this->logic_ding->sendToDing("独立站获取订单的店铺执行请求参数【".$name."】".json_encode($post));
+				//$this->logic_ding->sendToDing("独立站获取订单的店铺执行获取信息".$res);
+				curl_close($ch);
+				$res = json_decode($res,true);
+
+				$thatDayOrder = array();
+				if(isset($res['orders']))
+				{
+					foreach ($res['orders'] as $v)
+					{
+						//$this->logic_ding->sendToDing("独立站获取订单的店铺执行获取信息".json_encode($v));
+						$productNames = '';
+						$parameters = array();
+						$skus = '';
+						$quantity = '';
+						$thislink = '';
+						$price = [];
+						$row_total = [];
+						$tax_amount = [];
+						$discount_amount = [];
+						$product_ids = [];
+						foreach($v['line_items'] as $key=>$val)
+						{
+							$productNames .= ($key > 0)?','.$val['title']:$val['title'];
+							$skus .= ($key > 0)?','.$val['sku']:$val['sku'];
+							$quantity .= floor($val['quantity']).';';
+							$parameters[] = $val['variant_title'];
+							// $price[] = $val['price_set']['shop_money']['amount'];
+							// $row_total[] = $val['price'];
+							// $tax_amount[] = 0;
+							// $discount_amount[] =$val['total_discount'];
+							$product_ids[] = $val['product_id'];
+
+						}
+						if(strpos($v['name'],"#M-") !== false){
+							continue;
+						}
+						if(isset($v['gateway'])){
+							if(stripos($v['gateway'],'shopify_') !== false)
+							{
+								$pay = 'shopifypay';
+							}
+							else if(stripos($v['gateway'],'paypal') !== false)
+							{
+								$pay = 'paypal_express';
+							}
+							else if(stripos($v['gateway'],'gift_card') !== false)
+							{
+								$pay = 'gift_card';
+							}
+							else if(stripos($v['gateway'],'shop_cash') !== false)
+							{
+								$pay = 'shop_cash';
+							}
+							else if(stripos($v['gateway'],'Klarna') !== false || stripos($v['gateway'],'klarna') !== false){
+								if( (stripos($v['gateway'],'Payoneer') !== false) || (stripos($v['gateway'],'payoneer') !== false)  ){
+									$pay ="PayoneerKlarna";
+								}else{
+									$pay ="klarna_payments";
+								}
+							}	
+							else if(stripos($v['gateway'],'Afterpay') !== false || stripos($v['gateway'],'afterpay') !== false){
+								$pay ="afterpaypayovertime";
+							}
+							else if(stripos($v['gateway'],'Clearpay') !== false || stripos($v['gateway'],'clearpay') !== false){
+								$pay ="clearpaypayovertime";
+							}
+							else if(stripos($v['gateway'],'Airwallex') !== false || stripos($v['gateway'],'Airwallex') !== false){
+								$pay ="custompaymentmethod";
+							}
+							else
+							{
+								$pay = $v['gateway'];
+							}
+						}else{
+							if(count($v['payment_gateway_names'])>1){
+								$v['payment_gateway_names'] = array_reverse($v['payment_gateway_names']);
+							}
+							if(isset($v['payment_gateway_names'])&&isset($v['payment_gateway_names'][0])){
+								if(stripos($v['payment_gateway_names'][0],'shopify_') !== false)
+								{
+									$pay = 'shopifypay';
+								}
+								else if(stripos($v['payment_gateway_names'][0],'paypal') !== false)
+								{
+									$pay = 'paypal_express';
+								}
+								else if(stripos($v['payment_gateway_names'][0],'gift_card') !== false)
+								{
+									if(isset($v['payment_gateway_names'][1])){
+										$pay = $this->checkPay($v['payment_gateway_names'][1]);
+									}else{
+										$pay = 'gift_card';
+									}
+								}
+								else if(stripos($v['payment_gateway_names'][0],'shop_cash') !== false)
+								{
+									$pay = 'shop_cash';
+								}
+								else if(stripos($v['payment_gateway_names'][0],'Klarna') !== false || stripos($v['payment_gateway_names'][0],'klarna') !== false){
+
+									if( (stripos($v['payment_gateway_names'][0],'Payoneer') !== false) || (stripos($v['payment_gateway_names'][0],'payoneer') !== false)  ){
+										$pay ="PayoneerKlarna";
+									}else{
+										$pay ="klarna_payments";
+									}
+								}	
+								else if(stripos($v['payment_gateway_names'][0],'Afterpay') !== false ){
+									$pay ="afterpaypayovertime";
+								}
+								else if(stripos($v['payment_gateway_names'][0],'Airwallex') !== false ){
+									$pay ="custompaymentmethod";
+								}else if(stripos($v['payment_gateway_names'][0],'Clearpay') !== false || stripos($v['payment_gateway_names'][0],'clearpay') !== false){
+									$pay ="clearpaypayovertime";
+								}
+								else
+								{
+									$pay = isset($v['payment_gateway_names'][0])?$v['payment_gateway_names'][0]:"";
+								}
+							}else{
+								$this->logic_ding->sendToDing("订单".$v['name']."没有支付方式【".json_encode($v)."】");
+								$pay = "";
+							}
+							
+						}
+						
+						$khbz = '';
+						if(!isset($v['shipping_address']['country_code']))
+						{
+							$v['shipping_address'] = $v['billing_address'];
+							$khbz = '客户自提订单; ';
+						}
+						$shipping_address = $v['shipping_address']['address1'] .' '.$v['shipping_address']['address2']. ',' . $v['shipping_address']['city'] . ',' . $v['shipping_address']['province'] . ',' . $v['shipping_address']['zip'] . ',' . $v['shipping_address']['country_code'] . ',' . $v['shipping_address']['phone'];
+						@$billing_address = $v['billing_address']['address1'].' ' . $v['billing_address']['address2'].',' . $v['billing_address']['city'] . ',' . $v['billing_address']['province'] . ',' . $v['billing_address']['zip'] . ',' . $v['billing_address']['country_code'] . ',' . $v['billing_address']['phone'];
+						$created_at = explode('T',$v['created_at']);
+						$created_at_hi = explode('-',$created_at[1]);
+						$created_at = $created_at[0].' '.$created_at_hi[0];
+						$paypal = '';
+						$paypal_protection_eligibility = "";
+						$d = $this->authorizationv1($shop,$v['id'],$post['access_token']);
+						if($d['g'] == 1)
+						{
+							$paypal = $d['d'];
+							$paypal_protection_eligibility = $d['protection_eligibility'];
+						}
+						
+						$authorid = '';
+						$wjauthorid = '';
+						$sourcecontentid = '';
+						$link = '';
+						if($shop['tweak'] !='' && stripos($v['referring_site'],'author_id') !== false)
+						{
+							$geturl = explode('&',$v['referring_site']);
+							foreach ($geturl as $hqid)
+							{
+								if(stripos($hqid,'author_id') !== false)
+								{
+									$wjauthorid = str_replace('author_id=','',$hqid);
+								}
+								if(stripos($hqid,'source_content_id') !== false)
+								{
+									$sourcecontentid = str_replace('source_content_id=','',$hqid);
+								}
+								
+							}
+							$link = str_replace($shop['thurl'],$shop['url'],$v['referring_site']);
+						}
+						else if($shop['tweak'] !='' && stripos($v['landing_site'],'author_id') !== false)
+						{
+							$geturl = explode('&',$v['landing_site']);
+							foreach ($geturl as $hqid)
+							{
+								if(stripos($hqid,'author_id') !== false)
+								{
+									$wjauthorid = str_replace('author_id=','',$hqid);
+								}
+								if(stripos($hqid,'source_content_id') !== false)
+								{
+									$sourcecontentid = str_replace('source_content_id=','',$hqid);
+								}
+								
+							}
+							$link = $shop['url'].$v['landing_site'];
+						}
+						// if($link != '')
+						// {
+						// 	$authorid = $this->authorid($link,$shop['tweak']);
+						// }
+						$shippingmethod = '';
+						if(isset($v['shipping_lines'][0]['code'])){
+							$shippingmethod = $v['shipping_lines'][0]['code'].' '.$v['shipping_lines'][0]['discounted_price'];
+						}
+						$thatDayOrder[] = array(
+							'order_id' => $v['name'],//订单号
+							'product_name' => $productNames,//产品标题
+							'purchased_on' => strtotime($created_at),//订单时间
+							'name' => $v['shipping_address']['name'],//收货人
+							'bill_to_name' => $v['billing_address']['name'],//账单人名称
+							'ship_to_name' => $v['shipping_address']['name'],//收货人名称
+							'shipping_address' => $shipping_address,
+							'billing_address' => $billing_address,
+							'country' => $v['shipping_address']['country_code'],//收货国家二字码
+							'region' => $v['shipping_address']['province_code'],//收货省/州
+							'city' => $v['shipping_address']['city'],//收货城市
+							'street' => $v['shipping_address']['address1'].' '.$v['shipping_address']['address2'],//收货地址
+							'telephone' => $v['shipping_address']['phone'],//收货人手机
+							'postcode' => $v['shipping_address']['zip'],//收货人邮政编码
+							'email' => $v['customer']['email'],//收货人邮箱
+							'global_currency_code' => $v['currency'],//币种
+							'base_grand_total' => $v['total_price'],//应收金额
+							'grand_total' => $v['total_price_set']['shop_money']['amount'],//币种支付金额
+							'isstatus' => 'processing',//$v['order_status'],//默认processing待发货状态
+							'isparameter' => implode(';',$parameters),
+							// 'price'=>implode(",",$price),
+							// 'row_total'=>implode(",",$row_total),
+							// 'tax_amount'=>implode(",",$tax_amount),
+							// 'discount_amount'=>implode(",",$discount_amount),
+							'product_ids' => implode(",",$product_ids),
+							'sku' => $skus,
+							'order_quantity' => $quantity,
+							'order_comment' => $khbz.addslashes($v['note']),//客户备注
+							'pay' => $pay,
+							'paypal_num' => $paypal,
+							'insurance' => 0,
+							'paypal_protection_eligibility' => $paypal_protection_eligibility,
+							'shippingmethod'=>$shippingmethod,
+							'link'=>$link,//产品链接
+							'shopify'=>$v['id'],
+							'authorid'=>$authorid,
+							'wjauthorid'=>$wjauthorid,
+							'sourcecontentid'=>$sourcecontentid
+						);
+					}
+					return array_reverse($thatDayOrder);
+				}
+			
 		}
 		else if($name == '123')
 		{
@@ -632,6 +891,150 @@ function __construct(){
 			return $author_id['plainText'];
 		}
 	}
+
+	public function authorizationv1($shop,$shopifyid,$access_token){
+		var_dump($shopifyid);
+		$post = [];
+		$post['shopify_pay'] = [
+			"url"=> 'https://'.$shop['shopadmin'].'/admin/api/2022-10/orders/'.$shopifyid.'/transactions.json',
+			"token"=>$access_token,
+		];
+		$ch = curl_init();
+		curl_setopt($ch,CURLOPT_URL,'https://edm.alipearl.net/soapapi.php');
+		curl_setopt($ch, CURLOPT_POST, 1);
+		curl_setopt($ch, CURLOPT_HEADER, 0);
+		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
+		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
+		curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
+		curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($post));
+		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+		curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1800000);
+		$res = curl_exec($ch);
+		curl_close($ch);
+		//$this->logic_ding->sendToDing("独立站获取订单的店铺执行shopfiy支付信息【".$shopifyid."】".$res."}{".json_encode($post));
+		// $shopifyid = $shopifyid;
+		// $url = 'https://'.$shop['shopname'].'.myshopify.com/admin/api/2022-10/orders/'.$shopifyid.'/transactions.json';
+        // 	$headerA[] = "X-Shopify-Access-Token: ".$shop['token'];
+        // 	$ch = curl_init();
+        // 	curl_setopt($ch, CURLOPT_URL, $url);
+        // 	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+        // 	curl_setopt($ch, CURLOPT_HTTPHEADER, $headerA);
+        // 	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+        // 	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
+        // 	$res = curl_exec($ch);
+		// 	$headerA = curl_getinfo($ch)
+		$a = json_decode($res,true);
+
+		if(!isset($a['transactions']))
+		{
+			return array('g'=>0);exit;
+		}
+		//没有交易信息
+		if(empty($a['transactions'])){
+			return array('g'=>0);exit;
+		}
+		$tmp_pay_list = [];
+		foreach ($a['transactions'] as $v){
+			if(stripos($v['gateway'],'gift_card') === false){
+				$tmp_pay_list[] = $v;
+			}
+		}
+		$a['transactions'] = $tmp_pay_list;
+		if(count($a['transactions']) < 2){
+			//
+			if($a['transactions'][0]['gateway'] == 'Klarna' ){
+				return array('g'=>1,'d'=>$a['transactions'][0]['payment_id'],'protection_eligibility'=>"");
+			}elseif(stripos($a['transactions'][0]['gateway'],'Payoneer Checkout Klarna') !== false){
+				return array('g'=>1,'d'=>$a['transactions'][0]['payment_id'],'protection_eligibility'=>"");
+			}elseif(stripos($a['transactions'][0]['gateway'],'Afterpay') !== false){
+				return array('g'=>1,'d'=>$a['transactions'][0]['payment_id'],'protection_eligibility'=>"");
+			}elseif(stripos($a['transactions'][0]['gateway'],'Airwallex') !== false){
+				return array('g'=>1,'d'=>$a['transactions'][0]['payment_id'],'protection_eligibility'=>"");
+			}elseif(stripos($a['transactions'][0]['gateway'],'Clearpay') !== false){
+				return array('g'=>1,'d'=>$a['transactions'][0]['payment_id'],'protection_eligibility'=>"");
+			}else{
+				$a['transactions'] = array_reverse($a['transactions']);
+				if(isset($a['transactions'][0]['authorization']))
+				{
+					$protection_eligibility = "";
+					if(isset($a['transactions'][0]['receipt'])){
+						if(isset($a['transactions'][0]['receipt']['protection_eligibility'])){
+							$protection_eligibility = $a['transactions'][0]['receipt']['protection_eligibility'];
+						}elseif(isset($a['transactions'][0]['receipt']['purchase_units'])){
+							if(!empty($a['transactions'][0]['receipt']['purchase_units'])){
+								if(!empty($a['transactions'][0]['receipt']['purchase_units'][0]['payments'])){
+									if(isset($a['transactions'][0]['receipt']['purchase_units'][0]['payments']['captures'][0]['seller_protection'])){
+										if(isset($a['transactions'][0]['receipt']['purchase_units'][0]['payments']['captures'][0]['seller_protection']['status'])){
+											$protection_eligibility = $a['transactions'][0]['receipt']['purchase_units'][0]['payments']['captures'][0]['seller_protection']['status'];
+										}
+									}
+								}
+								
+							}
+						}
+					}
+					if(!empty($protection_eligibility)){
+						$protection_eligibility = ucfirst(strtolower($protection_eligibility));
+						if(stripos($protection_eligibility,'not') !== false){
+							$protection_eligibility = 'Ineligible';
+						}
+					}
+					return array('g'=>1,'d'=>$a['transactions'][0]['authorization'],'protection_eligibility'=>$protection_eligibility);
+				}
+				else
+				{
+					return array('g'=>0);
+				}
+			}
+		}else{
+			$end_info = end($a['transactions']);
+			if($end_info['gateway'] == 'Klarna' ){
+				return array('g'=>1,'d'=>$end_info['payment_id'],'protection_eligibility'=>"");
+			}elseif(stripos($end_info['gateway'],'Payoneer Checkout Klarna') !== false){
+				return array('g'=>1,'d'=>$end_info['payment_id'],'protection_eligibility'=>"");
+			}elseif(stripos($end_info['gateway'],'Afterpay') !== false){
+				return array('g'=>1,'d'=>$end_info['payment_id'],'protection_eligibility'=>"");
+			}elseif(stripos($end_info['gateway'],'Airwallex') !== false){
+				return array('g'=>1,'d'=>$end_info['payment_id'],'protection_eligibility'=>"");
+			}elseif(stripos($end_info['gateway'],'Clearpay') !== false){
+				return array('g'=>1,'d'=>$end_info['payment_id'],'protection_eligibility'=>"");
+			}
+			else{
+				$a['transactions'] = array_reverse($a['transactions']);
+				if(isset($a['transactions'][0]['authorization']))
+				{
+					$protection_eligibility = "";
+					if(isset($a['transactions'][0]['receipt'])){
+						if(isset($a['transactions'][0]['receipt']['protection_eligibility'])){
+							$protection_eligibility = $a['transactions'][0]['receipt']['protection_eligibility'];
+						}elseif(isset($a['transactions'][0]['receipt']['purchase_units'])){
+							if(!empty($a['transactions'][0]['receipt']['purchase_units'])){
+								if(!empty($a['transactions'][0]['receipt']['purchase_units'][0]['payments'])){
+									if(isset($a['transactions'][0]['receipt']['purchase_units'][0]['payments']['captures'][0]['seller_protection'])){
+										if(isset($a['transactions'][0]['receipt']['purchase_units'][0]['payments']['captures'][0]['seller_protection']['status'])){
+											$protection_eligibility = $a['transactions'][0]['receipt']['purchase_units'][0]['payments']['captures'][0]['seller_protection']['status'];
+										}
+									}
+								}
+								
+							}
+						}
+					}
+					if(!empty($protection_eligibility)){
+						$protection_eligibility = ucfirst(strtolower($protection_eligibility));
+						if(stripos($protection_eligibility,'not') !== false){
+							$protection_eligibility = 'Ineligible';
+						}
+					}
+					return array('g'=>1,'d'=>$a['transactions'][0]['authorization'],'protection_eligibility'=>$protection_eligibility);
+				}
+				else
+				{
+					return array('g'=>0);
+				}
+			}
+		}
+	}
 	public function authorization($shop,$shopifyid)//shopify获取交易号
     {
 		if($shop['brandname'] == 'yolissahair'){

+ 60 - 0
core/CoreApp/models/Model_shopifyv1.php

@@ -0,0 +1,60 @@
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
+class Model_shopifyv1 extends Lin_Model 
+{
+    function __construct(){
+        parent::__construct();
+    }
+
+
+    public function getAccessToken($shop_info){
+        $this->load->driver('cache');
+        $cache_key = 'shopify_dlz_'. $shop_info['id'];
+        $cached = $this->cache->get($cache_key);
+        if(!empty($cached)){
+            return $cached;
+        }
+        $cache = $this->getAccessTokenByCurl($shop_info);
+        if(empty($cache)){
+            return "";
+        }
+		$this->cache->save($cache_key,$cache , 3600*12); // 缓存12小时
+        return $cache;
+    }
+
+
+    public function getAccessTokenByCurl($shop_info){
+        $shopify_store = $shop_info['shopadmin'];
+		$client_id = $shop_info['app_key'];
+		$client_secret = $shop_info['app_secret'];
+		$token_url = "https://{$shopify_store}/admin/oauth/access_token";
+		$post_data = [
+			'client_id' => $client_id,
+			'client_secret' => $client_secret,
+			'grant_type' => 'client_credentials'
+		];
+
+		$post = [];
+		$post['shopify_accesstoken'] = 1;
+		$post['url'] = $token_url;
+		$post['data'] = $post_data;
+
+		$ch = curl_init();
+		curl_setopt($ch,CURLOPT_URL,'https://edm.alipearl.net/soapapi.php');
+		curl_setopt($ch, CURLOPT_POST, 1);
+		curl_setopt($ch, CURLOPT_HEADER, 0);
+		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
+		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
+		curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
+		curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($post));
+		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+		curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1800000);
+		$res = curl_exec($ch);
+		curl_close($ch);
+	    $token_result = json_decode($res,true);
+		if(!isset($token_result['access_token'])){
+            return ""; 
+        }
+        return $token_result['access_token'];
+    }
+
+}