manual.php ➔ give_manual_payment()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 39

Duplication

Lines 19
Ratio 48.72 %

Importance

Changes 0
Metric Value
cc 4
nc 8
nop 1
dl 19
loc 39
rs 9.296
c 0
b 0
f 0
1
<?php
2
/**
3
 * Manual Gateway
4
 *
5
 * @package     Give
6
 * @subpackage  Gateways
7
 * @copyright   Copyright (c) 2016, GiveWP
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Manual Gateway does not need a CC form, so remove it.
19
 *
20
 * @since 1.0
21
 * @return void
22
 */
23
add_action( 'give_manual_cc_form', '__return_false' );
24
25
/**
26
 * Processes the donation data and uses the Manual Payment gateway to record
27
 * the donation in the Donation History
28
 *
29
 * @since 1.0
30
 *
31
 * @param array $purchase_data Donation Data
32
 *
33
 * @return void
34
 */
35
function give_manual_payment( $purchase_data ) {
36
37 View Code Duplication
	if ( ! wp_verify_nonce( $purchase_data['gateway_nonce'], 'give-gateway' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
		wp_die( esc_html__( 'We\'re unable to recognize your session. Please refresh the screen to try again; otherwise contact your website administrator for assistance.', 'give' ), esc_html__( 'Error', 'give' ), array( 'response' => 403 ) );
39
	}
40
41
	//Create payment_data array
42
	$payment_data = array(
43
		'price'           => $purchase_data['price'],
44
		'give_form_title' => $purchase_data['post_data']['give-form-title'],
45
		'give_form_id'    => intval( $purchase_data['post_data']['give-form-id'] ),
46
		'give_price_id'   => isset($purchase_data['post_data']['give-price-id']) ? $purchase_data['post_data']['give-price-id'] : '',
47
		'date'            => $purchase_data['date'],
48
		'user_email'      => $purchase_data['user_email'],
49
		'purchase_key'    => $purchase_data['purchase_key'],
50
		'currency'        => give_get_currency( $purchase_data['post_data']['give-form-id'], $purchase_data ),
51
		'user_info'       => $purchase_data['user_info'],
52
		'status'          => 'pending'
53
	);
54
	// Record the pending payment
55
	$payment = give_insert_payment( $payment_data );
56
57 View Code Duplication
	if ( $payment ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $payment of type false|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
		give_update_payment_status( $payment, 'publish' );
59
		give_send_to_success_page();
60
	} else {
61
		give_record_gateway_error(
62
			esc_html__( 'Payment Error', 'give' ),
63
			sprintf(
64
				/* translators: %s: payment data */
65
				esc_html__( 'The payment creation failed while processing a manual (free or test) donation. Payment data: %s', 'give' ),
66
				json_encode( $payment_data )
67
			),
68
			$payment
0 ignored issues
show
Security Bug introduced by
It seems like $payment defined by give_insert_payment($payment_data) on line 55 can also be of type false; however, give_record_gateway_error() does only seem to accept integer, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
69
		);
70
		// If errors are present, send the user back to the donation page so they can be corrected
71
		give_send_back_to_checkout( '?payment-mode=' . $purchase_data['post_data']['give-gateway'] );
72
	}
73
}
74
75
add_action( 'give_gateway_manual', 'give_manual_payment' );
76