function paypal_return($id = null) { //pr($this->params->query); //exit; $payerId = $this->params->query['PayerID']; $token = $this->params->query['token']; //get nvp string //use your own logic to get and set each variable $Payments = $this->Payment->findByToken($token); /*$nvpStr = "TOKEN=$token&PAYERID=$payerId"; $nvpStr .="&PAYMENTREQUEST_0_CURRENCYCODE=MYR"; $nvpStr .="&PAYMENTREQUEST_0_AMT=".number_format($Payments['Payment']['amount'],2,'.',''); $nvpStr .="&PAYMENTREQUEST_0_AMT=".number_format($Payments['Payment']['amount'],2,'.',''); $nvpStr .="&AYMENTREQUEST_0_PAYMENTACTION=Sale"; $nvpStr .="&L_PAYMENTREQUEST_0_ITEMCATEGORY0=Digital"; $nvpStr .= "&L_PAYMENTREQUEST_0_NAME0=Harimau LTD \n Order#".$Payments['Payment']['order_no']; $nvpStr .= "&L_PAYMENTREQUEST_0_QTY0=1"; $nvpStr .="&PAYMENTREQUEST_0_AMT=".number_format($Payments['Payment']['amount'],2,'.',''); if(isset($shipping) and $shipping !=''){ $nvpStr .= "&L_PAYMENTREQUEST_0_AMT0=5.00"; }*/ //echo $nvpStr;exit; $min_order_price = $this->Orderprices->find('first'); $ShippingPrice = $this->ShippingPrice->find('first'); if ($min_order_price['Orderprices']['price'] >= $Payments['Payment']['amount']) { $total = $Payments['Payment']['amount'] + $ShippingPrice['ShippingPrice']['price']; $shipping = 'ship'; } else { $total = $Payments['Payment']['amount']; } $nvpStr = "TOKEN={$token}&PAYERID={$payerId}" . "&PAYMENTREQUEST_0_CURRENCYCODE=MYR" . "&PAYMENTREQUEST_0_AMT=" . number_format($total, 2, '.', '') . "&PAYMENTREQUEST_0_ITEMAMT=" . number_format($Payments['Payment']['amount'], 2, '.', '') . "&AYMENTREQUEST_0_PAYMENTACTION=Sale" . "&L_PAYMENTREQUEST_0_ITEMCATEGORY0=Digital" . "&L_PAYMENTREQUEST_0_NAME0=Harimau LTD \n Order#" . $Payments['Payment']['order_no'] . "&L_PAYMENTREQUEST_0_QTY0=1" . "&L_PAYMENTREQUEST_0_AMT0=" . number_format($Payments['Payment']['amount'], 2, '.', ''); if (isset($shipping) and $shipping == 'ship') { $nvpStr .= "&PAYMENTREQUEST_0_SHIPPINGAMT=" . number_format($ShippingPrice['ShippingPrice']['price'], 2, '.', ''); } //do paypal setECCheckout App::import('Model', 'Paypal'); $paypal = new Paypal(); if ($paypal->doExpressCheckoutPayment($nvpStr) != false) { $result = $paypal->doExpressCheckoutPayment($nvpStr); $updatepaymetn = array(); $updatepaymetn['Payment']['id'] = $Payments['Payment']['id']; $updatepaymetn['Payment']['transaction_id'] = $result['PAYMENTINFO_0_TRANSACTIONID']; $updatepaymetn['Payment']['status'] = $result['PAYMENTINFO_0_PAYMENTSTATUS']; $updatepaymetn['Payment']['amount'] = urldecode($result['PAYMENTINFO_0_AMT']); $updatepaymetn['Payment']['token'] = $token; $updatepaymetn['Payment']['payer_id'] = $payerId; $updatepaymetn['Payment']['paypal_date'] = urldecode($result['PAYMENTINFO_0_ORDERTIME']); if ($this->Payment->save($updatepaymetn)) { if (strtoupper($result['PAYMENTINFO_0_PAYMENTSTATUS']) == strtoupper('Completed')) { $status = 'Paid'; $is_paid = '1'; } else { if (strtoupper($result['PAYMENTINFO_0_PAYMENTSTATUS']) == strtoupper('fail') or strtoupper($result['PAYMENTINFO_0_PAYMENTSTATUS']) == strtoupper('failed')) { $status = 'Canceled'; $is_paid = '2'; } else { $status = 'Pending'; $is_paid = '0'; } } //$this->PaymentConfig->updateAll(array('Image.type' => 'gallery'), array('Image.user_id' => $this->Auth->user('id'))); $this->OrderDetail->updateAll(array('OrderDetail.status' => '"' . $status . '"', 'OrderDetail.is_paid' => '"' . $is_paid . '"', 'OrderDetail.payment_date' => "'" . date('Y-m-d H:i:s') . "'"), array('OrderDetail.order_no' => $Payments['Payment']['order_no'])); $arr['Notification']['type'] = 'Order'; $arr['Notification']['status'] = $status; $arr['Notification']['order_no'] = $Payments['Payment']['order_no']; $arr['Notification']['markas'] = 'Unread'; $arr['Notification']['bell'] = 'On'; $noti = $this->Notification->save($arr); $username = $this->Session->read('Auth.User.username'); if (isset($username) and $username != '') { $min_order_price = $this->Orderprices->find('first'); $ShippingPrice = $this->ShippingPrice->find('first'); $order_products = $this->OrderDetail->find('all', array('conditions' => array('order_no' => $Payments['Payment']['order_no']))); $emaildata = $this->Product->paymentemail($order_products, $min_order_price, $ShippingPrice); $Email = new CakeEmail('default'); $Email->template('welcome', 'fancy')->emailFormat('html')->from(array('*****@*****.**' => 'Harimau'))->to($username)->subject('Order details'); $Email->send($emaildata); } } //echo 'sfsdfsg';exit; } else { $this->log($paypal->errors); $result = false; } if ($result['PAYMENTINFO_0_ACK'] == 'Success') { $this->redirect('/purchases/success'); } //pr($result);exit; //pr(urldecode($result['PAYMENTINFO_0_ORDERTIME'])); // pr($paypal->errors); exit; if (false == $result) { //$this->Session->setFlash(__('Error while making payment, Please try again', true),'message_fail'); } else { // $this->Session->setFlash(__('Thank you for purchasing our deal.', true),'message_ok'); } //echo 'sdfsdf';exit; //$this->render('paypal_back'); }
/** * Redirects buyer after the buyer approves the payment */ public function paypal_return() { $this->layout = 'clean'; $payerId = $this->request->query['PayerID']; $token = $this->request->query['token']; /* If the buyer approves payment,you can optionally call GetExpressCheckoutDetails to obtain buyer details to display to your webpage. */ //do paypal setECCheckout App::import('Model', 'Paypal'); $paypal = new Paypal(); //Build the NVP string $total_codes = $this->request->params['named']['filter']['total_codes']; $codePrice = $this->Upload->Code->getPrice($total_codes); $itemName = $this->Upload->Code->getItemName($total_codes); $nvpCheckoutStr = $paypal->buildNVPCheckoutString($token, $payerId, $codePrice, $itemName); if ($paypal->doExpressCheckoutPayment($nvpCheckoutStr)) { $result = true; } else { $this->log($paypal->errors, 'paypal_debug'); $result = false; } if ($result === false) { $this->Session->setFlash(__('Error while making payment, Please try again', true), 'message_fail'); // Send an email to the administor to see if he can resolve $message = 'There was an error generating the codes for the user\'s upload.'; $message .= '\\n The user\'s name is: ' . $user['User']['fullname']; $message .= '\\n The user\'s email is: ' . $user['User']['email']; $message .= '\\n Upload ID: ' . $upload_id; $message .= '\\n Total codes to generate: ' . $total_codes; $subject = 'Payment error'; $this->_sendErrorEmail($message, $subject); } else { $user_id = $this->request->params['named']['filter']['uuid']; $upload_id = $this->request->params['named']['filter']['uid']; //Generate file codes $codeCreationResult = $this->Upload->Code->generateCodes($upload_id, $total_codes); if ($codeCreationResult === true) { //Activate the user account $this->Upload->User->activate($user_id); //Add the total codes to the uploaded file for easy calculation and make the upload active $this->Upload->read(null, $upload_id); $this->Upload->set(array('total_codes' => $total_codes, 'active' => 1)); $this->Upload->save(); //Send the user their activation email $user = $this->Upload->User->read(null, $user_id); $this->_sendActivationEmail(null, array('user' => $user)); //Codes generated successfully //$this->Session->setFlash(__('Thank you for purchasing.', true),'message_success'); } else { //Code generation failed $this->Session->setFlash(__('Your purchase has completed, but there was an issue with the code generation. The administrator has been notified via email.', true), 'message_fail'); // Send an email to the administor to see if he can resolve $message = 'There was an error generating the codes for the user\'s upload.'; $message .= '\\n The user\'s name is: ' . $user['User']['fullname']; $message .= '\\n The user\'s email is: ' . $user['User']['email']; $message .= '\\n Upload ID: ' . $upload_id; $message .= '\\n Total codes to generate: ' . $total_codes; $subject = 'Code Generation Error'; $this->_sendErrorEmail($message, $subject); } } //This window will close and redirect the user to the login page. To change the redirect, update this in the Views -> Uploads -> paypal_back.ctp file. $this->render('paypal_back'); }