|
|
get_description_html( $data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
?>
|
payment_icons( $this->id );
}
/**
* Check current section is cpsw section.
*
* @since 1.3.0
*
* @return boolean
*/
public function is_current_section() {
return Notice::is_cpsw_section( $this->id );
}
/**
* Add notices
*
* @since 1.2.0
*
* @return void
*/
public function add_notice() {
if ( 'yes' !== $this->enabled ) {
return;
}
if ( ! $this->is_current_section() ) {
return;
}
// Add notice if currency not supported.
if (
method_exists( $this, 'get_supported_currency' ) &&
! in_array( get_woocommerce_currency(), $this->get_supported_currency(), true ) &&
'no' !== get_option( 'cpsw_show_' . $this->id . '_currency_notice' )
) {
$method = ( 'cpsw_ideal' === $this->id ) ? 'iDEAL' : ucfirst( str_replace( 'cpsw_', '', $this->id ) );
/* translators: %1$s Payment method, %2$s List of supported currencies */
Notice::add( $this->id . '_currency', 'notice notice-error', sprintf( __( '%1$s is enabled - it requires store currency to be set to %2$s.', 'checkout-plugins-stripe-woo' ), $method, implode( ', ', $this->get_supported_currency() ) ), true );
}
// Add notice if currency not supported.
$default_currency = $this->get_stripe_default_currency();
if (
! empty( $default_currency ) &&
! in_array( strtolower( get_woocommerce_currency() ), $default_currency, true ) &&
'no' !== get_option( 'cpsw_show_' . $this->id . '_stripe_currency_notice' )
) {
/* translators: %1$s Payment method, %2$s List of supported currencies */
Notice::add( $this->id . '_stripe_currency', 'notice notice-error', sprintf( __( '%1$s is enabled - your store currency %2$s does not match with your stripe account supported currency %3$s.', 'checkout-plugins-stripe-woo' ), ucfirst( str_replace( 'cpsw_', '', $this->id ) ), get_woocommerce_currency(), strtoupper( implode( ', ', $default_currency ) ) ), true );
}
/**
* Action for add more notices for local gateways.
*
* @since 1.3.0
*
* @param obj $notice Notice object.
* @param obj $this Current full class object.
*/
do_action( 'cpsw_add_notices_for_local_gateways', Notice::get_instance(), $this );
}
/**
* Return a description for (for admin sections) describing the required currency & or billing country(s).
*
* @since 1.2.0
*
* @return string
*/
public function payment_description() {
$desc = '';
if ( method_exists( $this, 'get_supported_currency' ) && $this->get_supported_currency() ) {
// translators: %s: supported currency.
$desc .= sprintf( __( 'This gateway supports the following currencies only : ' . $e->getMessage() . '
', [ 'status' => 200 ] );
}
}
/**
* Modify redirect url
*
* @since 1.3.0
*
* @param array $result redirect url array.
* @param int $order_id woocommerce order id.
*
* @return array modified redirect url array.
*/
public function modify_successful_payment_result( $result, $order_id ) {
if ( empty( $order_id ) ) {
return $result;
}
$order = wc_get_order( $order_id );
if ( $this->id !== $order->get_payment_method() ) {
return $result;
}
if ( ! isset( $result['intent_secret'] ) ) {
return $result;
}
// Put the final thank you page redirect into the verification URL.
$verification_url = add_query_arg(
[
'order' => $order_id,
'confirm_payment_nonce' => wp_create_nonce( 'cpsw_confirm_payment_intent' ),
'redirect_to' => rawurlencode( $result['redirect'] ),
'order_key' => $order->get_order_key(),
],
WC_AJAX::get_endpoint( $this->id . '_verify_payment_intent' )
);
// Combine into a hash.
$redirect = sprintf( '#confirm-pi-%s:%s:%s', $result['intent_secret'], rawurlencode( $verification_url ), $this->id );
/**
* Action modify mayment successful result.
*
* @since 1.3.0
*
* @param obj $result Payment successful result.
* @param obj $order WooCommerce main order.
*/
do_action( 'cpsw_local_gateways_modify_successful_payment_result', $result, $order );
return [
'result' => 'success',
'redirect' => $redirect,
];
}
/**
* Verify intent state and redirect.
*
* @since 1.3.0
*
* @return void
*/
public function verify_intent() {
$checkout_url = wc_get_checkout_url();
$order_id = isset( $_GET['order'] ) ? sanitize_text_field( $_GET['order'] ) : 0; //phpcs:ignore WordPress.Security.NonceVerification.Recommended
// Check for empty order id.
if ( empty( $order_id ) ) {
wc_add_notice( __( 'No orders are found for provided order ID.', 'checkout-plugins-stripe-woo' ), 'error' );
Logger::error( __( 'Invalid order Id.', 'checkout-plugins-stripe-woo' ) );
wp_safe_redirect( $checkout_url );
exit();
}
$order = wc_get_order( $order_id );
// Check for empty order object.
if ( empty( $order ) ) {
wc_add_notice( __( 'No valid order found.', 'checkout-plugins-stripe-woo' ), 'error' );
Logger::error( __( 'Invalid order. No order found for provided order ID.', 'checkout-plugins-stripe-woo' ) );
wp_safe_redirect( $checkout_url );
exit();
}
if ( ! isset( $_GET['order_key'] ) || ! $order->key_is_valid( wc_clean( $_GET['order_key'] ) ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
wc_add_notice( __( 'Invalid order key received.', 'checkout-plugins-stripe-woo' ), 'error' );
Logger::error( __( 'Invalid order key.', 'checkout-plugins-stripe-woo' ) );
wp_safe_redirect( $checkout_url );
exit();
}
$intent_secret = $order->get_meta( '_cpsw_intent_secret' );
$stripe_api = new Stripe_Api();
$response = $stripe_api->payment_intents( 'retrieve', [ $intent_secret['id'] ] );
$intent = $response['success'] ? $response['data'] : false;
/**
* Action when verify intent.
*
* @since 1.3.0
*
* @param obj $intent Payment intent data.
* @param obj $order WooCommerce main order.
*/
do_action( 'cpsw_local_gateways_process_order', $intent, $order );
if ( 'succeeded' === $intent->status || 'requires_capture' === $intent->status ) {
$redirect_to = $this->process_order( end( $intent->charges->data ), $order_id );
$redirect_url = apply_filters( 'cpsw_redirect_order_url', $redirect_to, $order );
} elseif ( isset( $response['data']->last_payment_error ) ) {
$message = isset( $response['data']->last_payment_error->message ) ? $response['data']->last_payment_error->message : '';
$code = isset( $response['data']->last_payment_error->code ) ? $response['data']->last_payment_error->code : '';
$order->update_status( 'wc-failed' );
// translators: %s: payment fail message.
wc_add_notice( sprintf( __( 'Payment failed. %s', 'checkout-plugins-stripe-woo' ), Helper::get_localized_messages( $code, $message ) ), 'error' );
/* translators: %1$1s order id, %2$2s payment fail message. */
Logger::error( sprintf( __( 'Payment failed for Order id - %1$1s. %2$2s', 'checkout-plugins-stripe-woo' ), $order_id, Helper::get_localized_messages( $code, $message ) ) );
$redirect_url = $checkout_url;
}
wp_safe_redirect( $redirect_url );
exit();
}
/**
* Get verification url for payment intent
*
* @param int $order_id woocommerce order id.
* @param string $redirect_url redirect url.
* @param boolean $save_card save card.
* @since 1.7.0
* @return string verification url.
*/
public function get_verification_url( $order_id, $redirect_url, $save_card = false ) {
$order = wc_get_order( $order_id );
// Put the final thank you page redirect into the verification URL.
return add_query_arg(
[
'order' => $order_id,
'confirm_payment_nonce' => wp_create_nonce( 'cpsw_confirm_payment_intent' ),
'redirect_to' => rawurlencode( $redirect_url ),
'save_card' => $save_card,
'order_key' => $order->get_order_key(),
],
WC_AJAX::get_endpoint( $this->id . '_verify_payment_intent' )
);
}
/**
* Normalizes the SEPA IBAN label.
*
* @param string $label label.
* @since 1.8.0
* @return string $label
*/
public function normalize_sepa_label( $label ) {
if ( 'sepa iban' === strtolower( $label ) ) {
return 'SEPA IBAN';
}
return $label;
}
}