 wp-pay-gateways    /
                    omnikassa-2
                      wp-pay-gateways    /
                    omnikassa-2
                
                            | 1 | <?php | ||||
| 2 | /** | ||||
| 3 | * Gateway | ||||
| 4 | * | ||||
| 5 | * @author Pronamic <[email protected]> | ||||
| 6 | * @copyright 2005-2018 Pronamic | ||||
| 7 | * @license GPL-3.0-or-later | ||||
| 8 | * @package Pronamic\WordPress\Pay\Gateways\OmniKassa2 | ||||
| 9 | */ | ||||
| 10 | |||||
| 11 | namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2; | ||||
| 12 | |||||
| 13 | use Pronamic\WordPress\Pay\Core\Util as Core_Util; | ||||
| 14 | use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway; | ||||
| 15 | use Pronamic\WordPress\Pay\Core\PaymentMethods; | ||||
| 16 | use Pronamic\WordPress\Pay\Payments\Payment; | ||||
| 17 | |||||
| 18 | /** | ||||
| 19 | * Gateway | ||||
| 20 | * | ||||
| 21 | * @author Remco Tolsma | ||||
| 22 | * @version 2.0.2 | ||||
| 23 | * @since 1.0.0 | ||||
| 24 | */ | ||||
| 25 | class Gateway extends Core_Gateway { | ||||
| 26 | /** | ||||
| 27 | * Constructs and initializes an OmniKassa 2.0 gateway. | ||||
| 28 | * | ||||
| 29 | * @param Config $config Config. | ||||
| 30 | */ | ||||
| 31 | 	public function __construct( Config $config ) { | ||||
| 32 | parent::__construct( $config ); | ||||
| 33 | |||||
| 34 | $this->set_method( self::METHOD_HTTP_REDIRECT ); | ||||
| 35 | |||||
| 36 | // Client. | ||||
| 37 | $this->client = new Client(); | ||||
| 0 ignored issues–
                            show             Bug
            Best Practice
    
    
    
        introduced 
                            by  
  Loading history... | |||||
| 38 | |||||
| 39 | $url = Client::URL_PRODUCTION; | ||||
| 40 | |||||
| 41 | 		if ( self::MODE_TEST === $config->mode ) { | ||||
| 42 | $url = Client::URL_SANDBOX; | ||||
| 43 | } | ||||
| 44 | |||||
| 45 | $this->client->set_url( $url ); | ||||
| 46 | $this->client->set_refresh_token( $config->refresh_token ); | ||||
| 47 | $this->client->set_signing_key( $config->signing_key ); | ||||
| 48 | } | ||||
| 49 | |||||
| 50 | /** | ||||
| 51 | * Get supported payment methods. | ||||
| 52 | * | ||||
| 53 | * @see \Pronamic_WP_Pay_Gateway::get_supported_payment_methods() | ||||
| 54 | * @return array | ||||
| 55 | */ | ||||
| 56 | 	public function get_supported_payment_methods() { | ||||
| 57 | return array( | ||||
| 58 | PaymentMethods::BANCONTACT, | ||||
| 59 | PaymentMethods::CREDIT_CARD, | ||||
| 60 | PaymentMethods::IDEAL, | ||||
| 61 | PaymentMethods::PAYPAL, | ||||
| 62 | ); | ||||
| 63 | } | ||||
| 64 | |||||
| 65 | /** | ||||
| 66 | * Start. | ||||
| 67 | * | ||||
| 68 | * @see Core_Gateway::start() | ||||
| 69 | * | ||||
| 70 | * @param Payment $payment Payment. | ||||
| 71 | */ | ||||
| 72 | 	public function start( Payment $payment ) { | ||||
| 73 | $merchant_order_id = $payment->format_string( $this->config->order_id ); | ||||
| 74 | |||||
| 75 | $payment->set_meta( 'omnikassa_2_merchant_order_id', $merchant_order_id ); | ||||
| 76 | |||||
| 77 | $amount = new Money( | ||||
| 78 | $payment->get_currency(), | ||||
| 0 ignored issues–
                            show $payment->get_currency()of typePronamic\WordPress\Money\Currencyis incompatible with the typestringexpected by parameter$currencyofPronamic\WordPress\Pay\G...a2\Money::__construct().
                                                                                                                                                                                           (
                                     Ignorable by Annotation
                                ) If this is a false-positive, you can also ignore this issue in your code via the  
  Loading history... | |||||
| 79 | Core_Util::amount_to_cents( $payment->get_amount()->get_amount() ) | ||||
| 80 | ); | ||||
| 81 | |||||
| 82 | $merchant_return_url = str_replace( '.test', '.nl', $payment->get_return_url() ); | ||||
| 83 | |||||
| 84 | $order = new Order( $merchant_order_id, $amount, $merchant_return_url ); | ||||
| 85 | |||||
| 86 | $order->set_description( $payment->get_description() ); | ||||
| 87 | $order->set_language( $payment->get_language() ); | ||||
| 88 | |||||
| 89 | // Payment brand. | ||||
| 90 | $payment_brand = PaymentBrands::transform( $payment->get_method() ); | ||||
| 91 | |||||
| 92 | $order->set_payment_brand( $payment_brand ); | ||||
| 93 | |||||
| 94 | 		if ( null !== $payment_brand ) { | ||||
| 95 | // Payment brand force should only be set if payment brand is not empty. | ||||
| 96 | $order->set_payment_brand_force( PaymentBrandForce::FORCE_ONCE ); | ||||
| 97 | } | ||||
| 98 | |||||
| 99 | 		if ( ! $this->config->is_access_token_valid() ) { | ||||
| 100 | $data = $this->client->get_access_token_data(); | ||||
| 101 | |||||
| 102 | $error = $this->client->get_error(); | ||||
| 103 | |||||
| 104 | 			if ( is_wp_error( $error ) ) { | ||||
| 105 | $this->error = $error; | ||||
| 106 | |||||
| 107 | return; | ||||
| 108 | } | ||||
| 109 | |||||
| 110 | $this->config->access_token = $data->token; | ||||
| 111 | $this->config->access_token_valid_until = $data->validUntil; | ||||
| 112 | |||||
| 113 | update_post_meta( $this->config->post_id, '_pronamic_gateway_omnikassa_2_access_token', $data->token ); | ||||
| 0 ignored issues–
                            show | |||||
| 114 | update_post_meta( $this->config->post_id, '_pronamic_gateway_omnikassa_2_access_token_valid_until', $data->validUntil ); | ||||
| 115 | } | ||||
| 116 | |||||
| 117 | $result = $this->client->order_announce( $this->config, $order ); | ||||
| 118 | |||||
| 119 | $error = $this->client->get_error(); | ||||
| 120 | |||||
| 121 | 		if ( is_wp_error( $error ) ) { | ||||
| 122 | $this->error = $error; | ||||
| 123 | |||||
| 124 | return; | ||||
| 125 | } | ||||
| 126 | |||||
| 127 | 		if ( $result ) { | ||||
| 128 | $payment->set_action_url( $result->redirectUrl ); | ||||
| 129 | } | ||||
| 130 | } | ||||
| 131 | |||||
| 132 | /** | ||||
| 133 | * Update status of the specified payment. | ||||
| 134 | * | ||||
| 135 | * @param Payment $payment Payment. | ||||
| 136 | */ | ||||
| 137 | 	public function update_status( Payment $payment ) { | ||||
| 138 | 		if ( ! ReturnParameters::contains( $_GET ) ) { // WPCS: CSRF ok. | ||||
| 139 | return; | ||||
| 140 | } | ||||
| 141 | |||||
| 142 | $parameters = ReturnParameters::from_array( $_GET ); // WPCS: CSRF ok. | ||||
| 143 | |||||
| 144 | // Note. | ||||
| 145 | $note_values = array( | ||||
| 146 | 'order_id' => $parameters->get_order_id(), | ||||
| 147 | 'status' => $parameters->get_status(), | ||||
| 148 | 'signature' => $parameters->get_signature(), | ||||
| 149 | 'valid' => $parameters->is_valid( $this->config->signing_key ) ? 'true' : 'false', | ||||
| 150 | ); | ||||
| 151 | |||||
| 152 | $note = ''; | ||||
| 153 | |||||
| 154 | $note .= '<p>'; | ||||
| 155 | $note .= __( 'OmniKassa 2.0 return URL requested:', 'pronamic_ideal' ); | ||||
| 156 | $note .= '</p>'; | ||||
| 157 | |||||
| 158 | $note .= '<dl>'; | ||||
| 159 | |||||
| 160 | 		foreach ( $note_values as $key => $value ) { | ||||
| 161 | $note .= sprintf( '<dt>%s</dt>', esc_html( $key ) ); | ||||
| 162 | $note .= sprintf( '<dd>%s</dd>', esc_html( $value ) ); | ||||
| 163 | } | ||||
| 164 | |||||
| 165 | $note .= '</dl>'; | ||||
| 166 | |||||
| 167 | $payment->add_note( $note ); | ||||
| 168 | |||||
| 169 | // Validate. | ||||
| 170 | 		if ( ! $parameters->is_valid( $this->config->signing_key ) ) { | ||||
| 171 | return; | ||||
| 172 | } | ||||
| 173 | |||||
| 174 | // Status. | ||||
| 175 | $payment->set_status( Statuses::transform( $parameters->get_status() ) ); | ||||
| 176 | } | ||||
| 177 | |||||
| 178 | /** | ||||
| 179 | * Handle notification. | ||||
| 180 | * | ||||
| 181 | * @param Notification $notification Notification. | ||||
| 182 | * | ||||
| 183 | * @return void | ||||
| 184 | */ | ||||
| 185 | 	public function handle_notification( Notification $notification ) { | ||||
| 186 | 		if ( ! $notification->is_valid( $this->config->signing_key ) ) { | ||||
| 187 | return; | ||||
| 188 | } | ||||
| 189 | |||||
| 190 | 		switch ( $notification->get_event_name() ) { | ||||
| 191 | case 'merchant.order.status.changed': | ||||
| 192 | $this->handle_merchant_order_status_changed( $notification ); | ||||
| 193 | } | ||||
| 194 | } | ||||
| 195 | |||||
| 196 | /** | ||||
| 197 | * Handle `merchant.order.status.changed` event. | ||||
| 198 | * | ||||
| 199 | * @param Notification $notification Notification. | ||||
| 200 | * | ||||
| 201 | * @return void | ||||
| 202 | */ | ||||
| 203 | 	private function handle_merchant_order_status_changed( Notification $notification ) { | ||||
| 204 | 		do { | ||||
| 205 | $order_results = $this->client->get_order_results( $notification->get_authentication() ); | ||||
| 206 | |||||
| 207 | 			if ( ! $order_results || $order_results->is_valid( $this->config->signing_key ) ) { | ||||
| 208 | return; | ||||
| 209 | } | ||||
| 210 | |||||
| 211 | 			foreach ( $order_results as $order_result ) { | ||||
| 212 | $payment = get_pronamic_payment_by_meta( '_pronamic_payment_omnikassa_2_merchant_order_id', $order_result->get_merchant_order_id() ); | ||||
| 213 | |||||
| 214 | 				if ( empty( $payment ) ) { | ||||
| 215 | continue; | ||||
| 216 | } | ||||
| 217 | |||||
| 218 | $payment->set_transaction_id( $order_result->get_omnikassa_order_id() ); | ||||
| 219 | $payment->set_status( Statuses::transform( $order_result->get_order_status() ) ); | ||||
| 220 | |||||
| 221 | // Note. | ||||
| 222 | $note = ''; | ||||
| 223 | |||||
| 224 | $note .= '<p>'; | ||||
| 225 | $note .= __( 'OmniKassa 2.0 webhook URL requested:', 'pronamic_ideal' ); | ||||
| 226 | $note .= '</p>'; | ||||
| 227 | $note .= '<pre>'; | ||||
| 228 | $note .= wp_json_encode( $order_result->get_json(), JSON_PRETTY_PRINT ); | ||||
| 229 | $note .= '</pre>'; | ||||
| 230 | |||||
| 231 | $payment->add_note( $note ); | ||||
| 232 | |||||
| 233 | $payment->save(); | ||||
| 234 | } | ||||
| 235 | } while ( $order_results->more_available() ); | ||||
| 236 | } | ||||
| 237 | } | ||||
| 238 | 
