'', 'username' => '', 'password' => '', 'version' => '3.6.8', 'settlement_type_id' => '', 'settlement_day' => '0', 'warehouse_id' => '', 'credit_id' => '', 'dry_run' => '1', ); $settings = get_option( 'kara_wc_settings', array() ); return wp_parse_args( $settings, $defaults ); } private static function clean_base_url( $url ) { return rtrim( trim( $url ), '/' ); } private static function request( $endpoint, $method = 'GET', $query = array(), $body = null ) { $s = self::settings(); $base_url = self::clean_base_url( $s['base_url'] ); if ( empty( $base_url ) ) { return array( 'success' => false, 'message' => 'آدرس کارا وارد نشده است.' ); } $url = $base_url . '/' . ltrim( $endpoint, '/' ); if ( ! empty( $query ) ) { $url = add_query_arg( $query, $url ); } $args = array( 'method' => $method, 'timeout' => 60, 'headers' => array( 'Accept' => 'application/json', ), ); if ( null !== $body ) { $args['headers']['Content-Type'] = 'application/json'; $args['body'] = wp_json_encode( $body, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ); } $response = wp_remote_request( $url, $args ); if ( is_wp_error( $response ) ) { return array( 'success' => false, 'message' => $response->get_error_message(), ); } $http_code = wp_remote_retrieve_response_code( $response ); $raw_body = wp_remote_retrieve_body( $response ); $decoded = json_decode( $raw_body, true ); return array( 'success' => ( $http_code >= 200 && $http_code < 300 ), 'http_code' => $http_code, 'body' => $decoded !== null ? $decoded : $raw_body, 'raw_body' => $raw_body, ); } /** * Login */ public static function login() { $s = self::settings(); return self::request( '/MobileApp/General/Login', 'GET', array( 'Username' => $s['username'], 'Password' => $s['password'], ) ); } /** * دریافت کالاها */ public static function get_stuffs() { $s = self::settings(); return self::request( '/MobileApp/General/GetStuffs', 'GET', array( 'RequestId' => wp_generate_uuid4(), 'UserName' => $s['username'], 'Password' => $s['password'], 'From' => 1, 'Count' => 2001, 'CurrentVersionNumber' => $s['version'], ) ); } /** * دریافت مشتریان */ public static function get_partners( $from = 1, $count = 3874 ) { $s = self::settings(); return self::request( '/MobileApp/General/GetPartners', 'GET', array( 'RequestId' => wp_generate_uuid4(), 'UserName' => $s['username'], 'Password' => $s['password'], 'From' => $from, 'Count' => $count, 'CurrentVersionNumber' => $s['version'], ) ); } /** * جستجوی بازگشتی در آرایه * * برای پیدا کردن DynamicGroups یا هر ساختار مشابه */ private static function find_dynamic_group_recursive( $data, $target_name = 'فروش سایت' ) { if ( ! is_array( $data ) ) { return false; } foreach ( $data as $key => $value ) { /* * اگر به آرایه‌ای رسیدیم که خودش یک گروه است، * اسم گروه را بررسی می‌کنیم. */ if ( is_array( $value ) ) { $name_candidates = array( 'Name', 'name', 'Title', 'title', 'GroupName', 'groupName', 'Description', 'description', ); $found_name = ''; foreach ( $name_candidates as $name_key ) { if ( isset( $value[ $name_key ] ) && is_scalar( $value[ $name_key ] ) ) { $found_name = trim( (string) $value[ $name_key ] ); break; } } if ( $found_name !== '' && $found_name === $target_name ) { /* * اولویت با GroupCode */ $id_candidates = array( 'GroupCode', 'groupCode', 'Code', 'code', 'Id', 'ID', 'id', 'GroupId', 'groupId', ); foreach ( $id_candidates as $id_key ) { if ( isset( $value[ $id_key ] ) && is_scalar( $value[ $id_key ] ) && trim( (string) $value[ $id_key ] ) !== '' ) { return array( 'name' => $found_name, 'value' => trim( (string) $value[ $id_key ] ), 'key' => $id_key, 'data' => $value, ); } } } $nested = self::find_dynamic_group_recursive( $value, $target_name ); if ( false !== $nested ) { return $nested; } } } return false; } /** * پیدا کردن گروه فروش سایت */ public static function find_sales_group() { $response = self::get_partners( 1, 3874 ); if ( empty( $response['success'] ) ) { return array( 'success' => false, 'message' => 'دریافت اطلاعات مشتریان از کارا ناموفق بود.', 'response' => $response, ); } $data = $response['body']; if ( isset( $data['Data'] ) ) { $data = $data['Data']; } /* * اول مستقیماً DynamicGroups را بررسی می‌کنیم. */ if ( isset( $data['DynamicGroups'] ) ) { $found = self::find_dynamic_group_recursive( $data['DynamicGroups'], 'فروش سایت' ); if ( false !== $found ) { return array( 'success' => true, 'message' => 'گروه فروش سایت پیدا شد.', 'group' => $found, ); } } /* * اگر DynamicGroups در سطح دیگری بود، * کل Data را جستجو می‌کنیم. */ $found = self::find_dynamic_group_recursive( $data, 'فروش سایت' ); if ( false === $found ) { return array( 'success' => false, 'message' => 'گروه «فروش سایت» در DynamicGroups پیدا نشد.', 'data_keys' => is_array( $data ) ? array_keys( $data ) : array(), ); } return array( 'success' => true, 'message' => 'گروه فروش سایت پیدا شد.', 'group' => $found, ); } /** * ثبت مشتری در کارا */ public static function submit_partner( $customer = array() ) { $s = self::settings(); /* * گرفتن خودکار گروه فروش سایت */ $group_result = self::find_sales_group(); if ( empty( $group_result['success'] ) ) { return array( 'success' => false, 'message' => $group_result['message'], 'group_result' => $group_result, ); } $group_code = $group_result['group']['value']; /* * اگر چند GroupCode به صورت pipe جدا شده باشند، * همان مقدار را بدون تغییر می‌فرستیم. */ $id = wp_generate_uuid4(); $partner_id = ! empty( $customer['PartnerId'] ) ? $customer['PartnerId'] : wp_generate_uuid4(); $payload = array( 'UserName' => $s['username'], 'Password' => $s['password'], 'CurrentVersionNumber' => ! empty( $s['version'] ) ? $s['version'] : '3.6.8', 'Id' => $id, 'PartnerId' => $partner_id, 'FirstName' => isset( $customer['FirstName'] ) ? $customer['FirstName'] : '', 'LastName' => isset( $customer['LastName'] ) ? $customer['LastName'] : '', 'LegalName' => isset( $customer['LegalName'] ) ? $customer['LegalName'] : '', 'ZoneId' => isset( $customer['ZoneId'] ) ? $customer['ZoneId'] : '', 'Phone1' => isset( $customer['Phone1'] ) ? $customer['Phone1'] : '', 'Phone2' => isset( $customer['Phone2'] ) ? $customer['Phone2'] : '', 'Mobile' => isset( $customer['Mobile'] ) ? $customer['Mobile'] : '', 'Fax' => isset( $customer['Fax'] ) ? $customer['Fax'] : '', 'Address' => isset( $customer['Address'] ) ? $customer['Address'] : '', 'IsLegal' => false, 'CalculateVATForThisPerson' => true, 'CreditId' => ! empty( $customer['CreditId'] ) ? $customer['CreditId'] : $s['credit_id'], /* * مهم: * دیگر از تنظیمات GroupCode نمی‌خوانیم. */ 'GroupCode' => $group_code, 'SettlementTypeId' => ! empty( $customer['SettlementTypeId'] ) ? $customer['SettlementTypeId'] : $s['settlement_type_id'], 'SettlementDay' => ! empty( $customer['SettlementDay'] ) ? $customer['SettlementDay'] : $s['settlement_day'], 'GeoLocationLat' => isset( $customer['GeoLocationLat'] ) ? $customer['GeoLocationLat'] : '', 'GeoLocationLong' => isset( $customer['GeoLocationLong'] ) ? $customer['GeoLocationLong'] : '', 'totalPrice' => isset( $customer['totalPrice'] ) ? $customer['totalPrice'] : 0, ); /* * حالت تست */ if ( ! empty( $s['dry_run'] ) && $s['dry_run'] === '1' ) { return array( 'success' => true, 'dry_run' => true, 'message' => 'حالت تست فعال است؛ اطلاعات مشتری به کارا ارسال نشد.', 'data' => $payload, 'sales_group' => $group_result, ); } /* * SubmitPartnerNew طبق Postman به صورت POST * و پارامترها در QueryString ارسال می‌شوند. */ $response = self::request( '/MobileApp/General/SubmitPartnerNew', 'POST', $payload ); return array( 'success' => ! empty( $response['success'] ), 'message' => ! empty( $response['success'] ) ? 'اطلاعات مشتری به کارا ارسال شد.' : 'ارسال مشتری به کارا ناموفق بود.', 'request' => $payload, 'response' => $response, ); } } 5 – فروشگاه اینترنتی آیسان پخش
💳 خرید راحت‌تر با آیسان پخش، امکان خرید اقساطی در ۴ قسط با «دیجی‌پی» فراهم شد.
شما این محصولات را انتخاب کرده اید0
empty-cart

هیچ محصولی در سبد خرید نیست.

جهت مشاهده محصولات بیشتر به صفحات زیر مراجعه نمایید.