php – 在我的代码沙箱PayPal的未来付款是什么问题

我已经在我的应用程序中使用alredy启用未来的付款权限,并使用开发人员dashboard.but但不工作,请找到错误
http://developer.paypal.com/并登录
https://developer.paypal.com/developer/accountStatus在那里你可以看到你有什么允许.
$data = array(
                "intent" => "authorize","payer" => array(
                    "payment_method" => "paypal"
                ),"transactions" => array(
                    array("amount" => array(
                        "currency" => "USD","total" => "1.88"
                    ),"description" => "future of sauces")
                ));

        $data_string = json_encode($data);
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,"https://api.sandbox.paypal.com/v1/payments/payment");
        $headers = array(
            'Content-Type: application/json','PayPal-Client-Metadata-Id: d6743cbb53ea4470a53bfe458f0cb885','Authorization: Bearer A103.B7d5318JDS6NA1zGh02avjCx16oxnBPadUat5z9SlGVGEiOhoAeMuqyz0anGSICS.FAkzECypTS1IXfemHcpVa5yyrGu',);
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
        //curl_setopt($ch,CURLINFO_HEADER_OUT,true);
        curl_setopt($ch,CURLOPT_POST,CURLOPT_POSTFIELDS,$data_string);
        $result = curl_exec($ch);
        $information = curl_getinfo($ch);
        curl_close($ch);
        print_r($information);
        die;

放在这里

