| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Pronamic\WordPress\Pay\Gateways\Nocks; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Pronamic\WordPress\Pay\Gateways\Common\AbstractIntegration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Pronamic\WordPress\Pay\Util; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Title: Nocks integration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Description: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Copyright: 2005-2020 Pronamic | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Company: Pronamic | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @author  Reüel van der Steege | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @version 2.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @since   1.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class Integration extends AbstractIntegration { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	public function __construct() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 		$this->id            = 'nocks'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		$this->name          = 'Nocks - Checkout'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		$this->product_url   = 'https://www.nocks.com/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		$this->dashboard_url = 'https://www.nocks.com/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		$this->provider      = 'nocks'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		$this->supports      = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 			'payment_status_request', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 			'webhook', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 			'webhook_log', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 			'webhook_no_config', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		// Actions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		$function = array( __NAMESPACE__ . '\Listener', 'listen' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		if ( ! has_action( 'wp_loaded', $function ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 			add_action( 'wp_loaded', $function ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	 * Get settings fields. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	public function get_settings_fields() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		$fields = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		// Access token. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		$fields[] = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 			'section'  => 'general', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 			'filter'   => FILTER_SANITIZE_STRING, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 			'meta_key' => '_pronamic_gateway_nocks_access_token', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 			'title'    => _x( 'Access Token', 'nocks', 'pronamic_ideal' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 			'type'     => 'textarea', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			'classes'  => array( 'code' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		// Merchant profile. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		$fields[] = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 			'section'  => 'general', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			'filter'   => FILTER_SANITIZE_STRING, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			'meta_key' => '_pronamic_gateway_nocks_merchant_profile', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			'title'    => _x( 'Merchant Profile', 'nocks', 'pronamic_ideal' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			'type'     => 'description', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 			'callback' => array( $this, 'field_merchant_profile' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		// Webhook URL. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		$fields[] = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 			'section'  => 'feedback', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 			'title'    => __( 'Webhook URL', 'pronamic_ideal' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 			'type'     => 'text', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 			'classes'  => array( 'large-text', 'code' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			'value'    => add_query_arg( 'nocks_webhook', '', home_url( '/' ) ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 			'readonly' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 			'tooltip'  => __( 'The Webhook URL as sent with each transaction to receive automatic payment status updates on.', 'pronamic_ideal' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		return $fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	 * Field merchant profile select. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 * @param array $field Settings field. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	public function field_merchant_profile( $field ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		$access_token     = get_post_meta( get_the_ID(), '_pronamic_gateway_nocks_access_token', true ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		$merchant_profile = get_post_meta( get_the_ID(), '_pronamic_gateway_nocks_merchant_profile', true ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		if ( ! $access_token ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			esc_html_e( 'First enter an API Key and save the configuration, to be able to choose from your Nocks merchant profiles.', 'pronamic_ideal' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 			return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		$client = new Client(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		$client->set_access_token( $access_token ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		// Select merchant profile. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		printf( '<select name="%s">', esc_attr( $field['meta_key'] ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		$options = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 			__( '— Select Merchant Profile —', 'pronamic_ideal' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 			$options = array_merge( $options, $client->get_merchant_profiles() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		} catch ( \Exception $e ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 			// What to do? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		$options = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 				'options' => $options, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 			), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		echo Util::select_options_grouped( $options, $merchant_profile ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		echo '</select>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  | 	public function get_config( $post_id ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  | 		$config = new Config(); | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  | 		$config->mode             = $this->get_meta( $post_id, '_pronamic_gateway_mode' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  | 		$config->access_token     = $this->get_meta( $post_id, '_pronamic_gateway_nocks_access_token' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  | 		$config->merchant_profile = $this->get_meta( $post_id, '_pronamic_gateway_nocks_merchant_profile' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  | 		return $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	 * Get gateway. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	 * @param int $post_id Post ID. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	 * @return Gateway | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	public function get_gateway( $post_id ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 		return new Gateway( $this->get_config( $post_id ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 147 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 148 |  |  |  |