Changeset 2528934
- Timestamp:
- 05/10/2021 09:09:18 AM (5 years ago)
- File:
-
- 1 edited
-
usetada/trunk/includes/class.usetada-api.php (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
usetada/trunk/includes/class.usetada-api.php
r2528851 r2528934 1 <?php if ( ! defined( 'ABSPATH' ) ) { die( 'Forbidden' ); } 1 <?php if (!defined('ABSPATH')) { 2 die('Forbidden'); 3 } 2 4 3 class USETADA_API{ 5 class USETADA_API 6 { 4 7 5 8 /** 6 9 * Request token 7 10 */ 8 public static function request_token() { 11 public static function request_token() 12 { 9 13 10 $key = USETADA_Settings::get( 'apikey');11 $secret = USETADA_Settings::get( 'apisecret');14 $key = USETADA_Settings::get('apikey'); 15 $secret = USETADA_Settings::get('apisecret'); 12 16 13 $response = wp_remote_post( 'https://api.gift.id/oauth/token', array(14 'body' => json_encode( array(15 'username' => USETADA_Settings::get( 'username'),16 'password' => USETADA_Settings::get( 'password'),17 $response = wp_remote_post('https://api.gift.id/oauth/token', array( 18 'body' => json_encode(array( 19 'username' => USETADA_Settings::get('username'), 20 'password' => USETADA_Settings::get('password'), 17 21 'grant_type' => 'password', 18 22 'scope' => 'offline_access' 19 ) ),23 )), 20 24 'headers' => array( 21 25 'Content-Type' => 'application/json', 22 'Authorization' => 'Basic ' . base64_encode( $key . ':' . $secret),26 'Authorization' => 'Basic ' . base64_encode($key . ':' . $secret), 23 27 ), 24 ));28 )); 25 29 26 if ( is_wp_error( $response ) ){27 wp_die( $response);30 if (is_wp_error($response)) { 31 wp_die($response); 28 32 } else { 29 $retrieve = json_decode( $response['body']);33 $retrieve = json_decode($response['body']); 30 34 return $retrieve->access_token; 31 35 } … … 35 39 * Topup by phone number 36 40 */ 37 public static function topup_by_phone( $phone_number, $order_id, $order_total, $payment_method = 'cash', $order_items ){ 41 public static function topup_by_phone($phone_number, $order_id, $order_total, $payment_method = 'cash', $order_items) 42 { 38 43 39 if ( ! self::check_user_data() || USETADA_Settings::get('enabled') == '0')44 if (!self::check_user_data() || USETADA_Settings::get('enabled') == '0') 40 45 return false; 41 46 42 47 $token = self::request_token(); 43 $cashier_pin = USETADA_Settings::get( 'cashier_pin');44 $wallet_id = USETADA_Settings::get( 'wallet_id');48 $cashier_pin = USETADA_Settings::get('cashier_pin'); 49 $wallet_id = USETADA_Settings::get('wallet_id'); 45 50 46 51 $param = array( 47 'phone' => self::format_phone_number( $phone_number),52 'phone' => self::format_phone_number($phone_number), 48 53 'amount' => (int)$order_total, 49 'programId' => USETADA_Settings::get( 'program_id'),54 'programId' => USETADA_Settings::get('program_id'), 50 55 'billNumber' => (string)$order_id, 51 56 'paymentMethod' => $payment_method, … … 58 63 ); 59 64 60 if ( ! empty( $cashier_pin ) ){65 if (!empty($cashier_pin)) { 61 66 $param['cashierPin'] = (int)$cashier_pin; 62 67 } 63 68 64 if ( ! empty( $wallet_id ) ){69 if (!empty($wallet_id)) { 65 70 $param['walletId'] = $wallet_id; 66 71 } 67 72 68 73 // Add decimals header if decimals enable in setting 69 $decimals = get_option( 'woocommerce_price_num_decimals');70 if ( $decimals)74 $decimals = get_option('woocommerce_price_num_decimals'); 75 if ($decimals) 71 76 $headers['x-vnd-app-use-decimal'] = '1'; 72 77 73 $response = wp_remote_post( 'https://api.gift.id/v1/pos/phone/topup', array(74 'body' => json_encode( $param),78 $response = wp_remote_post('https://api.gift.id/v1/pos/phone/topup', array( 79 'body' => json_encode($param), 75 80 'headers' => $headers 76 ) );81 )); 77 82 78 if ( is_wp_error( $response ) ){79 wp_die( $response);83 if (is_wp_error($response)) { 84 wp_die($response); 80 85 } else { 81 86 return $response['body']; 82 87 } 83 84 88 } 85 89 … … 89 93 * @return bool whether required data is completed or no 90 94 */ 91 public static function check_user_data(){ 92 $settings = get_option( 'usetada_settings', USETADA_Settings::$default ); 93 if( empty( $settings['username'] ) || empty( $settings['password'] ) || empty( $settings['apikey'] ) || empty( $settings['apisecret'] ) ){ 95 public static function check_user_data() 96 { 97 $settings = get_option('usetada_settings', USETADA_Settings::$default); 98 if (empty($settings['username']) || empty($settings['password']) || empty($settings['apikey']) || empty($settings['apisecret'])) { 94 99 return false; 95 100 } else { … … 101 106 * Format phone number 102 107 */ 103 public static function format_phone_number( $number ){ 104 $countryCode = '+62'; 108 public static function format_phone_number($number) 109 { 110 $countryCode = '62'; 105 111 106 $isCountryCodeOfNumberIsMalaysia = (substr($number, 0, 2) == '01'); 107 108 if ($isCountryCodeOfNumberIsMalaysia) $countryCode = '+60'; 109 110 $internationalNumber = preg_replace('/^0/', $countryCode, $number); 111 $internationalNumber = preg_replace('/^62/', '+62', $internationalNumber); 112 112 $malaysiaPhoneCode = array("01", "60"); 113 $isCountryCodeOfNumberIsMalaysia = (in_array(substr($number, 0, 2), $malaysiaPhoneCode)); 114 115 if ($isCountryCodeOfNumberIsMalaysia) $countryCode = '60'; 116 117 $internationalNumber = preg_replace('/^0/', '+' . $countryCode . '', $number); 118 $internationalNumber = preg_replace('/^' . $countryCode . '/', '+' . $countryCode . '', $internationalNumber); 119 113 120 // Remove all spaces in string. 114 121 $internationalNumber = preg_replace('/\s+/', '', $internationalNumber); 115 122 116 123 return $internationalNumber; 117 124 }
Note: See TracChangeset
for help on using the changeset viewer.