Test Failed
Push — master ( 315839...9b266f )
by Devin
05:39
created

Give_Stripe_Logger::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Give - Stripe Core - Logger Class
4
 *
5
 * @package    Give
6
 * @subpackage Stripe Core
7
 * @copyright  Copyright (c) 2019, GiveWP
8
 * @license    https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since      2.5.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Class Give_Stripe_Logger
19
 *
20
 * @since 2.5.0
21
 */
22
class Give_Stripe_Logger {
23
24
	/**
25
	 * Give_Stripe_Logger constructor.
26
	 *
27
	 * @since 2.5.0
28
	 */
29
	public function __construct() {
30
	}
31
32
	/**
33
	 * Log a Stripe Error.
34
	 *
35
	 * Logs in the Give db the error and also displays the error message to the donor.
36
	 *
37
	 * @param \Stripe\Error\Base|\Stripe\Error\Card $exception    Stripe Exception Object.
38
	 * @param string                                $payment_mode Payment Mode.
39
	 *
40
	 * @since  2.5.0
41
	 * @access public
42
	 *
43
	 * @return bool
44
	 */
45
	public static function log_error( $exception, $payment_mode ) {
46
47
		$body       = $exception->getJsonBody();
48
		$error      = $body['error'];
49
		$error_code = ! empty( $error['code'] ) ? $error['code'] : '';
50
51
		// Update the error message based on custom error message using error code.
52
		$translated_error_message = self::get_card_error_message( $error_code );
53
		$error['message']           = ! empty( $translated_error_message ) ? $translated_error_message : $error['message'];
54
55
		$message = __( 'The payment gateway returned an error while processing the donation.', 'give' ) . '<br><br>';
56
57
		// Bad Request of some sort.
58
		if ( isset( $error['message'] ) ) {
59
			$message .= sprintf(
60
				/* translators: 1. Error Message */
61
				__( 'Message: %s', 'give' ),
62
				$error['message']
63
			);
64
			$message .= '<br><br>';
65
			$message .= sprintf(
66
				/* translators: 1. Error Code */
67
				__( 'Code: %s', 'give' ),
68
				$error_code
69
			);
70
71
			give_set_error( 'stripe_request_error', $error['message'] );
72
		} else {
73
			give_set_error( 'stripe_request_error', __( 'The Stripe API request was invalid, please try again.', 'give' ) );
74
		}
75
76
		// Log it with DB.
77
		give_record_gateway_error( __( 'Stripe Error', 'give' ), $message );
78
		give_send_back_to_checkout( '?payment-mode=' . $payment_mode );
79
80
		return false;
81
82
	}
83
84
	/**
85
	 * This function is used to fetch the custom card error messages.
86
	 *
87
	 * @since  2.5.0
88
	 * @access public
89
	 *
90
	 * @param string $error_code Error Code.
91
	 *
92
	 * @return string
93
	 */
94
	public static function get_card_error_message( $error_code ) {
95
96
		$message = '';
97
98
		switch ( $error_code ) {
99
			case 'incorrect_number':
100
				$message = __( 'The card number is incorrect.', 'give' );
101
				break;
102
			case 'invalid_number':
103
				$message = __( 'The card number is not a valid credit card number.', 'give' );
104
				break;
105
			case 'invalid_expiry_month':
106
				$message = __( 'The card\'s expiration month is invalid.', 'give' );
107
				break;
108
			case 'invalid_expiry_year':
109
				$message = __( 'The card\'s expiration year is invalid.', 'give' );
110
				break;
111
			case 'invalid_cvc':
112
				$message = __( 'The card\'s security code is invalid.', 'give' );
113
				break;
114
			case 'expired_card':
115
				$message = __( 'The card has expired.', 'give' );
116
				break;
117
			case 'incorrect_cvc':
118
				$message = __( 'The card\'s security code is incorrect.', 'give' );
119
				break;
120
			case 'incorrect_zip':
121
				$message = __( 'The card\'s zip code failed validation.', 'give' );
122
				break;
123
			case 'card_declined':
124
				$message = __( 'The card was declined.', 'give' );
125
				break;
126
			case 'missing':
127
				$message = __( 'There is no card on a customer that is being charged.', 'give' );
128
				break;
129
			case 'processing_error':
130
				$message = __( 'An error occurred while processing the card.', 'give' );
131
				break;
132
			case 'rate_limit':
133
				$message = __( 'An error occurred due to requests hitting the API too quickly. Please let us know if you\'re consistently running into this error.', 'give' );
134
				break;
135
		} // End switch().
136
137
		return $message;
138
	}
139
}
140
141
new Give_Stripe_Logger();
142