1 Replies - 751 Views - Last Post: 25 April 2013 - 10:36 PM Rate Topic: -----

#1 php learning curve  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-April 13

Replace user input "form action" to call PHP file

Posted 25 April 2013 - 08:37 PM

Hi,

I am quite new to PHP so hopefully I can explain this clearly. Let me know if anything needs clarification.

Problem: Between PayPal and the Cart66 Lite shopping cart, customers are having to press too many buttons. The PHP code called by the Cart page, via the customer clicking the checkout button, unnecessarily calls a Checkout page which is basically a repeat of the same information found on the Cart page. The Checkout page requires the customer to click yet another button to go to PayPal checkout. I want to bypass this button and replace the "form action" with a direct call to the PHP file that calls the PayPal checkout. Here is the code that I want to replace:

<form action="" method='post' id="paypalexpresscheckout">
  <input type='hidden' name='cart66-action' value='paypalexpresscheckout'>
  <input type="image" id='PayPalExpressCheckoutButton' src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" value="PayPal Express Checkout" name="PayPal Express Checkout" />
</form>



The form action code is located between the 2 following lines of code:

<?php if($settingsOk): ?>
<?php endif; ?>



I think the following code indicates that I need to call Cart66PayPalExpressCheckout.php to take the customer to PayPal checkout. For your reference, I have attached the PHP file which I am attempting to change: paypal-expresscheckout.php.:

