wp-pay-gateways /
icepay
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Pronamic\WordPress\Pay\Gateways\Icepay; |
||
| 4 | |||
| 5 | use Exception; |
||
| 6 | use Icepay_Api_Webservice; |
||
| 7 | use Icepay_Basicmode; |
||
| 8 | use Icepay_Paymentmethod_Creditcard; |
||
| 9 | use Icepay_Paymentmethod_Ddebit; |
||
| 10 | use Icepay_Paymentmethod_Ideal; |
||
| 11 | use Icepay_Paymentmethod_Mistercash; |
||
| 12 | use Icepay_PaymentObject; |
||
| 13 | use Icepay_Result; |
||
| 14 | use Icepay_StatusCode; |
||
| 15 | use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway; |
||
| 16 | use Pronamic\WordPress\Pay\Core\PaymentMethods; |
||
| 17 | use Pronamic\WordPress\Pay\Core\Statuses; |
||
| 18 | use Pronamic\WordPress\Pay\Payments\Payment; |
||
| 19 | use WP_Error; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Title: ICEPAY gateway |
||
| 23 | * Description: |
||
| 24 | * Copyright: 2005-2019 Pronamic |
||
| 25 | * Company: Pronamic |
||
| 26 | * |
||
| 27 | * @author Remco Tolsma |
||
| 28 | * @version 2.0.1 |
||
| 29 | * @since 1.0.0 |
||
| 30 | */ |
||
| 31 | class Gateway extends Core_Gateway { |
||
| 32 | /** |
||
| 33 | * Constructs and intializes an ICEPAY gateway |
||
| 34 | * |
||
| 35 | * @param Config $config Config. |
||
| 36 | */ |
||
| 37 | public function __construct( Config $config ) { |
||
| 38 | parent::__construct( $config ); |
||
| 39 | |||
| 40 | // Default properties for this gateway. |
||
| 41 | $this->set_method( self::METHOD_HTTP_REDIRECT ); |
||
| 42 | |||
| 43 | // Supported features. |
||
| 44 | $this->supports = array(); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Filter iDEAL |
||
| 49 | * |
||
| 50 | * @param array $method Payment method. |
||
| 51 | * |
||
| 52 | * @return bool |
||
| 53 | */ |
||
| 54 | private function filter_ideal( $method ) { |
||
| 55 | return is_array( $method ) && isset( $method['PaymentMethodCode'] ) && 'IDEAL' === $method['PaymentMethodCode']; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Get issuers |
||
| 60 | * |
||
| 61 | * @see Pronamic_WP_Pay_Gateway::get_issuers() |
||
| 62 | */ |
||
| 63 | public function get_issuers() { |
||
| 64 | $groups = array(); |
||
| 65 | $issuers = array(); |
||
| 66 | |||
| 67 | try { |
||
| 68 | $methods = Icepay_Api_Webservice::getInstance() |
||
| 69 | ->paymentmethodService() |
||
| 70 | ->setMerchantID( $this->config->merchant_id ) |
||
| 71 | ->setSecretCode( $this->config->secret_code ) |
||
| 72 | ->retrieveAllPaymentmethods() |
||
| 73 | ->asArray(); |
||
| 74 | } catch ( Exception $e ) { |
||
| 75 | return $groups; |
||
| 76 | } |
||
| 77 | |||
| 78 | $ideal_methods = array_filter( $methods, array( $this, 'filter_ideal' ) ); |
||
| 79 | |||
| 80 | if ( ! empty( $ideal_methods ) ) { |
||
| 81 | $issuers = Icepay_Api_Webservice::getInstance()->singleMethod() |
||
| 82 | ->loadFromArray( $methods ) |
||
| 83 | ->selectPaymentMethodByCode( 'IDEAL' ) |
||
| 84 | ->getIssuers(); |
||
| 85 | } |
||
| 86 | |||
| 87 | if ( $issuers ) { |
||
| 88 | $options = array(); |
||
| 89 | |||
| 90 | foreach ( $issuers as $issuer ) { |
||
| 91 | $options[ $issuer['IssuerKeyword'] ] = $issuer['Description']; |
||
| 92 | } |
||
| 93 | |||
| 94 | $groups[] = array( |
||
| 95 | 'options' => $options, |
||
| 96 | ); |
||
| 97 | } |
||
| 98 | |||
| 99 | return $groups; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get issuers |
||
| 104 | * |
||
| 105 | * @see Pronamic_WP_Pay_Gateway::get_issuers() |
||
| 106 | */ |
||
| 107 | public function get_credit_card_issuers() { |
||
| 108 | $groups = array(); |
||
| 109 | $issuers = array(); |
||
| 110 | |||
| 111 | $method = new Icepay_Paymentmethod_Creditcard(); |
||
| 112 | |||
| 113 | if ( isset( $method->_issuer ) ) { |
||
| 114 | $issuers = $method->_issuer; |
||
| 115 | } |
||
| 116 | |||
| 117 | if ( $issuers ) { |
||
| 118 | $options = array(); |
||
| 119 | |||
| 120 | foreach ( $issuers as $issuer ) { |
||
| 121 | switch ( $issuer ) { |
||
| 122 | case 'AMEX': |
||
| 123 | $name = _x( 'AMEX', 'Payment method name', 'pronamic_ideal' ); |
||
| 124 | |||
| 125 | break; |
||
| 126 | case 'MASTER': |
||
| 127 | $name = _x( 'MASTER', 'Payment method name', 'pronamic_ideal' ); |
||
| 128 | |||
| 129 | break; |
||
| 130 | case 'VISA': |
||
| 131 | $name = _x( 'VISA', 'Payment method name', 'pronamic_ideal' ); |
||
| 132 | |||
| 133 | break; |
||
| 134 | default: |
||
| 135 | $name = $issuer; |
||
| 136 | |||
| 137 | break; |
||
| 138 | } |
||
| 139 | |||
| 140 | $options[ $issuer ] = $name; |
||
| 141 | } |
||
| 142 | |||
| 143 | $groups[] = array( |
||
| 144 | 'options' => $options, |
||
| 145 | ); |
||
| 146 | } |
||
| 147 | |||
| 148 | return $groups; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get supported payment methods |
||
| 153 | * |
||
| 154 | * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods() |
||
| 155 | */ |
||
| 156 | public function get_supported_payment_methods() { |
||
| 157 | return array( |
||
| 158 | PaymentMethods::IDEAL, |
||
| 159 | PaymentMethods::CREDIT_CARD, |
||
| 160 | PaymentMethods::DIRECT_DEBIT, |
||
| 161 | PaymentMethods::BANCONTACT, |
||
| 162 | ); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Start an transaction |
||
| 167 | * |
||
| 168 | * @see Core_Gateway::start() |
||
| 169 | * |
||
| 170 | * @param Payment $payment Payment. |
||
| 171 | */ |
||
| 172 | public function start( Payment $payment ) { |
||
| 173 | try { |
||
| 174 | /* |
||
| 175 | * Order ID |
||
| 176 | * Your unique order number. |
||
| 177 | * This can be auto incremental number from your payments table |
||
| 178 | * |
||
| 179 | * Data type = String |
||
| 180 | * Max length = 10 |
||
| 181 | * Required = Yes |
||
| 182 | */ |
||
| 183 | |||
| 184 | // Locale, country and language. |
||
| 185 | $locale = get_locale(); |
||
| 186 | $language = substr( $locale, 0, 2 ); |
||
| 187 | |||
| 188 | if ( null !== $payment->get_customer() ) { |
||
| 189 | $locale = $payment->get_customer()->get_locale(); |
||
| 190 | |||
| 191 | $language = strtoupper( $payment->get_customer()->get_language() ); |
||
| 192 | } |
||
| 193 | |||
| 194 | $country = strtoupper( substr( $locale, 3, 2 ) ); |
||
| 195 | |||
| 196 | // Set country from billing address. |
||
| 197 | if ( null !== $payment->get_billing_address() ) { |
||
| 198 | $country_code = $payment->get_billing_address()->get_country_code(); |
||
| 199 | |||
| 200 | if ( ! empty( $country_code ) ) { |
||
| 201 | $country = $country_code; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | // Payment object. |
||
| 206 | $payment_object = new Icepay_PaymentObject(); |
||
| 207 | $payment_object |
||
| 208 | ->setAmount( $payment->get_total_amount()->get_cents() ) |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 209 | ->setCountry( $country ) |
||
| 210 | ->setLanguage( $language ) |
||
| 211 | ->setReference( $payment->get_order_id() ) |
||
| 212 | ->setDescription( $payment->get_description() ) |
||
| 213 | ->setCurrency( $payment->get_total_amount()->get_currency()->get_alphabetic_code() ) |
||
| 214 | ->setIssuer( $payment->get_issuer() ) |
||
| 215 | ->setOrderID( $payment->format_string( $this->config->order_id ) ); |
||
| 216 | |||
| 217 | /* |
||
| 218 | * Payment method |
||
| 219 | * @since 1.2.0 |
||
| 220 | */ |
||
| 221 | $icepay_method = null; |
||
| 222 | |||
| 223 | switch ( $payment->get_method() ) { |
||
| 224 | case PaymentMethods::CREDIT_CARD: |
||
| 225 | // @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/creditcard.php |
||
| 226 | $icepay_method = new Icepay_Paymentmethod_Creditcard(); |
||
| 227 | |||
| 228 | break; |
||
| 229 | case PaymentMethods::DIRECT_DEBIT: |
||
| 230 | // @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/ddebit.php |
||
| 231 | $icepay_method = new Icepay_Paymentmethod_Ddebit(); |
||
| 232 | |||
| 233 | break; |
||
| 234 | case PaymentMethods::IDEAL: |
||
| 235 | // @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/ideal.php |
||
| 236 | $icepay_method = new Icepay_Paymentmethod_Ideal(); |
||
| 237 | |||
| 238 | break; |
||
| 239 | case PaymentMethods::BANCONTACT: |
||
| 240 | case PaymentMethods::MISTER_CASH: |
||
| 241 | // @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/mistercash.php |
||
| 242 | $icepay_method = new Icepay_Paymentmethod_Mistercash(); |
||
| 243 | |||
| 244 | break; |
||
| 245 | } |
||
| 246 | |||
| 247 | if ( isset( $icepay_method ) ) { |
||
| 248 | // @link https://github.com/icepay/icepay/blob/2.4.0/api/icepay_api_base.php#L342-L353 |
||
| 249 | $payment_object->setPaymentMethod( $icepay_method->getCode() ); |
||
| 250 | |||
| 251 | // Force language 'NL' for unsupported languages (i.e. 'EN' for iDEAL). |
||
| 252 | if ( ! in_array( $language, $icepay_method->getSupportedLanguages(), true ) ) { |
||
| 253 | $payment_object->setLanguage( 'NL' ); |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | // Protocol. |
||
| 258 | $protocol = is_ssl() ? 'https' : 'http'; |
||
| 259 | |||
| 260 | // Basic mode. |
||
| 261 | $basicmode = Icepay_Basicmode::getInstance(); |
||
| 262 | $basicmode |
||
| 263 | ->setMerchantID( $this->config->merchant_id ) |
||
| 264 | ->setSecretCode( $this->config->secret_code ) |
||
| 265 | ->setProtocol( $protocol ) |
||
| 266 | ->setSuccessURL( $payment->get_return_url() ) |
||
| 267 | ->setErrorURL( $payment->get_return_url() ) |
||
| 268 | ->validatePayment( $payment_object ); |
||
| 269 | |||
| 270 | // Action URL. |
||
| 271 | $payment->set_action_url( $basicmode->getURL() ); |
||
| 272 | } catch ( Exception $exception ) { |
||
| 273 | $this->error = new WP_Error( 'icepay_error', $exception->getMessage(), $exception ); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Update the status of the specified payment |
||
| 279 | * |
||
| 280 | * @param Payment $payment Payment. |
||
| 281 | * |
||
| 282 | * @throws Exception |
||
| 283 | */ |
||
| 284 | public function update_status( Payment $payment ) { |
||
| 285 | // Get the Icepay Result and set the required fields. |
||
| 286 | $result = new Icepay_Result(); |
||
| 287 | $result |
||
| 288 | ->setMerchantID( $this->config->merchant_id ) |
||
| 289 | ->setSecretCode( $this->config->secret_code ); |
||
| 290 | |||
| 291 | try { |
||
| 292 | // Determine if the result can be validated. |
||
| 293 | if ( $result->validate() ) { |
||
| 294 | // What was the status response. |
||
| 295 | switch ( $result->getStatus() ) { |
||
| 296 | case Icepay_StatusCode::SUCCESS: |
||
| 297 | $payment->set_status( Statuses::SUCCESS ); |
||
| 298 | |||
| 299 | break; |
||
| 300 | case Icepay_StatusCode::OPEN: |
||
| 301 | $payment->set_status( Statuses::OPEN ); |
||
| 302 | |||
| 303 | break; |
||
| 304 | case Icepay_StatusCode::ERROR: |
||
| 305 | $payment->set_status( Statuses::FAILURE ); |
||
| 306 | |||
| 307 | break; |
||
| 308 | } |
||
| 309 | } |
||
| 310 | } catch ( Exception $exception ) { |
||
| 311 | $this->error = new WP_Error( 'icepay_error', $exception->getMessage(), $exception ); |
||
| 312 | } |
||
| 313 | } |
||
| 314 | } |
||
| 315 |