【问题标题】:How to verify payment in php如何在php中验证付款
【发布时间】:2021-11-13 06:21:26
【问题描述】:

我正在制作一个网站,候选人可以在该网站上申请比赛。 但是,候选人只有在支付了注册费的情况下才能申请。所以,当用户点击网站上的APPLY按钮时,他会被引导到PayStack网站的支付表单,当他进行支付时,他会被重定向到注册页面。 所以,我的问题是,我如何确认用户是否已付款,是否有资格申请。 在这方面需要帮助。

我使用 PHP 作为后端

【问题讨论】:

  • 能否将您尝试的简单代码显示为?
  • 代码的哪一部分?
  • 其实我还没有写后端。我将编写代码来注册候选人。我只想这样,候选人必须支付费用才能注册。因此,点击注册按钮的人会被重定向到 Paystack PAYMENT 表单以进行付款。当他完成付款后,他将被重定向到注册页面进行申请。我只想知道如何进行此付款流程。
  • 您应该阅读您的支付网关文档并查看他们如何返回您,您可以通过他们的回复进行验证。一些支付网关允许使用支付 ID 查询支付详细信息,因此您可以使用此类端点来确定用户是否支付了金额。
  • 您好,感谢您的回答。我从来没有做过这些付款,你能帮我吗?

标签: php mysql web paypal payment


【解决方案1】:

对于PayStack,您应该包含官方的接受支付javascript,它将在支付完成时触发一个事件(无论支付是否成功,都会触发此触发器)

注意:我将首先使用系统生成的“参考”(例如[日期]-[随机字符串])在数据库中插入一条付款记录(包括姓名、电子邮件、金额),然后使用以下 HTML接受付款:

接受付款的 HTML

 <form id="paymentForm">
  <div class="form-group">
    <label for="email">Email Address</label>
    <input type="email" id="email-address" required />
  </div>
  <div class="form-group">
    <label for="amount">Amount</label>
    <input type="tel" id="amount" required />
  </div>
  <div class="form-group">
    <label for="first-name">First Name</label>
    <input type="text" id="first-name" />
  </div>
  <div class="form-group">
    <label for="last-name">Last Name</label>
    <input type="text" id="last-name" />
  </div>
  <div class="form-submit">
    <button type="submit" onclick="payWithPaystack()"> Pay </button>
  </div>
</form>
<script src="https://js.paystack.co/v1/inline.js"></script> 

js是这样的:

const paymentForm = document.getElementById('paymentForm');
paymentForm.addEventListener("submit", payWithPaystack, false);
function payWithPaystack(e) {
  e.preventDefault();
  let handler = PaystackPop.setup({
    key: 'pk_test_xxxxxxxxxx', // Replace with your public key
    email: document.getElementById("email-address").value,
    amount: document.getElementById("amount").value * 100,
    ref: ''+Math.floor((Math.random() * 1000000000) + 1), // generates a pseudo-unique reference. Please replace with a reference you generated. Or remove the line entirely so our API will generate one for you
    // label: "Optional string that replaces customer email"
    onClose: function(){
      alert('Window closed.');
    },
    callback: function(response){
      let message = 'Payment complete! Reference: ' + response.reference;
      alert(message);
    }
  });
  handler.openIframe();
}

请确保上面的 ref 使用的是之前生成的 [date]-[random string]。

注意:支付步骤完成后,会触发回调事件。

当然,您不会希望在回调中“提醒消息”,您应该调用(例如通过 ajax)触发 CURL 以“验证/检查”付款状态的 PHP。一个典型的 CRUL 是这样的:

curl https://api.paystack.co/transaction/verify/:reference
-H "Authorization: Bearer YOUR_SECRET_KEY"
-X GET

如果你在PHP中实现curl,会是这样的:

// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.paystack.co/transaction/verify/:reference');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');


$headers = array();
$headers[] = 'Authorization: Bearer YOUR_SECRET_KEY';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

现在上述 CURL 返回的 $result 将类似于以下之一:

案例 1:成功