if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['cart66-action']) && $_POST['cart66-action'] == 'paypalexpresscheckout') {
  // Set up the PayPal object
  $pp = new Cart66PayPalExpressCheckout();



Basicially, I need to know the correct syntax for calling the correct PHP file which calls the PayPal checkout, which in short will replace the button click by the customer. This code will fall between:

<?php if($settingsOk): ?>
<?php endif; ?>



I've tried various things such as:

<?php if($settingsOk): ?>
exec('php Cart66PayPalExpressCheckout.php');
<?php endif; ?>



But so far nothing has worked.

I look forward to your solutions.

Thanks,
Nathen

For some reason, attaching the file did not work so I have pasted it below:

<?php
$settingsOk = true;
$username = Cart66Setting::getValue('paypalpro_api_username');
$password = Cart66Setting::getValue('paypalpro_api_password');
$signature = Cart66Setting::getValue('paypalpro_api_signature');
if(!($username && $password && $signature)) {
  $settingsOk = false;
  throw new Cart66Exception('Invalid PayPal Express Configuration', 66501);
}

if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['cart66-action']) && $_POST['cart66-action'] == 'paypalexpresscheckout') {
  // Set up the PayPal object
  $pp = new Cart66PayPalExpressCheckout();
  
  // Calculate total amount to charge customer
  $total = Cart66Session::get('Cart66Cart')->getGrandTotal(false);
  $total = number_format($total, 2, '.', '');
  Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] PayPal Express Checkout grand total: $total");
  
  // Calculate total cost of all items in cart, not including tax and shipping
  $itemTotal = Cart66Session::get('Cart66Cart')->getNonSubscriptionAmount();
  $itemTotal = number_format($itemTotal, 2, '.', '');
  Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] PayPal Express Checkout item total: $itemTotal");
  
  // Calculate shipping costs
  $shipping = Cart66Session::get('Cart66Cart')->getShippingCost();
  $promotion = Cart66Session::get('Cart66Promotion');
  $discount = Cart66Session::get('Cart66Cart')->getDiscountAmount();

  if(is_object($promotion) && $promotion->apply_to == 'total') {
    $itemTotal = Cart66Session::get('Cart66Cart')->getNonSubscriptionAmount();
    $itemDiscount = Cart66Session::get('Cart66Cart')->getDiscountAmount();
    if($itemDiscount > 0) {
      $itemTotal = $itemTotal - $itemDiscount;            
    }
    if($itemTotal <= 0) {
      $discount = Cart66Session::get('Cart66Cart')->getNonSubscriptionAmount();
      $shipping = $shipping + $itemTotal;
      $itemTotal = 0;
    }

  }

  if(is_object($promotion) && $promotion->apply_to == 'products'){
    $itemTotal = Cart66Session::get('Cart66Cart')->getNonSubscriptionAmount() - Cart66Session::get('Cart66Cart')->getDiscountAmount();
  }

  if(is_object($promotion) && $promotion->apply_to == 'shipping'){
    $shipping = $shipping - Cart66Session::get('Cart66Cart')->getDiscountAmount();
    $discount = 0;
  }
  
  // Calculate IPN URL
  $ipnPage = get_page_by_path('store/ipn');
  $ipnUrl = get_permalink($ipnPage->ID);

  // Set shipping as an item if the item total is $0.00, otherwise PayPal will fail
  if($itemTotal == 0 && $shipping > 0) {
    Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Setting shipping to be an item because the item total would otherwise be $0.00");
    $itemTotal = $shipping;
    $itemData = array(
      'NAME' => 'Shipping',
      'AMT' => $shipping,
      'NUMBER' => 'SHIPPING',
      'QTY' => 1
    );
    //$pp->addItem($itemData);
    $shipping = 0;
  }
  else {
    Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Not making shipping part of the item list. Item Total: $itemTotal");
  }
  
  // Set payment information
  $payment = array(
    'AMT' => $total,
    'CURRENCYCODE' => CURRENCY_CODE,
    'ITEMAMT' => $itemTotal,
    'SHIPPINGAMT' => $shipping,
    'NOTIFYURL' => $ipnUrl
  );
  Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Setting Payment Details:\n".print_r($payment,true));
  $pp->setPaymentDetails($payment);
  
  // Add cart items to PayPal
  $pp->populatePayPalCartItems();
  
  // Set Express Checkout URLs
  $returnPage = get_page_by_path('store/express');
  $returnUrl = get_permalink($returnPage->ID);
  $cancelPage = get_page_by_path('store/checkout');
  $cancelUrl = get_permalink($cancelPage->ID);
  $localeCode = Cart66Common::getLocaleCode();
  $ecUrls = array(
    'RETURNURL' => $returnUrl,
    'CANCELURL' => $cancelUrl,
    'LOCALECODE' => $localeCode
  );
  $pp->setEcUrls($ecUrls);
  
  $response = $pp->SetExpressCheckout();
  $ack = strtoupper($response['ACK']);
  if('SUCCESS' == $ack || 'SUCCESSWITHWARNING' == $ack) {
    Cart66Session::set('PayPalProToken', $response['TOKEN']);
    $expressCheckoutUrl = $pp->getExpressCheckoutUrl($response['TOKEN']);
  	wp_redirect($expressCheckoutUrl);
  	exit;
  }
  elseif(empty($ack)) {
      echo '<pre>Failed to connect via curl to PayPal. The most likely cause is that your PHP installation failed to verify that the CA cert is OK</pre>';
  }
  else {
    try {
      throw new Cart66Exception(ucwords($response['L_SHORTMESSAGE0']), 66503);
    }
    catch(Cart66Exception $e) {
      $exception = Cart66Exception::exceptionMessages($e->getCode(), $e->getMessage(), array('Error Number: ' . $response['L_ERRORCODE0'], $response['L_LONGMESSAGE0']));
      echo Cart66Common::getView('views/error-messages.php', $exception);
    }
  }
}
?>

<?php if($settingsOk): ?>
<form action="" method='post' id="paypalexpresscheckout">
  <input type='hidden' name='cart66-action' value='paypalexpresscheckout'>
  <input type="image" id='PayPalExpressCheckoutButton' src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" value="PayPal Express Checkout" name="PayPal Express Checkout" />
</form>
<?php endif; ?>



Is This A Good Question/Topic? 0
  • +

Replies To: Replace user input "form action" to call PHP file

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,488
  • Joined: 12-December 12

Re: Replace user input "form action" to call PHP file

Posted 25 April 2013 - 10:36 PM

exec('php Cart66PayPalExpressCheckout.php');

Erm, exec is to run an external program, not a php file. If you want to switch to a different page:

header('Location: Cart66PayPalExpressCheckout.php');

but this will only work if no output has been sent from the current file. Or, if you want to include the content from another page, it could be include() or require().

However, if you are not sure what you are doing, as seems the case with your use of exec() and "tried various things" :dontgetit: then I recommend that you employ someone to do this for you. There is a Post a Job forum.

This post has been edited by andrewsw: 25 April 2013 - 10:41 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1