与PayPal IPN一样,创建了使用PHP和Curl的响应听力服务



我是PHP Web服务的新手,我以前曾问过这个问题,但无法获得有用的解决方案,因此我试图重塑它。我正在尝试收听API的响应,并像PayPal IPN服务所做的那样更新数据库。当您在浏览器中运行它时,我到目前为止的脚本运行良好,并且我是通过帖子获得我的A JSON响应的脚本,但是API后来发送了另一个响应,而无需浏览/客户端的参与,我希望它可以交流到服务器中的PHP脚本并更新数据库。API具有一个通知字段,它需要它发送这些通知,并且我将其设置为发送请求的同一文件。以下是我到目前为止的代码:

    if($ch !== false){
            curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            //Attach our encoded JSON string to the POST fields.
            curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            //Set the content type to application/json
           // curl_setopt($ch, CURLOPT_HEADER, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
       'Content-Type: application/json;odata=verbose',
       'Content-length:' . strlen($jsonDataEncoded))
     );
    }

    //Execute the request
    $result = curl_exec($ch);
    if ($result === false){
      curl_close($ch);
      echo "Error".curl_errno($ch). "n";
    }else {
      curl_close($ch);
      $decoded = json_decode($result, true);
      // echo '<pre>';
      // var_dump($decoded["paymentAmount"]["totalAmountCharged"]);
      // die;
      $data['unique_id'] = $decoded["clientCorrelator"];
      $data['subscriber_number'] = $decoded["endUserId"];
      $data['payment_status'] = $decoded["transactionOperationStatus"];
      $data['payment_gross'] = $decoded["paymentAmount"]["totalAmountCharged"];
      $data['txn_id'] = $decoded["ecocashReference"];
      $this->payments->insertTransaction($data);
      die;

我需要在脚本中添加什么才能使用卷曲和PHP来实现我的目标。API返回下面的JSON响应,我正在尝试聆听它并将某些字段更新到数据库中。

  {"id":71109,"version":0,"clientCorrelator":"58ada3aec8615","endTime":null,"startTime":1487774641697,"notifyUrl":"http://website/ecocash_send/transaction","referenceCode":"17589","endUserId":"774705932","serverReferenceCode":"2202201716440169792864","transactionOperationStatus":"PENDING SUBSCRIBER VALIDATION","paymentAmount":{"id":71110,"version":0,"charginginformation":{"id":71112,"version":0,"amount":1,"currency":"USD","description":" Bulk Sms Online payment"},"chargeMetaData":{"id":71111,"version":0,"channel":"WEB","purchaseCategoryCode":"Online Payment","onBeHalfOf":"PAMONMEFT","serviceId":null},"totalAmountCharged":null},"ecocashReference":"MP170222.1644.A00059","merchantCode":"0000","merchantPin":"000","merchantNumber":"771999313","notificationFormat":null,"serviceId":null,"originalServerReferenceCode":null}"

响应PayPal IPN的脚本不需要任何特别的脚本。

它需要做的就是在那里等待其他API启动它,知道要期望什么参数,具有强大的错误报告机制,即在某个地方保存错误,您可以在某个地方进行查看,或将其发送电子邮件至电子邮件帐户您会定期查看。

除了通常的数据库访问代码之外,这是必要的。

当然,您无法在此过程中没有浏览器。

相关内容

  • 没有找到相关文章

最新更新