Exemple #1
0
 public function __construct($consumer_key, $consumer_secret, $statusrequestAPI, $key, $controller, $email, $mail, $name)
 {
     // Parameters sent to you by PesaPal IPN
     $pesapalNotification = Input::get('pesapal_notification_type');
     $pesapalTrackingId = Input::get('pesapal_transaction_tracking_id');
     $pesapal_merchant_reference = Input::get('pesapal_merchant_reference');
     if ($pesapalNotification == "CHANGE" && $pesapalTrackingId != '') {
         $token = $params = NULL;
         $consumer = new OAuthConsumer($consumer_key, $consumer_secret);
         $signature_method = new OAuthSignatureMethodHMACSHA();
         //get transaction status
         $request_status = OAuthRequest::from_consumer_and_token($consumer, $token, "GET", $statusrequestAPI, $params);
         $request_status->set_parameter("pesapal_merchant_reference", $pesapal_merchant_reference);
         $request_status->set_parameter("pesapal_transaction_tracking_id", $pesapalTrackingId);
         $request_status->sign_request($signature_method, $consumer, $token);
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $request_status);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_HEADER, 1);
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
         if (defined('CURL_PROXY_REQUIRED')) {
             if (CURL_PROXY_REQUIRED == 'True') {
                 $proxy_tunnel_flag = defined('CURL_PROXY_TUNNEL_FLAG') && strtoupper(CURL_PROXY_TUNNEL_FLAG) == 'FALSE' ? false : true;
                 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_tunnel_flag);
                 curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
                 curl_setopt($ch, CURLOPT_PROXY, CURL_PROXY_SERVER_DETAILS);
             }
         }
         $response = curl_exec($ch);
         $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
         $raw_header = substr($response, 0, $header_size - 4);
         $headerArray = explode("\r\n\r\n", $raw_header);
         $header = $headerArray[count($headerArray) - 1];
         //transaction status
         $elements = preg_split("/=/", substr($response, $header_size));
         $status = $elements[1];
         //UPDATE YOUR DB TABLE WITH NEW STATUS FOR TRANSACTION WITH pesapal_transaction_tracking_id $pesapalTrackingId
         $payment = Pesapalpayments::where("reference", $pesapal_merchant_reference)->first();
         //check if paypent exists we do not want errors on this page
         if (count($payment) == 1) {
             $payment->status = $status;
             if ($payment->tracking_id != '') {
                 $payment->tracking_id = $pesapalTrackingId;
             }
             $payment->save();
         }
         //Save the transaction details
         $pesapal_transactions = new PesapalpaymentsTransactions();
         $pesapal_transactions->create(['status' => $status, 'tracking_id' => $pesapalTrackingId, 'reference' => $pesapal_merchant_reference, 'data' => serialize(Input::all())]);
         //if status is COMPLETE and the controller is not empty
         //then call controller defined by the user to do whatever it has to
         if ($status == "COMPLETED" && $controller != "") {
             $obj = new $controller();
             echo $obj->updateItem($key, $pesapal_merchant_reference);
             if ($mail == true) {
                 $data = array('status' => $status, 'tracking_id' => $pesapalTrackingId, 'reference' => $pesapal_merchant_reference, 'name' => $name);
                 $user = array('email' => $email, 'name' => $name);
                 Mail::send('pesapal::payment', $data, function ($message) use($user) {
                     $message->to($user['email'], $user['name'])->subject('Payment was processed!');
                 });
             }
         }
         //we do not need to show the pesapal any data if the status is empty
         //so for pesapal to keep querying us when the status changes
         if ($status != "PENDING") {
             $resp = "pesapal_notification_type={$pesapalNotification}&pesapal_transaction_tracking_id={$pesapalTrackingId}&pesapal_merchant_reference={$pesapal_merchant_reference}";
             ob_start();
             echo $resp;
             ob_flush();
             exit;
         }
     }
 }
