【问题标题】:PayPal IPN listener does not work when adding trial period to a subscription向订阅添加试用期时,PayPal IPN 侦听器不起作用
【发布时间】:2021-06-25 20:09:45
【问题描述】:

我有一个基于订阅的流媒体网站,我正在尝试在向用户收取月费之前实施 3 天的试用期。

须知

  • 在支付页面上实现试用代码之前运行良好(即:向用户收费并自动开始订阅。Paypal IPN 显示响应)
  • 我很确定我需要向侦听器添加变量,但这是我遇到问题的地方。
  • 在下方提供未修改的支付代码、未修改的监听代码、未修改代码的paypal ipn响应、修改的支付代码、使用修改后的支付代码时的ipn响应
  • 为了保护信息,提供的代码中的某些信息已被删除

未修改的工作支付屏幕代码

   <!-- Buy button -->
    <form action="{{link}}" method="post" id="paypal-form-pay">
        <!-- Identify your business so that you can collect the payments -->
        <input type="hidden" name="business" value="{{account}}">
        <!-- Specify a subscriptions button. -->
        <input type="hidden" name="cmd" value="_xclick-subscriptions">
        <!-- Specify details about the subscription that buyers will purchase -->
        <input type="hidden" name="item_name" value="{{subscription.pack}}">
        <input type="hidden" name="item_number" value="{{id}}">
        
        <input type="hidden" name="currency_code" value="{{subscription.currency}}">
        <input type="hidden" name="a3" id="paypalAmt" value="{{subscription.price}}">
        <input type="hidden" name="subscription" id="paypalAmt" value="{{subscription.id}}">
        <input type="hidden" name="p3" id="paypalValid" value="1">
        <input type="hidden" name="t3" value="M">
        
<input type="hidden" name="src" value="100">
<input type="hidden" name="sra" value="5">
        <input type="hidden" name="cancel_return" value="{{ url('wep_subscription_cancel',{"id":subscription.id})}}">
        <input type="hidden" name="return" value="{{ url('wep_subscription_paypal_finish',{"id":subscription.id})}}">
        <input type="hidden" name="notify_url" value="{{ url('wep_subscription_notify')}}">
        <input class="buy-btn" style="display:none" type="submit" value="Buy Subscription">

    </form>

未修改的工作侦听器代码



            $paypalURL = "https://www.paypal.com/cgi-bin/webscr";
            $ch = curl_init($paypalURL);
            if ($ch == FALSE) {
                return FALSE;
            }
            curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
            curl_setopt($ch, CURLOPT_SSLVERSION, 6);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
            curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);

            // Set TCP timeout to 30 seconds
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close', 'User-Agent: company-name'));
            $res = curl_exec($ch);
                
            $tokens = explode("\r\n\r\n", trim($res));
            $res = trim(end($tokens));

            if (strcmp($res, "VERIFIED") == 0 || strcasecmp($res, "VERIFIED") == 0) {
                $txn_id = !empty($request->get('txn_id'))?$request->get('txn_id'):'';
                
                if(!empty($txn_id)){
                    $payment_status = !empty($request->get('payment_status'))?$request->get('payment_status'):'';
                    $currency_code = $request->get('mc_currency');
                    $payment_gross =  !empty($request->get('mc_gross'))?$request->get('mc_gross'):0;
                    $item_number = $request->get('item_number');

                    $subscription = $em->getRepository("AppBundle:Subscription")->findOneBy(array("id"=>$item_number,"method"=>"paypal","status"=>"unpaid"));

                    if (
                        $payment_status == "Completed" and 
                        $currency_code == $subscription->getCurrency() and
                        $payment_gross == $subscription->getPrice()
                    ) {
                        $subscr_id = $request->get('subscr_id');
                        $payer_email = $request->get('payer_email');
                        $payer_id = $request->get('payer_id');
                        $item_name = $request->get('item_name');
                        
                        $subscription->setEmail($payer_email);
                        $subscription->setStatus("paid");
                        $subscription->setTransaction($txn_id);

                        $started =  new \DateTime();
                        $expired =  new \DateTime();
                        $expired->modify('+'.$subscription->getDuration()." day");

                        $subscription->setStarted($started);
                        $subscription->setExpired($expired);

                        $em->flush();
                    }
                }

            }   
            return new Response("done"); 

    }
    public function finishAction(Request $request,$id){
        $em=$this->getDoctrine()->getManager();
        $subscription = $em->getRepository("AppBundle:Subscription")->findOneBy(array("user"=>$this->getUser(),"id"=>$id));
        if ($subscription == null) {
            throw new NotFoundHttpException("Page not found");  
        }
        return $this->render('WebBundle:Subscription:finish.html.twig',array("subscription"=>$subscription));
    }
    public function paypal_finishAction(Request $request,$id){
        $em=$this->getDoctrine()->getManager();
        $subscription = $em->getRepository("AppBundle:Subscription")->findOneBy(array("user"=>$this->getUser(),"id"=>$id));
        if ($subscription == null) {
            throw new NotFoundHttpException("Page not found");  
        }
        return $this->render('WebBundle:Subscription:paypal_finish.html.twig',array("subscription"=>$subscription));
    }

