Last active
November 24, 2021 09:17
-
-
Save zorem/5e0354304efbc6d1580c4a3dc11b8d44 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* slug: smswoo_clicksend | |
* replace slug in the code | |
*/ | |
add_filter( 'smswoo_sms_provider_array', 'smswoo_sms_provider_array', 10, 1 ); | |
function smswoo_sms_provider_array( $settings ){ | |
$settings['smswoo_sms_provider']['options']['smswoo_clicksend'] = 'ClickSend'; | |
// add the settings to SMS section so you can retrive SMS cridential | |
$settings['smswoo_clicksend_username'] = array( | |
'title' => __( 'Clicksend API Username', 'trackship-for-woocommerce' ), | |
'type' => 'text', | |
'show' => true, | |
'id' => 'smswoo_clicksend_username', | |
'class' => 'smswoo_sms_provider smswoo_clicksend_sms_provider', | |
); | |
$settings['smswoo_clicksend_key'] = array( | |
'title' => __( 'Clicksend API key', 'trackship-for-woocommerce' ), | |
'type' => 'text', | |
'show' => true, | |
'id' => 'smswoo_clicksend_key', | |
'class' => 'smswoo_sms_provider smswoo_clicksend_sms_provider', | |
); | |
//make sure to add slug in the class for sender phon number | |
$settings['smswoo_sender_phone_number']['class'] = $settings['smswoo_sender_phone_number']['class'] . ' smswoo_clicksend_sms_provider '; | |
return $settings; | |
} | |
/* | |
* Make sure that class name is same as slug | |
* slug: smswoo_clicksend | |
*/ | |
if ( ! class_exists( 'smswoo_clicksend' ) ) { | |
/** | |
* | |
* @class smswoo_clicksend | |
* @since 1.0 | |
* | |
* Make sure that you extend the class smswoo_sms_gateway | |
* | |
*/ | |
class smswoo_clicksend extends smswoo_sms_gateway { | |
private $_clicksend_authkey; | |
/** | |
* Constructor | |
* | |
* @since 1.0 | |
* @return void | |
*/ | |
public function __construct() { | |
// get cridential from settings fields | |
$this->_clicksend_username = get_option( 'smswoo_clicksend_username' ); | |
$this->_clicksend_key = get_option( 'smswoo_clicksend_key' ); | |
parent::__construct(); | |
} | |
/** | |
* Send SMS | |
* | |
* @since 1.0 | |
* | |
* @param $to_phone string | |
* @param $message string | |
* @param $country_code string | |
* | |
* @return void | |
* @throws Exception for WP HTTP API error, no response, HTTP status code is not 201 or if HTTP status code not set | |
*/ | |
public function send( $to_phone, $message, $country_code ) { | |
if ( '' != $this->_from_asid ) { | |
$from = $this->_from_asid; | |
} else { | |
$from = $this->_from_number; | |
} | |
$type = empty( apply_filters( 'smswoo_additional_charsets', get_option( 'smswoo_active_charsets', array() ) ) ) ? 'english' : 'unicode'; | |
$body = array( | |
'messages' => array( | |
array( | |
'to' => $to_phone, | |
'source' => "smswoo", | |
'body' => $message, | |
), | |
), | |
); | |
$args = array( | |
'body' => wp_json_encode( $body ), | |
'headers' => array( | |
'Authorization' => "Basic " . base64_encode( $this->_clicksend_username.":".$this->_clicksend_key ), | |
'Content-Type' => 'application/json', | |
), | |
); | |
$url = 'https://rest.clicksend.com/v3/sms/send'; | |
$response = wp_safe_remote_post( $url, $args); | |
if ( is_wp_error( $response ) ) { | |
throw new Exception( $response->get_error_message() ); | |
} | |
$this->_log[] = $response; | |
// Check for proper response / body | |
if ( ! isset( $response['response'] ) || ! isset( $response['body'] ) ) { | |
throw new Exception( __( 'No answer', 'trackship-for-woocommerce' ) ); | |
} | |
$result = json_decode( $response['body'], true ); | |
if ( $result['response_code'] != 'SUCCESS' ) { | |
throw new Exception( sprintf( __( 'An error has occurred: %s', 'trackship-for-woocommerce' ), $result['response_msg'] ) ); | |
} | |
if ( $result[ 'data' ][ 'messages' ][0]['status'] != 'SUCCESS' ) { | |
throw new Exception( sprintf( __( 'An error has occurred: %s', 'trackship-for-woocommerce' ), $result[ 'data' ][ 'messages' ][0]['status'] ) ); | |
} | |
return; | |
} | |
/** | |
* Send SMS | |
* | |
* @since 1.0 | |
* | |
* @param $to_phone string | |
* | |
* @return void | |
* @throws Exception for WP HTTP API error, no response, HTTP status code is not 201 or if HTTP status code not set | |
*/ | |
public function validate_number( $to_phone ) { | |
throw new Exception( sprintf( __( 'An error has occurred: Clicksend is not supported for phone number validation on checkout, Please contact support', 'trackship-for-woocommerce' ) ) ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment