Created
August 15, 2012 14:43
-
-
Save mmiliaus/3360698 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Zend Controller responsible for handling online payments. | |
* + Talking to PayPal API | |
* + Talking to PaymentSense API | |
* + Talking to SalesForce API | |
* + Auto-registering paying user with our system | |
* + Notifying about new lead | |
* | |
* @author Martynas Miliauskas <[email protected]> | |
* @copyright 2008-2012 GulfJobsMarket Ltd. | |
*/ | |
class PaymentsController extends Zend_Controller_Action | |
{ | |
protected $_redirector = null; | |
protected $shopping_basket = null; | |
protected $user = null; | |
function preDispatch() { | |
// forcing protected connection | |
$this->_helper->HTTPSForcer(); | |
} | |
public function init() | |
{ | |
$this->_redirector = $this->_helper->getHelper('Redirector'); | |
//=todo below is only for guests | |
$this->shopping_basket = new Zend_Session_Namespace('guest_shopping_basket'); | |
$this->user = $this->shopping_basket->user; | |
} | |
// ... | |
/** | |
* Handles credit card processing using PayPal | |
* | |
* @param String $token_id identification token issued by PayPal | |
* @param Int $payer_id | |
* if all OK | |
* @return array ( PayPalClient object, array containing reply from PayPal) | |
* else | |
* renders error page, containing explanation of what caused the error | |
*/ | |
private function checkout_with_paypal($token, $payer_id) | |
{ | |
global $config; | |
$session = new Zend_Session_Namespace('guest_shopping_basket'); | |
// creating a PayPal object | |
$paypal = new PayPalClient( | |
$config->payments->paypal->express->username, | |
$config->payments->paypal->express->password, | |
$config->payments->paypal->express->signature, | |
$config->payments->paypal->api->nvp, | |
$config->payments->paypal->api->express | |
); | |
// running a purchase | |
try { | |
$reply_data = $paypal->doExpressCheckout( | |
$token, | |
$payer_id, | |
$session->order_summary['price'], | |
'USD' | |
); | |
} catch (PayPal_Exception $e) { | |
$this->view->errors = array( | |
'xxxx' | |
); | |
$this->view->partial_path = 'guest/_back_to_login.phtml'; | |
# redirect to errors page | |
$this->renderScript('layout/errors.phtml'); | |
exit; | |
} | |
return array($paypal, $reply_data); | |
} | |
/** | |
* Notifying system administrator with an email, about failed purchase | |
* | |
* @param Zend_Db_Table_Row $user | |
* @param Zend_Db_Table_Row $product | |
* @param Zend_Db_Table_Row $campaign_details | |
* @param String $token | |
* @param Int $payer_id | |
*/ | |
private function notify_user_about_failed_purchase( $user, $product, $campaign_details, $token, $payer_id ) | |
{ | |
$order_details = array( | |
'error' => true, | |
'email' => $user['email'], | |
'firstName' => $user['first_name'], | |
'token' => $token, | |
'payerID' => $payer_id, | |
'amount' => $product['price'] . ' ' . 'USD', | |
'purchaseID' => 'NO PURCHASE HAS BEEN MADE', | |
'orderDetails' => $campaign_details ? $campaign_details->name." (".$product['price']." credits)" : "" | |
); | |
Emails_Factory::sendPaymentNotificationEmail( | |
$user['email'], | |
$order_details | |
); | |
} | |
/** | |
* Notify system administrator about successfull purchase with an email | |
* | |
* @param Zend_Db_Table_Row $user | |
* @param Int $purchase_id | |
* @param Zend_Db_Table_Row $product | |
* @param Zend_Db_Table_Row $campaign_details | |
* @param String $token | |
* @param Int $payer_id | |
*/ | |
private function notify_user_about_successfull_purchase( $user, $purchase_id, $product, $campaign_details, $token, $payer_id ) | |
{ | |
$email_data = array( | |
'email' => $user['email'], | |
'firstName' => $user['first_name'], | |
'token' => $token, | |
'payerID' => $payer_id, | |
'amount' => $product['price'] . ' ' . 'USD', | |
'purchaseID' => $purchase_id, | |
'orderDetails' => $campaign_details->name." (".$product['price']." credits)" // string should be constructed like “CAMPAIGN_NAME (PRODUCT_PRICE credits)” | |
); | |
Emails_Factory::sendPaymentNotificationEmail( | |
$user['email'], // this is a email address to send email to | |
$email_data | |
); | |
} | |
/** | |
* Upsert lead into SalesForce, and notify system admin about it | |
* | |
* @param Zend_Db_Table_Row $user | |
* @param String $case | |
*/ | |
private function upsert_lead_and_send_email_about_attempt_to_register($user, $case) | |
{ | |
$this->upsert_lead($user['first_name'], $user['last_name'], $user['company_name'], $user['phone'], $user['email']); | |
$user['case'] = $case; | |
Emails_Factory::sendUserSoftRegisteredEmail($user); | |
} | |
/** | |
* Upserts lead into SalesForce | |
* | |
* @param String $first_name | |
* @param String $last_name | |
* @param String $company | |
* @param String $phone | |
* @param String $email | |
* @param String $employees | |
* @param String $enquiry | |
*/ | |
private function upsert_lead($first_name, $last_name, $company, $phone, $email, $employees = null, $enquiry = null) | |
{ | |
$url = "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8"; | |
$fields = array( | |
'oid'=>urlencode('xxxxxx'), | |
'retURL'=>urlencode('/public/login/?callback=true'), | |
'first_name'=>urlencode($first_name), | |
'last_name'=>urlencode($last_name), | |
'company'=>urlencode($company), | |
'phone'=>urlencode($phone), | |
'email'=>urlencode($email) | |
); | |
if (($employees === null) and ($enquiry === null)) { | |
$fields['description'] = urlencode('Attempt to register'); | |
} else { | |
$fields['description'] = urlencode('Number of employees: ' . $employees . '; Enquiry details: ' . $enquiry); | |
} | |
//url-ify the data for the POST | |
$fields_string = ''; | |
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } | |
rtrim($fields_string,'&'); | |
//open connection | |
$ch = curl_init(); | |
//set the url, number of POST vars, POST data | |
curl_setopt($ch,CURLOPT_URL,$url); | |
curl_setopt($ch,CURLOPT_POST,count($fields)); | |
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); | |
//execute post | |
$result = curl_exec($ch); | |
//close connection | |
curl_close($ch); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment