Paypal PHP SDK HTTP在事务更新期间出错

时间:2015-04-20 00:10:41

标签: php paypal sdk paypal-rest-sdk

我正在为朋友开发一个电子商务网站,并在执行付款之前使用运费更新Paypal,我收到以下错误。当我调用Patch,PatchRequest,然后尝试执行付款时,会发生此错误。以下是所有代码:

if (Input::get('action', 'get') === "getDetails") { //Check to see if the action parameter is set to getDetails

      $payment = \PayPal\Api\Payment::get(Input::get('paymentId', 'get'), $paypalAPI);

      $payerInfo = $payment->getPayer()->payer_info;
      if (!empty($payment)){
        $quantity = 0;
        foreach ($payment->transactions[0]->item_list->items as $item) {
          $quantity += $item->quantity;
        }
        if ($quantity <= 20) {
          $parcelType = "MediumFlatRateBox";
        } else if ($quantity > 20) {
          $parcelType = "LargeFlatRateBox";
        }
        $shipment = \EasyPost\Shipment::create([
          'from_address' => \EasyPost\Address::retrieve(Config::get('easypost/addressObjectID')),
          'to_address' => [
            'name' => $payerInfo->shipping_address->recipient_name,
            'street1'=> $payerInfo->shipping_address->line1,
            'street2' => (isset($payerInfo->shipping_address->line2)) ? $payerInfo->shipping_address->line2 : null,
            'city' =>$payerInfo->shipping_address->city,
            'state' => $payerInfo->shipping_address->state,
            'country' => $payerInfo->shipping_address->country_code,
            'zip' => $payerInfo->shipping_address->postal_code,
            'email' => $payerInfo->email
          ],
          'parcel' => [
            'predefined_package' => $parcelType,
            'weight' => 520
          ]
        ]);
        //Grab the lowest shipping rate
        $shippingRate = $shipment->lowest_rate()->rate;
        //Make a call to PayPal updating our transaction with the tax and shipping rate
        $amount = $payment->transactions[0]->amount;
        $transactionUpdate = new \PayPal\Api\Patch();
        $transactionUpdate    ->setOp('replace')
                              ->setPath('transactions/0/amount')
                              ->setValue(json_decode('{
                                "total": "'.$amount->total.'",
                                "currency":"USD",
                                "detail": {
                                  "subtotal": "'.$amount->details->subtotal.'",
                                  "shipping":"'.$shippingRate.'"
                                }
                              }'));


        //Instantiate a new instance of PayPal's Patch Request Class and Update the Transaction with the tax and shipping rate
        $updateRequest = new \PayPal\Api\PatchRequest();
        $updateRequest->setPatches(
          [$transactionUpdate]
        );
        //Attempt Update

        $result = $payment->update($updateRequest, $paypalAPI);

        if ($result) {
          $transID = generateTransactionID();
          $fields = [
            'dateCreated' => strtotime($payment->create_time),
            'transID' => $transID,
            'paymentID' => $payment->id,
            'shipmentID' => $shipment->id
          ];
          $db->insert('transactionLog', $fields);
          Redirect::to('shoppingCartFinalize.php?transID='.$transID);
          exit();
        } else {
          Alert::set([
            'header' => "Uh Oh....Something Went Wrong",
            'message' => "Unable to update transaction. Transaction Cancelled. Please try again.",
            'type' => 'danger',
            'close' => 1
          ]);
          Redirect::to('shoppingCartView.php');
          exit();
        }
      }

以下是我在通话过程中遇到的错误:

  

致命错误:未捕获的异常   &#39;贝\异常\ PayPalConnectionException&#39;有消息&#39;得到了Http   访问时响应代码400   https://api.sandbox.paypal.com/v1/payments/payment/PAY-77N347011V970714EKU2D24Q&#39。   在   /var/www/html/myla-dev/vendor/paypal/rest-api-sdk-php/lib/PayPal/Core/PayPalHttpConnection.php:177   堆栈跟踪:#0   /var/www/html/myla-dev/vendor/paypal/rest-api-sdk-php/lib/PayPal/Transport/PayPalRestCall.php(74):   PayPal \ Core \ PayPalHttpConnection-&gt;执行(&#39; [{&#34;操作&#34;:&#34;替换......&#39;)#1   /var/www/html/myla-dev/vendor/paypal/rest-api-sdk-php/lib/PayPal/Common/PayPalResourceModel.php(103):   PayPal \ Transport \ PayPalRestCall-&gt;执行(数组,&#39; / v1 / payments / pa ...&#39;,   &#39; PATCH&#39;,&#39; [{&#34; op&#34;:&#34;替换...&#39;,NULL)#2   /var/www/html/myla-dev/vendor/paypal/rest-api-sdk-php/lib/PayPal/Api/Payment.php(474):   贝宝\ COMMON \ PayPalResourceModel :: executeCall(&#39; / V1 /支付/ PA ...&#39 ;,   &#39; PATCH&#39;,&#39; [{&#34; op&#34;:&#34;替换...&#39;,NULL,对象(PayPal \ Rest \ ApiContext),   NULL)#3 /var/www/html/myla-dev/apiProcessing.php(146):   PayPal \ Api \ Payment-&gt; update(对象(PayPal \ Api \ in。)   /var/www/html/myla-dev/vendor/paypal/rest-api-sdk-php/lib/PayPal/Core/PayPalHttpConnection.php   在第177行

更新:感谢您对Thaer的帮助。它有所帮助。现在我收到一个单独的错误。当我尝试更新付款时,现在说出以下内容:

  

阵列(       [name] =&gt; PAYMENT_STATE_INVALID       [message] =&gt;由于当前的付款状态,此请求无效       [information_link] =&gt; https://developer.paypal.com/webapps/developer/docs/api/#PAYMENT_STATE_INVALID       [debug_id] =&gt; 1b1b9b71e91d8)

如果你知道如何解决这个问题。我不知道如何更改状态,以便更新付款。

如果您发现错误,请告诉我。我很困惑,这不好笑。

戴夫道格拉斯

2 个答案:

答案 0 :(得分:1)

请参阅此Answer 我想你必须尝试 - 抓住你的代码来确定错误 ,问候。

答案 1 :(得分:1)

首先,您能否确认付款是否已经执行?

请注意,它只能在执行完成之前更新。一旦付款被执行,就无法对此进行更新。文档:https://developer.paypal.com/webapps/developer/docs/api/#update-a-payment-resource

我认为您的付款已经执行,因此state将是complete而不是created,这会导致400状态无效状态。

如果这有意义,请告诉我。

要更改付款,您可以创建退款,以退还剩余金额。

或者,您也可以使用授权或订单,先保留资金,然后再实际收取费用。您是如何在餐馆等中看到的,他们持有您的总金额,但最终会在稍后添加提示,并在稍后完成交易。

有关详细信息,请参阅此处:https://developer.paypal.com/webapps/developer/docs/integration/direct/create-process-order/