IPM 对有效的未修改代码的响应

mc_gross=0.01&amp;protection_eligibility=Eligible&amp;address_status=confirmed&amp;payer_id=3H4HMXYSVLVWL&amp;address_street=6384 flathead avenue&amp;payment_date=11:26:19 Mar 29, 2021 PDT&amp;payment_status=Completed&amp;charset=windows-1252&amp;address_zip=89122&amp;first_name=Benjamin&amp;mc_fee=0.01&amp;address_country_code=US&amp;address_name=Benjamin Halkum&amp;notify_version=3.9&amp;subscr_id=I-HY6W0PTGL3NB&amp;payer_status=unverified&amp;business=REMOVED.com&amp;address_country=United States&amp;address_city=Las Vegas&amp;verify_sign=ArlJEh2PTclCmA4aNtb3eN2HF8lEAGBRRl4PvyzHc0gTKjP7ykq8080X&amp;payer_email=REMOVED@gmail.com&amp;txn_id=4W280838190693944&amp;payment_type=instant&amp;last_name=Halkum&amp;address_state=NV&amp;receiver_email=paypal@halkum.com&amp;payment_fee=0.01&amp;receiver_id=NFGUHZAMQSLPS&amp;txn_type=subscr_payment&amp;item_name=Test Only&amp;mc_currency=USD&amp;item_number=314&amp;residence_country=US&amp;receipt_id=0577-5054-4256-1714&amp;transaction_subject=Test Only&amp;payment_gross=0.01&amp;ipn_track_id=79a7131ef33e4

修改后的付款代码试用版

<!-- Buy button -->
    <form action="{{link}}" method="post" id="paypal-form-pay">
        <!-- Identify your business so that you can collect the payments -->
        <input type="hidden" name="business" value="{{account}}">
        <!-- Specify a subscriptions button. -->
        <input type="hidden" name="cmd" value="_xclick-subscriptions">
        <!-- Specify details about the subscription that buyers will purchase -->
        <input type="hidden" name="item_name" value="{{subscription.pack}}">
        <input type="hidden" name="item_number" value="{{id}}">
        
                <input type="hidden" name="currency_code" value="{{subscription.currency}}">
        <input type="hidden" name="a1" id="paypalAmt" value="0.00">
        <input type="hidden" name="subscription" id="paypalAmt" value="{{subscription.id}}">
        <input type="hidden" name="p1" id="paypalValid" value="1">
        <input type="hidden" name="t1" value="D">
        
        
        <input type="hidden" name="currency_code" value="{{subscription.currency}}">
        <input type="hidden" name="a3" id="paypalAmt" value="{{subscription.price}}">
        <input type="hidden" name="subscription" id="paypalAmt" value="{{subscription.id}}">
        <input type="hidden" name="p3" id="paypalValid" value="1">
        <input type="hidden" name="t3" value="M">
        
