| 1 |  |  | ## | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | # Copyright (c) 2012-2016 WePay. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | # http://opensource.org/licenses/Apache2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | ## | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | require 'cgi' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | require 'json' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | require 'net/http' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | require 'net/https' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | module WePay | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |   ## | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |   # A very simple wrapper for the WePay API. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |   ## | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |   class Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     # Stage API endpoint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     STAGE_API_ENDPOINT = "https://stage.wepayapi.com/v2" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     # Stage UI endpoint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     STAGE_UI_ENDPOINT = "https://stage.wepay.com/v2" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     # Production API endpoint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     PRODUCTION_API_ENDPOINT = "https://wepayapi.com/v2" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     # Production UI endpoint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     PRODUCTION_UI_ENDPOINT = "https://www.wepay.com/v2" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     attr_reader :client_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 :client_secret, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 :use_stage, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 :api_version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     def initialize(client_id, client_secret, use_stage = true, api_version = nil) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |       @client_id     = client_id.to_s | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |       @client_secret = client_secret.to_s | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |       @use_stage     = !!use_stage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |       @api_version   = api_version.to_s | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     ## | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     # Execute a call to the WePay API. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     ## | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     def call(path, access_token = false, params = {}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |       request_class.new( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         client_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         client_secret, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         api_version, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         path, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         access_token, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |       ).response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     ## | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     # Returns the OAuth 2.0 URL that users should be redirected to for | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     # authorizing your API application. The `redirect_uri` must be a | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     # fully-qualified URL (e.g., `https://www.wepay.com`). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     ## | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     def oauth2_authorize_url( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |       redirect_uri, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |       user_email   = false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |       user_name    = false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |       permissions  = default_oauth_permissions, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |       user_country = false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |       url = ui_endpoint + | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             '/oauth2/authorize?client_id=' + @client_id.to_s + | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             '&redirect_uri=' + redirect_uri + | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             '&scope=' + permissions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |       url += user_name ?    '&user_name='    + CGI::escape(user_name)    : '' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |       url += user_email ?   '&user_email='   + CGI::escape(user_email)   : '' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |       url += user_country ? '&user_country=' + CGI::escape(user_country) : '' | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     ## | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     # Call the `/v2/oauth2/token` endpoint to exchange an OAuth 2.0 `code` for an `access_token`. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     ## | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     def oauth2_token(code, redirect_uri) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |       call('/oauth2/token', false, { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         'client_id'     => @client_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         'client_secret' => @client_secret, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         'redirect_uri'  => redirect_uri, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         'code'          => code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |       }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     ## | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     # Support exisiting API | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     # `WePay::Client#api_endpoint` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     ## | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     def api_endpoint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |       if use_stage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         STAGE_API_ENDPOINT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |       else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         PRODUCTION_API_ENDPOINT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |       end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     ## | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     # Support exisiting API | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     # | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  |     # `WePay::Client#ui_endpoint` | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |     ## | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     def ui_endpoint | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |       if use_stage | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         STAGE_UI_ENDPOINT | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |       else | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |         PRODUCTION_UI_ENDPOINT | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |       end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     private | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     def request_class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |       if use_stage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         TestRequest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |       else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         ProductionRequest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |       end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     def default_oauth_permissions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |       %w( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         manage_accounts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         collect_payments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         view_user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         send_money | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         preapprove_payments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         manage_subscriptions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |       ).join(',') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |   end | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 138 |  |  | end | 
            
                                                        
            
                                    
            
            
                | 139 |  |  |  |