通过 Paypal 的 API 取消定期付款

Posted

技术标签:

【中文标题】通过 Paypal 的 API 取消定期付款【英文标题】:Cancelling a Recurring payment via Paypal's API 【发布时间】:2012-09-10 06:43:44 【问题描述】:

我有这个代码:

$cancel_payment = UpdateRecurringPaymentsProfile( $paypal_profileid , 'Cancel' );

function UpdateRecurringPaymentsProfile( $user_id, $method  )

    //$user_id is the profile ID returned in the CreateRecurringPaymentsProfile response.
    $nvpstr = "&profileid=".$user_id;
    $nvpstr .= "&PROFILESTATUS=".$method;
    $nvpstr .= "&ACTION=Cancel";
    $resArray = hash_call( "UpdateRecurringPaymentsProfile" , $nvpstr );
    return $resArray;


function hash_call($methodName,$nvpStr)

        //declaring of global variables
        global $API_Endpoint, $version, $API_UserName, $API_Password, $API_Signature;
        global $USE_PROXY, $PROXY_HOST, $PROXY_PORT;
        global $gv_ApiErrorURL;
        global $sBNCode;

        //setting the curl parameters.
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$API_Endpoint);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);

        //turning off the server and peer verification(TrustManager Concept).
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_POST, 1);
        
        //if USE_PROXY constant set to TRUE in Constants.php, then only proxy will be enabled.
       //Set proxy name to PROXY_HOST and port number to PROXY_PORT in constants.php 
        if($USE_PROXY)
            curl_setopt ($ch, CURLOPT_PROXY, $PROXY_HOST. ":" . $PROXY_PORT); 

        //NVPRequest for submitting to server
        $nvpreq="METHOD=" . urlencode($methodName) . "&VERSION=" . urlencode($version) . "&PWD=" . urlencode($API_Password) . "&USER=" . urlencode($API_UserName) . "&SIGNATURE=" . urlencode($API_Signature) . $nvpStr . "&BUTTONSOURCE=" . urlencode($sBNCode);

        // var_dump($nvpreq);
        //setting the nvpreq as POST FIELD to curl
        curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);

        //getting response from server
        $response = curl_exec($ch);

        //convrting NVPResponse to an Associative Array
        $nvpResArray=deformatNVP($response);
        $nvpReqArray=deformatNVP($nvpreq);
        $_SESSION['nvpReqArray']=$nvpReqArray;

        if (curl_errno($ch)) 
        
            // moving to display page to display curl errors
              $_SESSION['curl_error_no']=curl_errno($ch) ;
              $_SESSION['curl_error_msg']=curl_error($ch);

              //Execute the Error handling module to display errors. 
         
        else 
        
             //closing the curl
            curl_close($ch);
        

        return $nvpResArray;
    

我只是向你展示我的代码片段..

此代码不会返回任何错误。问题是当我在我的贝宝仪表板上检查我的“预先批准的付款”时(我正在使用沙盒),我没有看到状态为已取消,它仍然处于活动状态。所以简而言之,我的代码有问题或者有问题。

谁能指导我如何在贝宝中取消定期付款?

【问题讨论】:

什么 print_r(curl_exec($ch));说什么? PROFILEID=I-H9398DRFC1ET&TIMESTAMP=2012-09-18T02:51:23Z&CORRELATIONID=5bce7c679b991&ACK=Success&VERSION=64&BUILD=3587318 我认为您想使用ManageRecurringPaymentsProfileStatus cms.paypal.com/ca/cgi-bin/… 并将操作设置为“取消” @Jrod - 你能把这个添加为答案吗?我几乎没有发现你的答案就离开了这个页面,虽然我很高兴我这样做了,因为它也帮助了我! @ElsonSolano 您是否有机会使用完整的工作代码进行更新? 【参考方案1】:

要取消定期付款,您需要使用ManageRecurringPaymentsProfileStatus API 操作并将操作设置为Cancel

【讨论】:

【参考方案2】:

这是我取消的 PayPal 定期付款代码。

          $ch = curl_init();
          $clientId ='Your Client ID';
          $clientSecret= 'Your Secret ID';
           //you get Clientid and clientSecret  
           https://developer.paypal.com/developer/applications

            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, $clientId.":".$clientSecret);
            curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");

            $result = curl_exec($ch);

            if(empty($result))die("Error: No response.");
            else
            
                $json = json_decode($result);
            
            curl_close($ch);

             $ch = curl_init();

            curl_setopt($ch, CURLOPT_URL, 'https://api.sandbox.paypal.com/v1/billing/subscriptions/'.$value1->transaction_id.'/cancel');
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, "\n  \"reason\": \"Not satisfied with the service\"\n");
            curl_setopt($ch, CURLOPT_POST, 1);

            $headers = array();
            $headers[] = 'Content-Type: application/json';
            $headers[] = 'Authorization: Bearer '.$json->access_token.'';
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

            $result = curl_exec($ch);
            if (curl_errno($ch)) 
                echo 'Error:' . curl_error($ch);
            
            curl_close ($ch);
            $workerpayment=  DB::table('subscriptions')->where('id','=',$value1->id)->update(['subscription_status' => 'cancelled']);
        

对我来说效果很好。

【讨论】:

以上是关于通过 Paypal 的 API 取消定期付款的主要内容,如果未能解决你的问题,请参考以下文章

取消 Paypal 定期付款

通过即时付款通知取消贝宝定期付款

PayPal 定期付款 - 不允许取消配置文件

取消 Paypal 沙盒定期付款

如何处理取消的定期付款

如果用户没有余额,带有初始付款的 Paypal 定期付款会创建并立即取消