Passed
Push — master ( 6e7987...32865b )
by Mehul
08:32 queued 05:32
created

PaiementPro4Give_Card   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 67
c 1
b 0
f 1
dl 0
loc 128
rs 10
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
C process_donation() 0 103 10
1
<?php
2
/**
3
 * PaiementPro for Give | Credit Card
4
 *
5
 * @since 1.0.2
6
 */
7
8
// Bailout, if accessed directly.
9
if ( ! defined( 'ABSPATH' ) ) {
10
	exit;
11
}
12
13
class PaiementPro4Give_Card {
14
15
	/**
16
	 * Constructor
17
	 *
18
	 * @since  1.0.2
19
	 * @access public
20
	 *
21
	 * @return void
22
	 */
23
	public function __construct() {
24
		add_action( 'give_gateway_paiementpro_card', [ $this, 'process_donation' ] );
0 ignored issues
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
		/** @scrutinizer ignore-call */ 
25
  add_action( 'give_gateway_paiementpro_card', [ $this, 'process_donation' ] );
Loading history...
25
		add_action( 'give_paiementpro_card_cc_form', '__return_false' );
26
	}
27
28
	/**
29
	 * Process Donation
30
	 *
31
	 * @param array $data List of posted data.
32
	 *
33
	 * @since  1.0.2
34
	 * @access public
35
	 *
36
	 * @return void
37
	 */
38
	public function process_donation( $data ) {
39
		// Check for any stored errors.
40
		$errors = give_get_errors();
0 ignored issues
show
Bug introduced by
The function give_get_errors was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
		$errors = /** @scrutinizer ignore-call */ give_get_errors();
Loading history...
41
42
		if ( ! $errors ) {
43
44
			$form_id      = ! empty( $data['post_data']['give-form-id'] ) ? intval( $data['post_data']['give-form-id'] ) : false;
45
			$first_name   = ! empty( $data['post_data']['give_first'] ) ? $data['post_data']['give_first'] : '';
46
			$last_name    = ! empty( $data['post_data']['give_last'] ) ? $data['post_data']['give_last'] : '';
47
			$email        = ! empty( $data['post_data']['give_email'] ) ? $data['post_data']['give_email'] : '';
48
			$donation_key = ! empty( $data['purchase_key'] ) ? $data['purchase_key'] : '';
49
			$currency     = give_get_currency( $form_id );
0 ignored issues
show
Bug introduced by
The function give_get_currency was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
			$currency     = /** @scrutinizer ignore-call */ give_get_currency( $form_id );
Loading history...
50
51
			// Setup the donation details.
52
			$data_to_send = [
53
				'price'           => $data['price'],
54
				'give_form_title' => $data['post_data']['give-form-title'],
55
				'give_form_id'    => $form_id,
56
				'give_price_id'   => isset( $data['post_data']['give-price-id'] ) ? $data['post_data']['give-price-id'] : '',
57
				'date'            => $data['date'],
58
				'user_email'      => $email,
59
				'purchase_key'    => $data['purchase_key'],
60
				'currency'        => $currency,
61
				'user_info'       => $data['user_info'],
62
				'status'          => 'pending',
63
				'gateway'         => $data['gateway'],
64
			];
65
66
			// Record the pending payment.
67
			$donation_id = give_insert_payment( $data_to_send );
0 ignored issues
show
Bug introduced by
The function give_insert_payment was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
			$donation_id = /** @scrutinizer ignore-call */ give_insert_payment( $data_to_send );
Loading history...
68
69
			// Verify donation payment.
70
			if ( ! $donation_id ) {
71
72
				// Record the error.
73
				give_record_gateway_error(
0 ignored issues
show
Bug introduced by
The function give_record_gateway_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
				/** @scrutinizer ignore-call */ 
74
    give_record_gateway_error(
Loading history...
74
					__( 'Payment Error', 'give' ),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
					/** @scrutinizer ignore-call */ 
75
     __( 'Payment Error', 'give' ),
Loading history...
75
					sprintf(
76
					/* translators: %s: payment data */
77
						__( 'Payment creation failed before processing payment via PaiementPro. Payment data: %s', 'give' ),
78
						wp_json_encode( $data )
0 ignored issues
show
Bug introduced by
The function wp_json_encode was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
						/** @scrutinizer ignore-call */ 
79
      wp_json_encode( $data )
Loading history...
79
					),
80
					$donation_id
81
				);
82
83
				// Problems? Send back.
84
				give_send_back_to_checkout( '?payment-mode=' . $data['post_data']['payment-mode'] );
0 ignored issues
show
Bug introduced by
The function give_send_back_to_checkout was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
				/** @scrutinizer ignore-call */ 
85
    give_send_back_to_checkout( '?payment-mode=' . $data['post_data']['payment-mode'] );
Loading history...
85
			}
86
87
			// Auto set payment to abandoned in one hour if donor is not able to donate in that time.
88
			wp_schedule_single_event( time() + HOUR_IN_SECONDS, 'paiementpro4give_set_donation_abandoned', [ $donation_id ] );
0 ignored issues
show
Bug introduced by
The constant HOUR_IN_SECONDS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The function wp_schedule_single_event was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
			/** @scrutinizer ignore-call */ 
89
   wp_schedule_single_event( time() + HOUR_IN_SECONDS, 'paiementpro4give_set_donation_abandoned', [ $donation_id ] );
Loading history...
89
90
			$url          = paiementpro4give_get_api_url();
91
			$merchant_id  = paiementpro4give_get_merchant_id();
92
			$args         = [
93
				'headers' => [
94
					'Content-Type' => 'application/x-www-form-urlencoded',
95
				],
96
				'body'    => [
97
					'merchantId'      => $merchant_id,
98
					'currency'        => 952, // CHF.
99
					'amount'          => $data['price'],
100
					'channel'         => 'CARD',
101
					'customer_id'     => '',
102
					'description'     => give_payment_gateway_donation_summary( $data ),
0 ignored issues
show
Bug introduced by
The function give_payment_gateway_donation_summary was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

102
					'description'     => /** @scrutinizer ignore-call */ give_payment_gateway_donation_summary( $data ),
Loading history...
103
					'email'           => $email,
104
					'firstname'       => $first_name,
105
					'lastname'        => $last_name,
106
					// 'phone_mobile'    => '',
107
					'referenceNumber' => $donation_key,
108
					'notificationURL' => give_get_success_page_uri(),
0 ignored issues
show
Bug introduced by
The function give_get_success_page_uri was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

108
					'notificationURL' => /** @scrutinizer ignore-call */ give_get_success_page_uri(),
Loading history...
109
					'returnContext'   => wp_json_encode(
110
						[
111
							'id_order' => $donation_id,
112
						]
113
					),
114
				],
115
			];
116
			$response     = wp_remote_post( "{$url}init2.php", $args );
0 ignored issues
show
Bug introduced by
The function wp_remote_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
			$response     = /** @scrutinizer ignore-call */ wp_remote_post( "{$url}init2.php", $args );
Loading history...
117
			$responseBody = wp_remote_retrieve_body( $response );
0 ignored issues
show
Bug introduced by
The function wp_remote_retrieve_body was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
			$responseBody = /** @scrutinizer ignore-call */ wp_remote_retrieve_body( $response );
Loading history...
118
			$responseCode = wp_remote_retrieve_response_code( $response );
0 ignored issues
show
Bug introduced by
The function wp_remote_retrieve_response_code was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

118
			$responseCode = /** @scrutinizer ignore-call */ wp_remote_retrieve_response_code( $response );
Loading history...
119
120
			if ( 200 === $responseCode ) {
121
122
				$responseBodyParts = explode( '|', $responseBody );
123
				$sessionId         = $responseBodyParts[1];
124
125
				$redirect_to_url = add_query_arg(
0 ignored issues
show
Bug introduced by
The function add_query_arg was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
				$redirect_to_url = /** @scrutinizer ignore-call */ add_query_arg(
Loading history...
126
					[
127
						'sessionid' => $sessionId,
128
						'id'        => $donation_id,
129
					],
130
					"{$url}processing.php"
131
				);
132
133
			} else {
134
				// Send user to failed page and change donation status to failed as well.
135
				give_update_payment_status( $donation_id, 'failed' );
0 ignored issues
show
Bug introduced by
The function give_update_payment_status was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

135
				/** @scrutinizer ignore-call */ 
136
    give_update_payment_status( $donation_id, 'failed' );
Loading history...
136
				$redirect_to_url = give_get_failed_transaction_uri();
0 ignored issues
show
Bug introduced by
The function give_get_failed_transaction_uri was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

136
				$redirect_to_url = /** @scrutinizer ignore-call */ give_get_failed_transaction_uri();
Loading history...
137
			}
138
139
			wp_redirect( $redirect_to_url );
0 ignored issues
show
Bug introduced by
The function wp_redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

139
			/** @scrutinizer ignore-call */ 
140
   wp_redirect( $redirect_to_url );
Loading history...
140
			give_die();
0 ignored issues
show
Bug introduced by
The function give_die was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

140
			/** @scrutinizer ignore-call */ 
141
   give_die();
Loading history...
141
		}
142
	}
143
}
144
145
new PaiementPro4Give_Card();
146