skip to Main Content

I am working on a wordpress website of a restoraunt that has Gift Cards bought in form via PayPal API in PHP, I’m not the one that wrote code on rediricting URL but it’s only calling API using data sent to it with $_SESSION variable.


class GCS_Card_Action extends ElementorProModulesFormsClassesAction_Base {
    public function get_name() {
      return "gift_card";
    }

    public function get_label() {
      return __('Gift Card', 'gift-cards-spending');
    }

    public function run($record, $ajax_handler) {
      @session_start();
      $_SESSION["giftCardFormData"]=json_encode($_POST);
      $form_data = json_decode($_SESSION['giftCardFormData'], true);
      $currency_symbol = $form_data['form_fields']['currency'];

      if ($currency_symbol == '€') {
        $currency = 'EUR';
      } elseif ($currency_symbol == '£') {
        $currency = 'GBP';
      } elseif ($currency_symbol == '$') {
        $currency = 'USD';
      }

      $form_data['form_fields']['currency'] = $currency;
      $amount = floatval($form_data['form_fields']['amount']);

      $price = abs($amount);

    if ($currency != "JPY") {
        $price = number_format($price, 2, ".", "");
    } else {
        $price = number_format($price, 0, ".", "");
    }

    $paypal_request = [
          "intent" => "sale",
          "payer" => [
              "payment_method" => "paypal"
          ],
          "transactions" => [
              [
                  "amount" => [
                      "total" => $price,
                      "currency" => $currency
                  ],
                  
                  "item_list" => [
                      "items" => [
                          [
                              "name" => $form_data['form_fields']['name'],
                              "price" => $price,
                              "currency" => $currency,
                              "quantity" => 1
                          ]
                      ]
                  ]
              ]
          ],
          "redirect_urls" => [
              "return_url" => "X",
              "cancel_url" => "X"
          ]
      ];


      //$request = array_merge($paypal_request, $form_data);
      $request = $paypal_request;

      $_SESSION['paypal_request'] = json_encode($request);
      $redirUrl="/ig-payment";
      $ajax_handler->add_response_data('redirect_url', $redirUrl);
    }

    public function register_settings_section($widget) {
      
    }

    public function on_export($element) {
      
    }
}

This code is ran after submitting the form, sends data to PayPal API that prompts user to login with account and process payment.

I get these three errors

{
  "name": "VALIDATION_ERROR",
  "message": "Invalid request - see details",
  "debug_id": "14a52a47cf143",
  "information_link": "https://developer.paypal.com/docs/api/payments/#errors",
  "details": [
    {
      "field": "transactions[0].amount.total",
      "location": "body",
      "issue": "Currency amount must be non-negative number, contain exactly 2 decimal places separated by '.' (JPY contains 0 decimal places), optional thousands separator ',', limited to 7 digits before the decimal point and currency which is a valid ISO Currency Code"
    },
    {
      "field": "transactions[0].item_list.items[0].price",
      "location": "body",
      "issue": "Currency amount must be non-negative number, contain exactly 2 decimal places separated by '.' (JPY contains 0 decimal places), optional thousands separator ',', limited to 7 digits before the decimal point and currency which is a valid ISO Currency Code"
    },
    {
      "field": "transactions[0].amount.currency",
      "location": "body",
      "issue": "Required field missing"
    }
  ]
}

As you can see transactions[0].amount.currency is there in a structure but it throws Required field missing error

And for transactions[0].amount.total and transactions[0].item_list.items[0].price i get
Currency amount must be non-negative number, contain exactly 2 decimal places separated by ‘.’ (JPY contains 0 decimal places), optional thousands separator ‘,’, limited to 7 digits before the decimal point and currency which is a valid ISO Currency Code
Even if i formatted price variable using

if ($currency != "JPY") {
        $price = number_format($price, 2, ".", "");
    } else {
        $price = number_format($price, 0, ".", "");
}

If i try to echo $_SESSION['paypal_request'] out i get this output

{
  "intent": "sale",
  "payer": { "payment_method": "paypal" },
  "transactions": [
    {
      "amount": { "total": "25.00", "currency": "GBP" },
      "item_list": {
        "items": [
          {
            "name": "EMAIL",
            "price": "25.00",
            "currency": "GBP",
            "quantity": 1
          }
        ]
      }
    }
  ],
  "redirect_urls": {
    "return_url": "X",
    "cancel_url": "X"
  }
}

I tried searching for this error and could only found examples of Vue and React, tried translating it to PHP but it just wont work.

2

Answers


  1. Chosen as BEST ANSWER

    Turns out that code on the receiving page was changed by someone to do everything with data and I didn't need to change symbols or anything, just send $_POST to that page, which was the state of code when I first started working on it.

    But also, when someone edited that code, they made $_SESSION read the wrong variable and it only returned an empty array.


  2. It looks like the price is actually becoming an invalid number like zero for some reason. You can probably find PayPal’s logs in the developer dashboard: https://developer.paypal.com/dashboard/dashboard/sandbox


    You are using the old v1/payments API, which is deprecated. I would recommend changing it to use the current v2/checkout/orders API.

    For payer approval, rather than redirecting away from your site and back you can pair that API with the JS SDK: https://developer.paypal.com/demo/checkout/#/pattern/server

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search