Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5 create new admin setting for client id client secrect key for getting access token #7

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ public function register_settings( $settings ) {
'type' => 'text',
'default' => '',
],
[
'name' => esc_html__( 'Client Id', 'instamojo-for-give' ),
'desc' => esc_html__( 'Please enter the client id from your LIVE Instamojo Account.', 'instamojo-for-give' ),
'id' => 'instamojo_get_client_id',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ankur0812 rename the id to use mg_instamojo_for_givewp_client_id and similar for client secret mg_instamojo_for_givewp_client_secret

'type' => 'text',
'default' => '',
],
[
'name' => esc_html__( 'Client Secret', 'instamojo-for-give' ),
'desc' => esc_html__( 'Please enter the client secret from your LIVE Instamojo Account.', 'instamojo-for-give' ),
'id' => 'instamojo_get_client_secret',
'type' => 'text',
'default' => '',
],
[
'name' => esc_html__( 'Test - Private API Key', 'instamojo-for-give' ),
'desc' => esc_html__( 'Please enter the private API key from your TEST Instamojo Account.', 'instamojo-for-give' ),
Expand Down
41 changes: 40 additions & 1 deletion src/Includes/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,44 @@ public static function get_private_auth_token() {
return trim( $token );
}

/**
* Get Access Token.
*
* @since 1.0.0
* @access public
*
* @return string
*/
public static function get_access_token() {
$client_id = give_get_option( 'instamojo_get_client_id' );
$client_secret = give_get_option( 'instamojo_get_client_secret' );

if ( give_is_test_mode() ) {
// $token = give_get_option( 'instamojo_get_test_auth_token' );
}

$payload_data = [
'grant_type' => 'client_credentials',
'client_id' => $client_id,
'client_secret' => $client_secret
];

$url = 'https://api.instamojo.com/oauth2/token/';

$args = [
'body' => $payload_data,
];

$response = wp_remote_post( $url, $args );
$response_body = json_decode( wp_remote_retrieve_body( $response ) );
$response_code = json_decode( wp_remote_retrieve_response_code( $response ) );
if(200 === $response_code) {
return $response_body->access_token;
} else {
return '';
}
}

/**
* Get Private Salt.
*
Expand Down Expand Up @@ -97,7 +135,8 @@ public static function get_private_salt() {
* @return array
*/
public static function get_headers() {
$access_token = self::get_private_auth_token();
//$access_token = self::get_private_auth_token();
$access_token = self::get_access_token();
return [
'Authorization' => "Bearer {$access_token}",
];
Expand Down