{"name":"PERMISSION_DENIED","message":"No permission for the requested operation","information_link":"https://developer.paypal.com/docs/api/#PERMISSION_DENIED","debug_id":"5b39efd4cf370"}Array
(
    [url] => https://api.sandbox.paypal.com/v1/payments/payment
    [content_type] => application/json
    [http_code] => 403
    [header_size] => 592
因为我没有得到任何人的任何解决方案,所以我逐步挖掘我的代码,并找到解决方案.
function paypalFuturePayment($userID,$amount)
{
    $amount=number_format($amount,2);
    /* paypal App truxx dev client and secret key*/
    if($userID && $amount){
    $userData = selectById('tbl_users','*',"id='".$userID."'");
    $refresh_token = $userData['paypal_refresh_tokens'];
    $Metadata_id = $userData['paypal_metadata_id'];
    if($refresh_token && $Metadata_id){
        if ($_SERVER['SERVER_NAME'] == 'syonserver.com') {

            $clientId = "xxxxx";
            $secret = "xxx";

            $url1="https://api.sandbox.paypal.com/v1/oauth2/token";
            $url2="https://api.sandbox.paypal.com/v1/payments/payment";

        }else{
            $account = 0; // 0 for sandbox,1 for live
            if ($account == 1) {
                //client live
                $clientId = "xxx";
                $secret = xxx";
                $url1 = "https://api.paypal.com/v1/oauth2/token";
                $url2 = "https://api.paypal.com/v1/payments/payment";
            } else {
                //client sandbox
                $clientId = "xxx";
                $secret = "xxx";
                $url1 = "https://api.sandbox.paypal.com/v1/oauth2/token";
                $url2 = "https://api.sandbox.paypal.com/v1/payments/payment";
        }
    }



//print_r($refresh_token);die;

        $ch = curl_init();
        curl_setopt($ch,$url1);
        curl_setopt($ch,CURLOPT_HEADER,"Content-Type: application/x-www-form-urlencoded");
        curl_setopt($ch,CURLOPT_USERPWD,$clientId . ":" . $secret);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,"grant_type=refresh_token&refresh_token=" . $refresh_token);
        $result = curl_exec($ch);
        curl_close($ch);
        $result=json_decode($result);

       //11111111   for payment Authorize: For example,to first authorize the payment,use a request similar to this:

        $access_token =  $result->access_token;
        $data = array(
            "intent" => "authorize","payer" => array(
                "payment_method" => "paypal"
            ),"transactions" => array(
                array("amount" => array(
                    "currency" => "USD","total" => $amount
                ),"description" => "future of sauces")
            ));

        $data_string = json_encode($data);
        $ch1 = curl_init();
        curl_setopt($ch1,$url2);
        $headers = array(
            'Content-Type: application/json','PayPal-Client-Metadata-Id: '.$Metadata_id,'Authorization: Bearer '.$access_token,'Content-Length: ' . strlen($data_string)
        );
        curl_setopt($ch1,$headers);
        curl_setopt($ch1,true);
        curl_setopt($ch1,1);
        curl_setopt($ch1,$data_string);
        $result1 = curl_exec($ch1);
        curl_close($ch1);

        $result1=json_decode($result1);
        $message  = $result1->message;

       if($result1->state=='approved'){
           $access_id=  $result1->transactions[0]->related_resources[0]->authorization->id;

       }else{
           if(empty($message)){
               $message ='Authorization error,Please try again.';
           }
           return array('response' => '','success' => '0','message'=>$message);
       }

       // print_r($result1);die;

      //2222222   capture the payment:
        $data =  array("amount" => array(
            "currency" => "USD","total" => $amount
        ),"is_final_capture" => "true"
        );
        $data_string = json_encode($data);
        $ch2 = curl_init();
        if ($_SERVER['SERVER_NAME'] == 'syonserver.com') {
            curl_setopt($ch2,"https://api.sandbox.paypal.com/v1/payments/authorization/$access_id/capture");
        }else {
            $account = 0; // 0 for sandbox,1 for live
            if ($account == 1) {
                //client live
             curl_setopt($ch2,"https://api.paypal.com/v1/payments/authorization/$access_id/capture");
        }else{
                curl_setopt($ch2,"https://api.sandbox.paypal.com/v1/payments/authorization/$access_id/capture");
            }
        }

        $headers = array(
            'Content-Type: application/json','Content-Length: ' . strlen($data_string)
        );
        curl_setopt($ch2,$headers);
        curl_setopt($ch2,true);
        curl_setopt($ch2,1);
        curl_setopt($ch2,$data_string);
        $response = curl_exec($ch2);
        curl_close($ch2);

        $response_a = json_decode($response,true);
        $state = $response_a['state'];
        $message = $response_a['message'];
        if(!empty($response_a)){
            if($state=='completed') {
                return array('response' => $response_a,'success' => '1','message'=>'Data received');
            }else{
                if(empty($message)){
                    $message ='Payment authorization error,Please try again.';
                }
                return array('response' => '','message'=>$message);
             }
        }
       else{
           return array('response' => '','success'=>'0','message'=>'Response nil');
           }
        }
      else
        {
            return array('response' => '','message'=>'Authorization code not available.');
        }
}else{

        return array('response' => '','message'=>'Unauthorize request.');

    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


如何选择合适的 C++ Web 开发框架?
利用 C++ 框架构建高并发 Web 应用的策略
用 C++ 框架开发跨平台图形应用程序
golang框架中安全编码实践的最佳指南是什么?
golang框架与其他语言框架在设计理念上的区别有哪些?
C++ 图形框架与其他语言框架的比较
C++ 框架与其他 Web 开发框架的对比分析
使用 C++ 框架构建大型项目最佳实践
C++ 框架如何提高大型项目开发效率
C++ 框架中依赖注入的持续集成与部署工具
如何与社区协作和贡献到自定义 Golang 框架?
C++ 框架在大型项目中如何实现模块化开发
使用 C++ 框架开发跨平台 Web 应用
C++ 框架在大型项目中的优缺点
golang框架在性能上的优势体现在哪些方面?
C++ 框架在嵌入式系统内存优化中的优势
golang框架在人工智能与机器学习中的作用
如何扩展 Golang 框架以支持特定功能?
如何利用 Go Modules 和依赖项管理来自定义 Golang 框架?
Golang 框架中的性能优化技巧