php - PayPal PHP 沙箱 header 验证?

标签 php paypal paypal-sandbox paypal-ipn

我目前在 Paypal 退款测试中遇到了一些问题。

我通过以下方式获得帐户的 token :

 public function __construct(){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_USERPWD, $this->clientID.":".$this->secret);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");

    $result = curl_exec($ch);
    $json = json_decode($result);

    $this->auth = $json->access_token;

   // echo "Auth?" . $this->auth;

 }

当我尝试退款时,无论如何它只会返回 1。交易 ID 是正确的,但它好像没有设置正确的 header - 有人可以帮助我吗?

 public function RefundSale($transaction_id){
     $url = self::getPath() . "/payments/sale/".$transaction_id."/refund";
     echo "URL:" . $url;
     self::rest($url);
 }

 public function rest($url,$data=""){

    $ch = curl_init();

    if ( !empty($data)): $data=json_encode($data); endif; //Array to json?

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    if ( !empty($data)): curl_setopt($ch, CURLOPT_POSTFIELDS, $data); endif;
     curl_setopt($ch, CURLOPT_USERPWD, $this->clientID . ":" . $this->secret);
    $result = curl_exec($ch);

    var_dump($result);
 }

最佳答案

一些事情

  1. 您必须设置 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) 以检索 curl 响应。否则,您只会收到真/假成功/错误消息。
  2. 修复此问题后,代码片段会给出 400(错误请求)http 响应代码。这可以通过无条件调用 curl_setopt($ch, CURLOPT_POSTFIELDS, $data) 来纠正。即使您没有数据要发送,最终也会发送一个空的负载。
  3. 最后一个问题是您对调用进行身份验证的方式。获得授权 token 后,您可以通过发送“Authentication: Bearer” header 对 API 调用进行身份验证。

关于php - PayPal PHP 沙箱 header 验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18844292/

相关文章:

php - 通过 API 下载 Vimeo 视频

php - Paypal 付款高级 : what is the return data for Direct Payment

php - 具有可选值的 PDO 更新

python - 使用 Paypal Python SDK 将 PayPal 集成到 Django 应用程序中

PayPal Pro NVP API 错误本地化

Paypal 付款流程分两步?

php - MySQL PHP : Can't get any results

ruby-on-rails - 用于 rails/activemerchant 的 Paypal 嵌入式支付(iframe 或其他)?

paypal-ipn - 本地主机中的 Paypal 沙盒测试工具 IPN 模拟器

paypal - 为不存在的业务测试 Premier/Business 帐户