<input type="hidden" name="src" value="100">
<input type="hidden" name="sra" value="5">
        <input type="hidden" name="cancel_return" value="{{ url('wep_subscription_cancel',{"id":subscription.id})}}">
        <input type="hidden" name="return" value="{{ url('wep_subscription_paypal_finish',{"id":subscription.id})}}">
        <input type="hidden" name="notify_url" value="{{ url('wep_subscription_notify')}}">
        <input class="buy-btn" style="display:none" type="submit" value="Buy Subscription">

    </form>

修改后的代码 IPN 响应消息

amount1=0.00&amp;amount3=6.99&amp;address_status=unconfirmed&amp;subscr_date=11:39:54 Mar 29, 2021 PDT&amp;payer_id=3H4HMXYSVLVWL&amp;address_street=REMOVED&amp;mc_amount1=0.00&amp;mc_amount3=6.99&amp;charset=windows-1252&amp;address_zip=89122&amp;first_name=Benjamin&amp;reattempt=1&amp;address_country_code=US&amp;address_name=Benjamin Halkum&amp;notify_version=3.9&amp;subscr_id=I-G1VY47ASJ8C4&amp;payer_status=unverified&amp;business=REMOVED.com&amp;address_country=United States&amp;address_city=Las Vegas&amp;verify_sign=AhM9chhyQTrOGTRyOPkwcY26Rcv3AhiXC3kA9XVfl3desynG0cKTMHw4&amp;payer_email=contact@indystars.co&amp;last_name=Halkum&amp;address_state=NV&amp;receiver_email=REMOVED.com&amp;recurring=1&amp;txn_type=subscr_signup&amp;item_name=Monthly*&amp;mc_currency=USD&amp;item_number=318&amp;residence_country=US&amp;period1=1 D&amp;period3=1 M&amp;ipn_track_id=bfcdb7a2bc514

【问题讨论】:

  • 普雷斯顿在看什么方面给了我一个很好的起点。为了重新编写侦听器以说明试用期,是否有人有任何好的起点或指向处理此问题的示例的链接?我搜索了 PayPal 开发者文档。

标签: paypal


【解决方案1】:

“修改后的代码 IPN 响应消息”中的示例似乎符合预期,因此这是在试用期更新侦听器以处理不同负载的问题。除此之外,还没有交易,因此没有txn_id,所以你需要为新情况编写代码。

【讨论】:

  • 欣赏它@Preston。我是新手,你能解释一下为什么当我添加试用期时,它不再输出 txn_id 吗?我认为,它在大多数情况下会保持不变,但 mc_gross 会将这两个金额包括在一起。
  • 在您的试用期内,初始金额为 0.00 ,因此直到 3 天后才有 PayPal 交易......
  • 这很有意义。不幸的是,我还没有解决它在付款更新时自动更新的问题,所以这又是一场斗争。我从你的帖子中想到的是,如果我将我的试用设置为第一周 0.99 美元,然后 X 金额,理论上应该可以工作,直到我能弄清楚如何最好地重写听众
  • 作为更新,我的理论是正确的。我现在至少可以提供折扣试用。
  • 请记住,有一个更新版本的 PayPal 订阅:developer.paypal.com/docs/subscriptions -- 它使用更好的 JS 按钮,旨在与 Webhooks 而不是 IPN 一起用于事件更新
猜你喜欢
  • 2020-12-18
  • 2019-12-29
  • 1970-01-01
  • 2012-10-09
  • 2012-05-20
  • 2017-01-30
  • 2013-08-07
  • 2016-10-11
  • 2010-12-28
相关资源
最近更新 更多