/** * Checks information received from PayPal to ensure it's correct data. * If correct, stores updated transaction details. * * @param int $order_id Registration # of event being paid * @param float $mc_gross Amount paid by user during the PayPal checkout * @param int $paid_by User ID of player who made the payment */ function validatePayment($order_id, $mc_gross, $paid_by) { // is the item number a valid registration? $registration = Registration::load(array('order_id' => $order_id)); if (!$registration) { $status = array('status' => false, 'message' => 'Invalid Registration ID'); return $status; } // has registration been already paid? //if ($registration->payment_type == 'Paid') { // $status = array('status' => false, 'message' =>'Registration '.$order_id.' already paid in full'); // return $status; //} // is the registration attached to the correct Event $event = Event::load(array('registration_id' => $registration->registration_id)); if (!$event) { $status = array('status' => false, 'message' => 'Invalid Event ID'); return $status; } // does the price paid and registration cost match? if ($mc_gross != $event->cost) { $status = array('status' => false, 'message' => 'Amount Paid does not match Registration Cost'); return $status; } // Payment is valid, and should be saved $payment = new RegistrationPayment(); $payment->set('order_id', $registration->order_id); // TODO: PDT returns from PayPal are logged under the Paypal account. // Would be nice to find a better way to do this instead of a Paypal user account $payment->set('entered_by', 999); // assign requrired values to the RegistrationPayment from the talkback results $payment->set('payment_type', 'Full'); $payment->set('payment_amount', $mc_gross); $payment->set('payment_method', 'PayPal'); $payment->set('paid_by', $paid_by); $payment->set('date_paid', date("Y-m-d")); // Save the payment if it's not already stored in the database // It's possible that the IPN payment beats the user PDT return. // Still need to ensure user is informed correctly, while not displaying any errors. if ($registration->payment_type != 'Paid') { if (!$payment->save()) { $status = array('status' => false, message => "Couldn't save payment to database"); return $status; } // update registration in question $registration->set('payment', 'Paid'); if (!$registration->save()) { $status = array('status' => false, message => "Internal error: couldn't save changes to registration"); } } // if successful, return the $payment to handle/display to user return array('status' => true, 'message' => $payment); }