add and deduct points remotely outside wordpress system

front page Forums Community add and deduct points remotely outside wordpress system

This topic contains 3 replies, has 2 voices, and was last updated by Avatar fcpro 1 month ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #39000
    Avatar
    terungwa

    I have a use case for mycred. I need to integrate with external systems outside wordpress such that points can be transfered from one user to another based on external triggers. In addition, payment for points can be made directly to a bank account and based om my integration with that bank, points should be added to the points account of the paying customer.

    Does mycred support this feature?

    #39328
    Avatar
    terungwa

    Find code sample below:

    add_action('rest_api_init', function () {
        register_rest_route( 'myplugin/v1', 'ipoints/(?P<user_id>\d+)/(?P<qty>\d+)',array(
                      'methods'  => 'GET',
                      'callback' => 'add_iPoints'
            ));
      });
    
    function getRealIpAddr()
      {
        if ( !empty( $_SERVER['HTTP_CLIENT_IP'] ) )
        {
          $ip = $_SERVER['HTTP_CLIENT_IP'];
        }
        elseif( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
        //to check ip passed from proxy
        {
          $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else
        {
          $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }
    
    function add_iPoints($request){
        if(getRealIpAddr() != '::1'){
            die('Stop');
        }
        $args = array(
            'user_id' => $request['user_id'],
            'quantity' => $request['qty']
        );
    
        if ( function_exists( 'mycred' ) ) {
            $point_type = 'iPoints';
            $mycred     = mycred( $point_type );
        
            // Make sure user is not excluded
            if ( ! $mycred->exclude_user( $args['user_id'] ) ) {
        
                // get users balance
                $balance = $mycred->get_users_balance( $args['user_id'] );
        
                // Adjust balance with a log entry
                $mycred->add_creds(
                    'New iPoints reward from remote client',
                    $args['user_id'],
                    $args['quantity'],
                    '%plural% from remote client'
                );
    
                return "This is your current iPoints balance: {$balance}";    
            }else{
                return "This user with ID {$args['user_id']} can not receive iPoints";    
            }
        }
    }
    #39327
    Avatar
    terungwa

    I have figured this out. See solution below:

    add_action('rest_api_init', function () {
        register_rest_route( 'myplugin/v1', 'ipoints/(?P<user_id>\d+)/(?P<qty>\d+)',array(
                      'methods'  => 'GET',
                      'callback' => 'add_iPoints'
            ));
      });
    
    function getRealIpAddr()
      {
        if ( !empty( $_SERVER['HTTP_CLIENT_IP'] ) )
        {
          $ip = $_SERVER['HTTP_CLIENT_IP'];
        }
        elseif( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
        //to check ip passed from proxy
        {
          $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else
        {
          $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }
    
    function add_iPoints($request){
        if(getRealIpAddr() != '::1'){
            die('Stop');
        }
        $args = array(
            'user_id' => $request['user_id'],
            'quantity' => $request['qty']
        );
    
        if ( function_exists( 'mycred' ) ) {
            $point_type = 'iPoints';
            $mycred     = mycred( $point_type );
        
            // Make sure user is not excluded
            if ( ! $mycred->exclude_user( $args['user_id'] ) ) {
        
                // get users balance
                $balance = $mycred->get_users_balance( $args['user_id'] );
        
                // Adjust balance with a log entry
                $mycred->add_creds(
                    'New iPoints reward from remote client',
                    $args['user_id'],
                    $args['quantity'],
                    '%plural% from remote client'
                );
    
                return "This is your current iPoints balance: {$balance}";    
            }else{
                return "This user with ID {$args['user_id']} can not receive iPoints";    
            }
        }
    }
    #45254
    Avatar
    fcpro

    @ terungwa,
    Hi I’m very interested and curious to know if you have succeeded with your code to add and deduct points remotely outside wordpress system. Can you tell us/me more about it ?
    Thank you.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

11