mercredi 27 mai 2020

How do I create an order in paypal and send the email at the same time

I've been struggling with a problem that when I try to send an email to the customer and redirect him to paypal website (to pay for a product), then he's redirected to the paypal website, but the email is not sent (unless i remove the form that is responsible for redirecting to paypal site).

The way I tried was to put it all in one form and then name the button as send, as it is shown in the script.

 <form method="post" action="/order/createOrder.php">
            First Name: <input type="text" name="first_name"><br>
            Last Name: <input type="text" name="last_name"><br>
            Phone number:<br><textarea rows="5" name="phone_num" cols="30"></textarea><br>
                </div>
                    <button class="btn btn-primary float-right" name="send">Order!</button>
                </div>
            </form>

and this is the script for email

<?php

    if (isset($_POST["send"])) {

        $to = "hellotest@gmail.com";
        $first_name =  $_POST['first_name'];
        $last_name = $_POST['last_name'];
        $phone_num = $_POST['phone_num'];
        $subject = "Zamowienie";

        foreach(Cart::GetRooms() as $room)

        $message = "Name: " . $first_name . "<br>Last name: " . $last_name . "<br>Phone Number:" . $phone_num . "<br> Room:" . $room->GetData()["title"];



        $headers = "From: testert32@gmail.com\r\n";
        $headers.= "MIME-Version: 1.0\r\n";
        $headers.= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        $headers.= "X-Priority: 1\r\n";


        mail($to, $subject, $message, $headers);
    }

this is the redirection code

<?php
require_once "../settings.php";
require_once BASE_DIR."/lib/PayPal/autoload.php";

use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;


$apiContext = new ApiContext(
    new OAuthTokenCredential(
        PAYPAL_CLIENT_ID,
        PAYPAL_SECRET
    )
);

$apiContext->setConfig([
    "mode" => PAYPAL_MODE
]);

// Create new payer and method
$payer = new Payer();
$payer->setPaymentMethod("paypal");

// Set redirect URLs
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl(PAYPAL_REDIRECT_SUCCESS)
  ->setCancelUrl(PAYPAL_REDIRECT_CANCEL);

$total = 0.0;
$order = [];


$item_list = new ItemList();

foreach (Cart::GetRooms() as $offer) {
      $row = $offer->GetData();
      $item_list = new ItemList();
      $item = new Item();
      $item->setQuantity(1);
      $item->setPrice($row["price"]);
      $item->setCurrency("USD");
      $item->setName( $row["title"]);
      $item->setDescription( $row["short_description"]);
      $item_list->addItem($item);

      $total += $row["price"];
      $order = $row["id"];
}

if ($total === 0.0) {
    http_response_code(404);
    exit("Error: Empty!");
}

// Set payment amount
$amount = new Amount();
$amount->setCurrency("USD")
  ->setTotal($total);

Cart::ClearCart();

// Set transaction object
$transaction = new Transaction();
$transaction->setAmount($amount)
  ->setDescription("Order #2131")
  ->setItemList($item_list);

  // Create the full payment object
$payment = new Payment();
$payment->setIntent('sale')
  ->setPayer($payer)
  ->setRedirectUrls($redirectUrls)
  ->setTransactions(array($transaction));

// Create payment with valid API context
try {
    $payment->create($apiContext);

    // Get PayPal redirect URL and redirect the customer
    $approvalUrl = $payment->getApprovalLink();

    // Redirect the customer to $approvalUrl
    header("Location: $approvalUrl");
  } catch (PayPal\Exception\PayPalConnectionException $ex) {
    echo $ex->getCode();
    echo $ex->getData();
    die($ex);
  } catch (Exception $ex) {
    die($ex);
  }



Aucun commentaire:

Enregistrer un commentaire