| @@ -25,8 +25,9 @@ | ||
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * @param string $stripe_id either a Payment Intent ID (prefixed with pi_) or a Setup Intent ID (prefixed with seti_). |
| 28 | 28 | * @param string $client_secret |
| 29 | + * | |
| 29 | 30 | * @return void |
| 30 | 31 | */ |
| 31 | 32 | public function __construct( $stripe_id, $client_secret ) { |
| 32 | 33 | $this->stripe_id = $stripe_id; |
| @@ -36,9 +37,10 @@ | ||
| 36 | 37 | /** |
| 37 | 38 | * Set the entry ID to pull referer data from. |
| 38 | 39 | * This is separate from the constructor as the entry ID isn't known for some error cases. |
| 39 | 40 | * |
| 40 | - * @param string|int $entry_id | |
| 41 | + * @param int|string $entry_id | |
| 42 | + * | |
| 41 | 43 | * @return void |
| 42 | 44 | */ |
| 43 | 45 | public function set_entry_id( $entry_id ) { |
| 44 | 46 | $this->entry_id = absint( $entry_id ); |
| @@ -45,12 +47,14 @@ | ||
| 45 | 47 | } |
| 46 | 48 | |
| 47 | 49 | /** |
| 48 | 50 | * @param string $error_code |
| 51 | + * @param string $charge_id | |
| 52 | + * | |
| 49 | 53 | * @return void |
| 50 | 54 | */ |
| 51 | - public function handle_error( $error_code ) { | |
| 52 | - if ( ! empty( $this->entry_id ) ) { | |
| 55 | + public function handle_error( $error_code, $charge_id = '' ) { | |
| 56 | + if ( $this->entry_id ) { | |
| 53 | 57 | $referer = FrmStrpLiteAuth::get_referer_url( $this->entry_id ); |
| 54 | 58 | } |
| 55 | 59 | |
| 56 | 60 | if ( empty( $referer ) ) { |
| @@ -56,10 +60,18 @@ | ||
| 56 | 60 | if ( empty( $referer ) ) { |
| 57 | 61 | $referer = FrmAppHelper::get_server_value( 'HTTP_REFERER' ); |
| 58 | 62 | } |
| 59 | 63 | |
| 64 | + $args = array( | |
| 65 | + 'frm_link_error' => $error_code, | |
| 66 | + ); | |
| 67 | + | |
| 68 | + if ( $charge_id ) { | |
| 69 | + $args['charge'] = $charge_id; | |
| 70 | + } | |
| 71 | + | |
| 60 | 72 | $this->add_intent_info_and_redirect( |
| 61 | - add_query_arg( array( 'frm_link_error' => $error_code ), $referer ) | |
| 73 | + add_query_arg( $args, $referer ) | |
| 62 | 74 | ); |
| 63 | 75 | } |
| 64 | 76 | |
| 65 | 77 | /** |
| @@ -66,8 +78,9 @@ | ||
| 66 | 78 | * Redirect to handle the form's on success condition similar to how 3D secure is handled after being redirected. |
| 67 | 79 | * |
| 68 | 80 | * @param stdClass $entry |
| 69 | 81 | * @param string $charge_id |
| 82 | + * | |
| 70 | 83 | * @return void |
| 71 | 84 | */ |
| 72 | 85 | public function handle_success( $entry, $charge_id ) { |
| 73 | 86 | $form = FrmForm::getOne( $entry->form_id ); |
| @@ -74,10 +87,13 @@ | ||
| 74 | 87 | |
| 75 | 88 | // Let a stripe link success message get handled the same as a 3D secure redirect. |
| 76 | 89 | // When it shows a message, it adds a &frmstrp= param to the URL. |
| 77 | 90 | $redirect = FrmStrpLiteAuth::return_url( compact( 'form', 'entry' ) ); |
| 78 | - $is_message_redirect = false !== strpos( $redirect, 'frmstrp=' ); | |
| 91 | + $is_message_redirect = str_contains( $redirect, 'frmstrp=' ); | |
| 79 | 92 | |
| 93 | + // Call this before all redirects so the referer is always deleted. | |
| 94 | + $referer_url = $this->get_referer_url( $entry->id ); | |
| 95 | + | |
| 80 | 96 | if ( $this->url_is_external( $redirect ) || ! $is_message_redirect ) { |
| 81 | 97 | wp_redirect( $redirect ); |
| 82 | 98 | die(); |
| 83 | 99 | } |
| @@ -82,14 +98,15 @@ | ||
| 82 | 98 | die(); |
| 83 | 99 | } |
| 84 | 100 | |
| 85 | 101 | // $redirect may not include the whole link to the form, breaking the redirect as iDEAL/Sofort have an additional redirect. |
| 86 | - $referer_url = $this->get_referer_url( $entry->id ); | |
| 87 | 102 | if ( is_string( $referer_url ) ) { |
| 88 | 103 | $parts = explode( '?', $redirect, 2 ); |
| 104 | + | |
| 89 | 105 | if ( 2 === count( $parts ) ) { |
| 90 | 106 | $redirect = $parts[1]; |
| 91 | 107 | } |
| 108 | + | |
| 92 | 109 | $redirect = $referer_url . '?' . $redirect; |
| 93 | 110 | } |
| 94 | 111 | |
| 95 | 112 | if ( $charge_id ) { |
| @@ -102,20 +119,24 @@ | ||
| 102 | 119 | /** |
| 103 | 120 | * Determine if a redirect URL is going to an external site or not. |
| 104 | 121 | * |
| 105 | 122 | * @param string $url |
| 123 | + * | |
| 124 | + * @return bool | |
| 106 | 125 | */ |
| 107 | 126 | private function url_is_external( $url ) { |
| 108 | - if ( false === strpos( $url, 'http' ) ) { | |
| 127 | + if ( ! str_contains( $url, 'http' ) ) { | |
| 109 | 128 | return false; |
| 110 | 129 | } |
| 111 | 130 | |
| 112 | - $home_url = home_url(); | |
| 113 | - $parsed = parse_url( $home_url ); | |
| 131 | + $home_url = home_url(); | |
| 132 | + $parsed = parse_url( $home_url ); | |
| 133 | + | |
| 114 | 134 | if ( is_array( $parsed ) ) { |
| 115 | 135 | $home_url = $parsed['scheme'] . '://' . $parsed['host']; |
| 116 | 136 | } |
| 117 | - return 0 !== strpos( $url, $home_url ); | |
| 137 | + | |
| 138 | + return ! str_starts_with( $url, $home_url ); | |
| 118 | 139 | } |
| 119 | 140 | |
| 120 | 141 | /** |
| 121 | 142 | * Try to get the referer URL from the entry meta. |
| @@ -120,10 +141,11 @@ | ||
| 120 | 141 | /** |
| 121 | 142 | * Try to get the referer URL from the entry meta. |
| 122 | 143 | * If it is found, it will also be deleted as it is only required once. |
| 123 | 144 | * |
| 124 | - * @param string|int $entry_id | |
| 125 | - * @return string|false | |
| 145 | + * @param int|string $entry_id | |
| 146 | + * | |
| 147 | + * @return false|string | |
| 126 | 148 | */ |
| 127 | 149 | private function get_referer_url( $entry_id ) { |
| 128 | 150 | $row = FrmDb::get_row( |
| 129 | 151 | 'frm_item_metas', |
| @@ -133,8 +155,9 @@ | ||
| 133 | 155 | 'meta_value LIKE' => '{"referer":', |
| 134 | 156 | ), |
| 135 | 157 | 'id, meta_value' |
| 136 | 158 | ); |
| 159 | + | |
| 137 | 160 | if ( ! $row ) { |
| 138 | 161 | return false; |
| 139 | 162 | } |
| 140 | 163 | |
| @@ -152,8 +175,9 @@ | ||
| 152 | 175 | /** |
| 153 | 176 | * Delete the referer meta as we'll no longer need it. |
| 154 | 177 | * |
| 155 | 178 | * @param int $row_id |
| 179 | + * | |
| 156 | 180 | * @return void |
| 157 | 181 | */ |
| 158 | 182 | private static function delete_temporary_referer_meta( $row_id ) { |
| 159 | 183 | global $wpdb; |
| @@ -165,9 +189,9 @@ | ||
| 165 | 189 | * |
| 166 | 190 | * @param string $url |
| 167 | 191 | */ |
| 168 | 192 | private function add_intent_info_and_redirect( $url ) { |
| 169 | - if ( 0 === strpos( $this->stripe_id, 'pi_' ) ) { | |
| 193 | + if ( str_starts_with( $this->stripe_id, 'pi_' ) ) { | |
| 170 | 194 | $url = add_query_arg( 'payment_intent', $this->stripe_id, $url ); |
| 171 | 195 | $url = add_query_arg( 'payment_intent_client_secret', $this->client_secret, $url ); |
| 172 | 196 | } else { |
| 173 | 197 | $url = add_query_arg( 'setup_intent', $this->stripe_id, $url ); |