{
  "status": true,
  "message": "Verification successful",
  "data": {
    "amount": 27000,
    "currency": "NGN",
    "transaction_date": "2016-10-01T11:03:09.000Z",
    "status": "success",
    "reference": "DG4uishudoq90LD",
    "domain": "test",
    "metadata": 0,
    "gateway_response": "Successful",
    "message": null,
    "channel": "card",
    "ip_address": "41.1.25.1",
    "log": {
      "time_spent": 9,
      "attempts": 1,
      "authentication": null,
      "errors": 0,
      "success": true,
      "mobile": false,
      "input": [],
      "channel": null,
      "history": [{
        "type": "input",
        "message": "Filled these fields: card number, card expiry, card cvv",
        "time": 7
        },
        {
          "type": "action",
          "message": "Attempted to pay",
          "time": 7
        },
        {
          "type": "success",
          "message": "Successfully paid",
          "time": 8
        },
        {
          "type": "close",
          "message": "Page closed",
          "time": 9
        }
      ]
    }
    "fees": null,
    "authorization": {
      "authorization_code": "AUTH_8dfhjjdt",
      "card_type": "visa",
      "last4": "1381",
      "exp_month": "08",
      "exp_year": "2018",
      "bin": "412345",
      "bank": "TEST BANK",
      "channel": "card",
      "signature": "SIG_idyuhgd87dUYSHO92D",
      "reusable": true,
      "country_code": "NG",
      "account_name": "BoJack Horseman"
    },
    "customer": {
      "id": 84312,
      "customer_code": "CUS_hdhye17yj8qd2tx",
      "first_name": "BoJack",
      "last_name": "Horseman",
      "email": "bojack@horseman.com"
    },
    "plan": "PLN_0as2m9n02cl0kp6",
    "requested_amount": 1500000
  }
}

案例2:支付失败

{  
  "status":true,
  "message":"Verification successful",
  "data": {  
    "amount":27000,
    "currency":"NGN",
    "transaction_date":"2016-10-01T11:03:09.000Z",
    "status":"failed",
    "reference":"djfoidjkdkj41",
    "domain":"test",
    "metadata":0,
    "gateway_response":"Insufficient Funds",
    "message":null,
    "channel":"card",
    "ip_address":"41.1.25.1",
    "log": {  
      "time_spent":9,
      "attempts":1,
      "authentication":null,
      "errors":0,
      "success":true,
      "mobile":false,
      "input":[ ],
      "channel":null,
      "history": [  
         {  
           "type":"input",
           "message":"Filled these fields: card number, card expiry, card cvv",
           "time":7
         },
         {  
           "type":"action",
           "message":"Attempted to pay",
           "time":7
         },
         {  
           "type":"close",
           "message":"Page closed",
           "time":9
         }
      ]
    },
    "fees":null,
    "authorization":{  
      "authorization_code":"AUTH_8dfhjjdt",
      "card_type":"visa",
      "last4":"1381",
      "exp_month":"08",
      "exp_year":"2018",
      "bin":"412345",
      "bank":"TEST BANK",
      "channel":"card",
      "signature": "SIG_idyuhgd87dUYSHO92D",
      "reusable":true,
      "country_code":"NG",
      "account_name": "BoJack Horseman"
    },
    "customer":{  
      "id":84312,
      "customer_code":"CUS_hdhye17yj8qd2tx",
      "first_name":"BoJack",
      "last_name":"Horseman",
      "email":"bojack@horseman.com"
    },
    "plan":""
  }
}

案例 3:无效

{  
  "status":false,
  "message":"Invalid key"
}

因此,您应该依靠 CURL 返回的状态,根据事务“参考”更新您的数据库(例如,如果数据状态为“成功”,则将您的数据库表的事务记录更新为“PAID”)。

最后,如果数据状态为“成功”,请将用户页面重定向到您的比赛页面。

【讨论】:

  • 非常感谢您的回答。但是,我已经向用户提供了 PayStack 自己制作的表单的链接。我没有自己做。这是该表单的链接,paystack.com/pay/missuniversalafrica 在这里,用户将付款。我希望当用户付款时,他应该被重定向到由我制作的注册表单,并进行进一步的处理。
【解决方案2】:

您可以设置一个您希望客户在付款页面上完成付款后被重定向到的网址。您是否尝试在“付款后重定向”文本字段中设置值?

https://support.paystack.com/hc/en-us/articles/360009881560-How-do-I-collect-one-time-payments-with-Payment-Pages-

【讨论】:

    猜你喜欢
    • 2014-01-14
    • 2020-03-14
    • 1970-01-01
    • 2011-11-17
    • 2021-05-15
    • 2017-11-18
    • 2012-04-10
    • 1970-01-01
    • 2023-04-10
    相关资源
    最近更新 更多