PluginProbe
Texty – SMS Notification for WordPress, WooCommerce, Dokan and more / 2.0.0
Texty – SMS Notification for WordPress, WooCommerce, Dokan and more v2.0.0
2.0.2 trunk 0.2 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 2.0.0 2.0.1
texty / includes / Gateways / Vonage.php

Vonage.php in Texty – SMS Notification for WordPress, WooCommerce, Dokan and more 2.0.0, at includes/Gateways/Vonage.php

161 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Texty\Gateways;
4
5 use WP_Error;
6 use WP_REST_Request;
7
8 /**
9 * Nexmo Class
10 *
11 * @see https://developer.nexmo.com/api/sms
12 */
13 class Vonage implements GatewayInterface {
14
15 /**
16 * API Endpoint
17 */
18 const ENDPOINT = 'https://rest.nexmo.com';
19
20 /**
21 * Get the name
22 *
23 * @return string
24 */
25 public function name() {
26 return __( 'Vonage (nexmo)', 'texty' );
27 }
28
29 /**
30 * Get the logo
31 *
32 * @return string
33 */
34 public function logo() {
35 return TEXTY_URL . '/assets/images/vonage-logo.png';
36 }
37
38 /**
39 * Get the name
40 *
41 * @return string
42 */
43 public function description() {
44 return sprintf(
45 // translators: Vonage dashboard settings URL and help docs
46 __(
47 'Send SMS with Vonage (formerly Nexmo). Follow <a href="%1$s" target="_blank">this link</a> to get the API Key and Secret from Vonage. Follow <a href="%2$s" target="_blank">these instructions</a> to configure the gateway.',
48 'texty'
49 ),
50 'https://dashboard.nexmo.com/settings',
51 'https://github.com/weDevsOfficial/texty/wiki/Vonage'
52 );
53 }
54
55 /**
56 * Get the settings
57 *
58 * @return array
59 */
60 public function get_settings() {
61 $creds = texty()->settings()->get( 'vonage' );
62
63 return [
64 'key' => [
65 'name' => __( 'API Key', 'texty' ),
66 'type' => 'text',
67 'value' => isset( $creds['key'] ) ? $creds['key'] : '',
68 'help' => '',
69 ],
70 'secret' => [
71 'name' => __( 'API Secret', 'texty' ),
72 'type' => 'password',
73 'value' => isset( $creds['secret'] ) ? $creds['secret'] : '',
74 'help' => '',
75 ],
76 'from' => [
77 'name' => __( 'From Number', 'texty' ),
78 'type' => 'text',
79 'value' => isset( $creds['from'] ) ? $creds['from'] : '',
80 'help' => '',
81 ],
82 ];
83 }
84
85 /**
86 * Send SMS
87 *
88 * @param string $to
89 * @param string $message
90 *
91 * @return WP_Error|array
92 */
93 public function send( $to, $message ) {
94 $creds = texty()->settings()->get( 'vonage' );
95
96 $args = [
97 'body' => [
98 'from' => $creds['from'],
99 'text' => $message,
100 'to' => $to,
101 'api_key' => $creds['key'],
102 'api_secret' => $creds['secret'],
103 ],
104 ];
105
106 $request = wp_remote_post( self::ENDPOINT . '/sms/json', $args );
107 $body = json_decode( wp_remote_retrieve_body( $request ) );
108 $response_code = wp_remote_retrieve_response_code( $request );
109
110 if ( is_wp_error( $request ) ) {
111 return $request;
112 }
113
114 if ( $body->messages[0]->status !== '0' ) {
115 return new WP_Error(
116 $body->messages[0]->status,
117 $body->messages[0]->{'error-text'}
118 );
119 }
120
121 return [
122 'success' => true,
123 'reference_id' => isset( $body->messages[0]->{'message-id'} ) ? $body->messages[0]->{'message-id'} : null,
124 ];
125 }
126
127 /**
128 * Validate a REST API request
129 *
130 * @param WP_REST_Request $request
131 *
132 * @return WP_Error|mixed
133 */
134 public function validate( $request ) {
135 $creds = $request->get_param( 'vonage' );
136
137 $args = [
138 'api_key' => $creds['key'],
139 'api_secret' => $creds['secret'],
140 ];
141
142 $endpoint = self::ENDPOINT . '/account/get-balance?' . http_build_query( $args );
143 $response = wp_remote_get( $endpoint, $args );
144 $body = json_decode( wp_remote_retrieve_body( $response ) );
145 $response_code = wp_remote_retrieve_response_code( $response );
146
147 if ( 401 === $response_code ) {
148 return new WP_Error(
149 $body->{'error-code'},
150 $body->{'error-code-label'}
151 );
152 }
153
154 return [
155 'key' => $creds['key'],
156 'secret' => $creds['secret'],
157 'from' => $creds['from'],
158 ];
159 }
160 }
161