【问题标题】:Symfony Paypal "Cannot use object of type RedirectResponse as array"Symfony Paypal“不能使用 RedirectResponse 类型的对象作为数组”
【发布时间】:2016-03-25 15:04:12
【问题描述】:

我设置了一个简单的快速结帐 Paypal,在开发模式下一切正常,但在生产模式下出现错误。

不能使用 Symfony\Component\HttpFoundation\RedirectResponse 类型的对象作为数组

$responseArray = $this->requestPaypal('SetExpressCheckout', array(
        'RETURNURL' => 'http://returnUrl.fr/member/payment/confirm/validate/membership/'.$ref,
        'CANCELURL' => 'http://cancelUrl.fr/member/payment/cancel',
        'PAYMENTREQUEST_0_AMT' =>  $info['price'],            # amount of transaction \
        'PAYMENTREQUEST_0_CURRENCYCODE' => 'EUR',     # currency of transaction \
        'PAYMENTREQUEST_0_DESC0' => $info['description'],
        'PAYMENTREQUEST_0_CUSTOM' => $info['time'],
        ));

    $token = $responseArray['TOKEN']; // Error line

    $payPalUrl = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&useraction=commit&token='.$token.'';  

    return $this->redirect($payPalUrl);

方法请求支付宝:

public function requestPaypal($method, $params)
{
    $params = array_merge($params, array(
        'METHOD' => $method,
        'VERSION' => $this->getParameter('version'),
        'USER' => $this->getParameter('username'),
        'SIGNATURE' => $this->getParameter('signature'),
        'PWD' => $this->getParameter('password'),
        ));

    $params = http_build_query($params);

    $curl = curl_init();
    curl_setopt_array($curl, array(
            CURLOPT_URL => $this->getParameter('endpoint'),
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => $params,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_VERBOSE => 1
    ));

    $response = curl_exec($curl);
    $responseArray = array();
    parse_str($response, $responseArray);

    if(curl_errno($curl))
    {
        $errors = curl_error($curl);
        $this->addFlash('danger', $errors);
        curl_close($curl);
        return $this->redirectToRoute('payment');
    }
    if($responseArray['ACK'] == 'Success')
    {
        curl_close($curl);
    }

    return $responseArray;
}

我认为由于 paypal/sandbox,代码在 prod 中不起作用。

请帮忙

网卡

【问题讨论】:

  • 您发布的代码不包含我认为的错误触发。请添加$this->requestPaypal() 方法。同样在$this->redirect() 中缺少美元符号。请发布您运行的代码,而不是一些编辑过的东西...

标签: php symfony paypal


【解决方案1】:

如我所见,您面临 CURL 错误,您的代码执行以下代码块:if(curl_errno($curl)) { ... }。我假设方法 $this->redirectToRoute 返回 RedirectResponse 对象。之后,您尝试在 RedirectResponse 对象上获取“TOKEN”元素,这会导致错误。要解决此问题,请添加条件以检查方法 requestPaypal 是否返回 RedirectResponse 对象,并将 RedirectResponse 从您的控制器返回到带有 flash 消息的“payment”路由,其中​​包含 CURL 错误消息。

这段代码应该处理这种情况:

$responseArray = $this->requestPaypal('SetExpressCheckout', array(
        'RETURNURL' => 'http://returnUrl.fr/member/payment/confirm/validate/membership/'.$ref,
        'CANCELURL' => 'http://cancelUrl.fr/member/payment/cancel',
        'PAYMENTREQUEST_0_AMT' =>  $info['price'],            # amount of transaction \
        'PAYMENTREQUEST_0_CURRENCYCODE' => 'EUR',     # currency of transaction \
        'PAYMENTREQUEST_0_DESC0' => $info['description'],
        'PAYMENTREQUEST_0_CUSTOM' => $info['time'],
        ));

    if ($responseArray instanceof RedirectResponse) {
        return $responseArray;
    }

    $token = $responseArray['TOKEN'];

    $payPalUrl = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&useraction=commit&token='.$token.'';  

    return $this->redirect($payPalUrl);

【讨论】:

  • 只有当网站使用沙箱进行生产时才会发生此错误,否则如果 live paypal 一切正常
  • 您应该阅读 CURL 错误消息以确定原因。也许您的沙盒 Paypal 帐户凭据无效,或者沙盒已禁用。
猜你喜欢
  • 1970-01-01
  • 2021-12-17
  • 2019-12-01
  • 2012-01-19
  • 2016-07-22
  • 1970-01-01
  • 2019-12-07
  • 1970-01-01
相关资源
最近更新 更多