Exemple #2
0
 /**
  * generates the iframe from the given details
  * @param array $values this array should contain the fields required by pesapal
  * description - description of the item or service
  * currency - if set will override the config settings you have of currency
  * user -which should be your client user id if you have a system of users
  * first_name- the first name of the user that is paying
  * last_name - the last name of the user that is paying
  * email - this should be a valid email or pesapal will throw an error
  * phone_number -which is option if you have the email
  * amount - the total amount to be posted to pesapal
  * reference Please Make sure this is a unique key to the transaction. May be left empty it will be auto generated
  * type - default is MERCHANT
  * frame_height- this is the height of the iframe please provide integers as in 900 without the px
  *
  * @return string the iframe of pesapal
  */
 public function Iframe($values = array())
 {
     global $enabled, $consumer_key, $consumer_secret, $currency;
     //        echo "$currency enabled $enabled consumer_key $consumer_key  consumer_se =$consumer_secret";
     //die();
     $token = $params = NULL;
     //account on demo.pesapal.com. When you are ready to go live make sure you
     //change the secret to the live account registered on www.pesapal.com!
     $signature_method = new Oauth\OAuthSignatureMethod_HMAC_SHA1();
     //set if enabled
     if ($enabled) {
         $iframelink = 'https://www.pesapal.com/api/PostPesapalDirectOrderV4';
     } else {
         $iframelink = 'http://demo.pesapal.com/API/PostPesapalDirectOrderV4';
     }
     $amount = $values['amount'];
     //removed the below code since I saw on the forums pesapal interprets as a fullstop as in 1,000 = 1.0
     // $amount = number_format($amount, 2); //format amount to 2 decimal places
     $desc = $values['description'];
     //$type = $values['type']; //default value = MERCHANT
     if (in_array("reference", $values, false)) {
         $ref = str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 5);
         $reference = substr(str_shuffle($ref), 0, 10);
     } else {
         $reference = $values['reference'];
         //unique order id of the transaction, generated by merchant
     }
     $first_name = $values['first_name'];
     //[optional]
     $last_name = $values['last_name'];
     //[optional]
     $email = $values['email'];
     $type = 'MERCHANT';
     if (!in_array("type", $values)) {
         $type = 'MERCHANT';
     } else {
         $type = $values['type'];
     }
     $phone_number = $values['phone_number'];
     if (in_array("currency", $values, TRUE)) {
         $currency = $values['currency'];
     }
     $amount = number_format($amount, 2);
     //format amount to 2 decimal places
     if (Input::has("currency")) {
         $currency = Input::get('currency');
     }
     //the array data to be posted to pesapal
     $data = array("currency" => $currency, "amount" => $amount, "description" => $desc, "type" => $type, "reference" => $reference, "first_name" => $first_name, "last_name" => $last_name, "phone_number" => $phone_number, "user" => $values['user'], "email" => $email);
     //check to see if there is any payment with this reference id
     //and also avoids duplicates in the database
     $query = Pesapalpayments::where("reference", $reference)->first();
     if (count($query) == 0) {
         Pesapalpayments::create($data);
     }
     $callback_url = url('/pesapal_redirect');
     //redirect url, the page that will handle the response from pesapal.
     $post_xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\t\t\t\t   <PesapalDirectOrderInfo\n\t\t\t\t\t\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n\t\t\t\t\t  \txmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n\t\t\t\t\t  \tCurrency=\"" . $currency . "\"\n\t\t\t\t\t  \tAmount=\"" . $amount . "\"\n\t\t\t\t\t  \tDescription=\"" . $desc . "\"\n\t\t\t\t\t  \tType=\"" . $type . "\"\n\t\t\t\t\t  \tReference=\"" . $reference . "\"\n\t\t\t\t\t  \tFirstName=\"" . $first_name . "\"\n\t\t\t\t\t  \tLastName=\"" . $last_name . "\"\n\t\t\t\t\t  \tEmail=\"" . $email . "\"\n\t\t\t\t\t  \tPhoneNumber=\"" . $phone_number . "\"\n\t\t\t\t\t  \txmlns=\"http://www.pesapal.com\" />";
     $post_xml = htmlentities($post_xml);
     $consumer = new Oauth\OAuthConsumer($consumer_key, $consumer_secret);
     //post transaction to pesapal
     $iframe_src = Oauth\OAuthRequest::from_consumer_and_token($consumer, $token, "GET", $iframelink, $params);
     $iframe_src->set_parameter("oauth_callback", $callback_url);
     $iframe_src->set_parameter("pesapal_request_data", $post_xml);
     $iframe_src->sign_request($signature_method, $consumer, $token);
     // var_dump($post_xml);
     if (!isset($values['frame_height'])) {
         $values['frame_height'] = 900;
     }
     return '<iframe src="' . $iframe_src . '" width="100%" height="' . $values['frame_height'] . 'px" scrolling="no" frameBorder="0">';
     //return '<iframe src="'.$iframe_src.' width="500px" height="620px" scrolling="auto" frameBorder="0"> <p>Unable to load the payment page</p> </iframe>';
 }