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' ] ); |
|
|
|
|
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(); |
|
|
|
|
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 ); |
|
|
|
|
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 ); |
|
|
|
|
68
|
|
|
|
69
|
|
|
// Verify donation payment. |
70
|
|
|
if ( ! $donation_id ) { |
71
|
|
|
|
72
|
|
|
// Record the error. |
73
|
|
|
give_record_gateway_error( |
|
|
|
|
74
|
|
|
__( 'Payment Error', 'give' ), |
|
|
|
|
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 ) |
|
|
|
|
79
|
|
|
), |
80
|
|
|
$donation_id |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
// Problems? Send back. |
84
|
|
|
give_send_back_to_checkout( '?payment-mode=' . $data['post_data']['payment-mode'] ); |
|
|
|
|
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 ] ); |
|
|
|
|
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 ), |
|
|
|
|
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(), |
|
|
|
|
109
|
|
|
'returnContext' => wp_json_encode( |
110
|
|
|
[ |
111
|
|
|
'id_order' => $donation_id, |
112
|
|
|
] |
113
|
|
|
), |
114
|
|
|
], |
115
|
|
|
]; |
116
|
|
|
$response = wp_remote_post( "{$url}init2.php", $args ); |
|
|
|
|
117
|
|
|
$responseBody = wp_remote_retrieve_body( $response ); |
|
|
|
|
118
|
|
|
$responseCode = wp_remote_retrieve_response_code( $response ); |
|
|
|
|
119
|
|
|
|
120
|
|
|
if ( 200 === $responseCode ) { |
121
|
|
|
|
122
|
|
|
$responseBodyParts = explode( '|', $responseBody ); |
123
|
|
|
$sessionId = $responseBodyParts[1]; |
124
|
|
|
|
125
|
|
|
$redirect_to_url = add_query_arg( |
|
|
|
|
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' ); |
|
|
|
|
136
|
|
|
$redirect_to_url = give_get_failed_transaction_uri(); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
wp_redirect( $redirect_to_url ); |
|
|
|
|
140
|
|
|
give_die(); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
new PaiementPro4Give_Card(); |
146
|
|
|
|