@@ -25,6 +25,8 @@ class WooViet_VND_PayPal_Standard {
25
25
*/
26
26
protected $ paypal_currency = 'USD ' ;
27
27
28
+ //@todo - declare two vars
29
+
28
30
/**
29
31
* WooViet_VND_PayPal_Standard constructor.
30
32
*
@@ -44,8 +46,12 @@ public function __construct( $exchange_rate_to_vnd = 22770, $paypal_currency ) {
44
46
// Add the exchange rate info for this gateway in the checkout page before proceeding in the PayPal pages
45
47
add_filter ( 'option_woocommerce_paypal_settings ' , array ( $ this , 'add_exchange_rate_info ' ), 11 );
46
48
47
- // Match currency of Paypal with local order
48
- add_action ( 'valid-paypal-standard-ipn-request ' , array ( $ this , 'match_currency_order ' ), 10 );
49
+ // Match currency and amount between Paypal and WC Order
50
+ add_action ( 'valid-paypal-standard-ipn-request ' , array ( $ this , 'match_order_currency_and_amount ' ), 5 );
51
+
52
+ // Restore currency and amount for WC Order
53
+ add_action ( 'valid-paypal-standard-ipn-request ' , array ( $ this , 'restore_order_currency_and_amount ' ), 15 );
54
+
49
55
}
50
56
51
57
/**
@@ -111,17 +117,78 @@ public function add_exchange_rate_info( $value ) {
111
117
}
112
118
113
119
/*
114
- * Match response currency from Paypal IPN with the order
120
+ * Match currency and amount from Paypal IPN with the order
115
121
*
116
122
* Topic https://wordpress.org/support/topic/loi-order-bi-on-hold/
117
123
*
118
- * @author Longkt
119
- * @since 1.4
124
+ * @author htdat
125
+ * @since 1.4.3
126
+ */
127
+ public function match_order_currency_and_amount ($ posted ) {
128
+
129
+ $ order = ! empty ( $ posted ['custom ' ] ) ? $ this ->get_paypal_order ( $ posted ['custom ' ] ) : false ;
130
+
131
+ if ( $ order ) {
132
+ $ this ->original_order_currency = $ order ->get_currency ();
133
+ $ this ->original_order_total = $ order ->get_total ();
134
+
135
+ $ order ->set_currency ( $ posted ['mc_currency ' ] );
136
+ $ order ->set_total ( $ posted ['mc_gross ' ] );
137
+
138
+ $ order ->save ();
139
+ }
140
+
141
+ }
142
+
143
+ /*
144
+ * Restore currency and amount of the order after the 'match_order_currency_and_amount' action
145
+ *
146
+ * @author htdat
147
+ * @since 1.4.3
120
148
*/
121
- public function match_currency_order ($ posted ) {
122
- if ($ posted ['mc_currency ' ]) {
123
- $ posted ['mc_currency ' ] = $ order ->get_currency ();
124
- }
149
+ public function restore_order_currency_and_amount ($ posted ) {
150
+
151
+ $ order = ! empty ( $ posted ['custom ' ] ) ? $ this ->get_paypal_order ( $ posted ['custom ' ] ) : false ;
152
+
153
+ if ( $ order ) {
154
+
155
+ $ order ->set_currency ( $ this ->original_order_currency );
156
+ $ order ->set_total ( $ this ->original_order_total );
157
+
158
+ $ order ->save ();
159
+ }
160
+
125
161
}
126
162
163
+
164
+ /**
165
+ * @see Grab this code from - can not call it directly https://github.com/woocommerce/woocommerce/blob/f5c2f89af6a9421af8edc2a4aa20d372e5be40f8/includes/gateways/paypal/includes/class-wc-gateway-paypal-response.php#L30
166
+ *
167
+ * @since 1.4.3
168
+ * @author htdat
169
+ */
170
+ protected function get_paypal_order ( $ raw_custom ) {
171
+ // We have the data in the correct format, so get the order.
172
+ $ custom = json_decode ( $ raw_custom );
173
+ if ( $ custom && is_object ( $ custom ) ) {
174
+ $ order_id = $ custom ->order_id ;
175
+ $ order_key = $ custom ->order_key ;
176
+ } else {
177
+ // Nothing was found.
178
+ WC_Gateway_Paypal::log ( 'Order ID and key were not found in "custom". ' , 'error ' );
179
+ return false ;
180
+ }
181
+ $ order = wc_get_order ( $ order_id );
182
+ if ( ! $ order ) {
183
+ // We have an invalid $order_id, probably because invoice_prefix has changed.
184
+ $ order_id = wc_get_order_id_by_order_key ( $ order_key );
185
+ $ order = wc_get_order ( $ order_id );
186
+ }
187
+ if ( ! $ order || $ order ->get_order_key () !== $ order_key ) {
188
+ WC_Gateway_Paypal::log ( 'Order Keys do not match. ' , 'error ' );
189
+ return false ;
190
+ }
191
+ return $ order ;
192
+ }
193
+
127
194
}
0 commit comments