Plugin Directory

Changeset 2528934


Ignore:
Timestamp:
05/10/2021 09:09:18 AM (5 years ago)
Author:
usetada
Message:

fix: modify malaysia phone number format

File:
1 edited

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}
    24
    3 class USETADA_API{
     5class USETADA_API
     6{
    47
    58    /**
    69     * Request token
    710     */
    8     public static function request_token() {
     11    public static function request_token()
     12    {
    913
    10         $key        = USETADA_Settings::get( 'apikey' );
    11         $secret     = USETADA_Settings::get( 'apisecret' );
     14        $key        = USETADA_Settings::get('apikey');
     15        $secret     = USETADA_Settings::get('apisecret');
    1216
    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'),
    1721                'grant_type'    => 'password',
    1822                'scope'         => 'offline_access'
    19             ) ),
     23            )),
    2024            'headers' => array(
    2125                'Content-Type'      => 'application/json',
    22                 'Authorization'     => 'Basic ' . base64_encode( $key . ':' . $secret ),
     26                'Authorization'     => 'Basic ' . base64_encode($key . ':' . $secret),
    2327            ),
    24         ) );
     28        ));
    2529
    26         if( is_wp_error( $response ) ){
    27             wp_die( $response );
     30        if (is_wp_error($response)) {
     31            wp_die($response);
    2832        } else {
    29             $retrieve = json_decode( $response['body'] );
     33            $retrieve = json_decode($response['body']);
    3034            return $retrieve->access_token;
    3135        }
     
    3539     * Topup by phone number
    3640     */
    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    {
    3843
    39         if( ! self::check_user_data() || USETADA_Settings::get('enabled')  == '0' )
     44        if (!self::check_user_data() || USETADA_Settings::get('enabled')  == '0')
    4045            return false;
    4146
    4247        $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');
    4550
    4651        $param = array(
    47             'phone'             =>  self::format_phone_number( $phone_number ),
     52            'phone'             =>  self::format_phone_number($phone_number),
    4853            'amount'                => (int)$order_total,
    49             'programId'         => USETADA_Settings::get( 'program_id' ),
     54            'programId'         => USETADA_Settings::get('program_id'),
    5055            'billNumber'        => (string)$order_id,
    5156            'paymentMethod' => $payment_method,
     
    5863        );
    5964
    60         if( ! empty( $cashier_pin ) ){
     65        if (!empty($cashier_pin)) {
    6166            $param['cashierPin'] = (int)$cashier_pin;
    6267        }
    6368
    64         if( ! empty( $wallet_id ) ){
     69        if (!empty($wallet_id)) {
    6570            $param['walletId'] = $wallet_id;
    6671        }
    6772
    6873        // 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)
    7176            $headers['x-vnd-app-use-decimal']   = '1';
    7277
    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),
    7580            'headers'   => $headers
    76         ) );
     81        ));
    7782
    78         if( is_wp_error( $response ) ){
    79             wp_die( $response );
     83        if (is_wp_error($response)) {
     84            wp_die($response);
    8085        } else {
    8186            return $response['body'];
    8287        }
    83 
    8488    }
    8589
     
    8993     * @return bool whether required data is completed or no
    9094     */
    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'])) {
    9499            return false;
    95100        } else {
     
    101106     * Format phone number
    102107     */
    103     public static function format_phone_number(  $number ){
    104         $countryCode = '+62';
     108    public static function format_phone_number($number)
     109    {
     110        $countryCode = '62';
    105111
    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
    113120        // Remove all spaces in string.
    114121        $internationalNumber = preg_replace('/\s+/', '', $internationalNumber);
    115        
     122
    116123        return $internationalNumber;
    117124    }
Note: See TracChangeset for help on using the changeset viewer.