我将 PayPal 集成到 Codeigniter 4 中,但在更新数据库的交易后我无法获取数据
我想做的是从 IPN 侦听器获取 PayPal 响应,以便我可以相应地修改我的数据库,但无论我做什么,它都不起作用。我已经在我的 PayPal Sandbox 帐户中完成了以下操作:
我无法获取数据库中的数据,我正在发送数据
function index()
{
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
$keyval = explode('=', $keyval);
if (count($keyval) == 2)
$myPost[$keyval[0]] = urldecode($keyval[1]);
}
$req = 'cmd=_notify-validate';
if (function_exists('get_magic_quotes_gpc')) {
$get_magic_quotes_exists = true;
}
foreach ($myPost as $key => $value) {
if ($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
$value = urlencode(stripslashes($value));
} else {
$value = urlencode($value);
}
$req .= "&$key=$value";
}
if (sandbox == true) {
$paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
} else {
$paypal_url = "https://www.paypal.com/cgi-bin/webscr";
}
$ch = curl_init($paypal_url);
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_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
if (DEBUG == true) {
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
}
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
$res = curl_exec($ch);
if (curl_errno($ch) != 0) // cURL error
{
if (DEBUG == true) {
error_log(date('[Y-m-d H:i e] ') . "Can't connect to PayPal to validate IPN message: " . curl_error($ch) . PHP_EOL, 3, LOG_FILE);
}
curl_close($ch);
exit;
} else {
// Log the entire HTTP response if debug is switched on.
if (DEBUG == true) {
error_log(date('[Y-m-d H:i e] ') . "HTTP request of validation request:" . curl_getinfo($ch, CURLINFO_HEADER_OUT) . " for IPN payload: $req" . PHP_EOL, 3, LOG_FILE);
error_log(date('[Y-m-d H:i e] ') . "HTTP response of validation request: $res" . PHP_EOL, 3, LOG_FILE);
}
curl_close($ch);
}
// Inspect IPN validation result and act accordingly
// Split response headers and payload, a better way for strcmp
$tokens = explode("\r\n\r\n", trim($res));
$res = trim(end($tokens));
if (strcmp($res, "VERIFIED") == 0) {
// assign posted variables to local variables
$paypalInfo = $this->input->post();
$aData = array();
$item_name = $_POST['item_name'];
$data['product_id'] = $paypalInfo["item_number"];
$data['txn_id'] = $paypalInfo["txn_id"];
$data['payment_gross'] = $paypalInfo["mc_gross"];
$data['currency_code'] = $paypalInfo["mc_currency"];
$data['payer_email'] = $paypalInfo["payer_email"];
$data['payment_status'] = $paypalInfo["payment_status"];
// check whether the payment_status is Completed
$isPaymentCompleted = false;
if ($paypalInfo["payment_status"] == "Completed") {
$isPaymentCompleted = true;
}
if ($isPaymentCompleted) {
$oPaypalBuilder = $this->modelHelper->getBuilder('Paypal');
$oPaypalBuilder->skipValidation();
$oPaypalBuilder->insert($data);
}
// process payment and mark item as paid.
if (DEBUG == true) {
error_log(date('[Y-m-d H:i e] ') . "Verified IPN: $req " . PHP_EOL, 3, LOG_FILE);
}
} else if (strcmp($res, "INVALID") == 0) {
// log for manual investigation
// Add business logic here which deals with invalid IPN messages
if (DEBUG == true) {
error_log(date('[Y-m-d H:i e] ') . "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE);
}
}
}
我无法获取数据库中的数据,我正在发送数据
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr"
method="post" class="d-none" id="paypal" target="_top">
<input type='hidden' name='business'
value=''> <input type='hidden'
name='item_name' value=''> <input type='hidden'
name='item_number' value=''> <input type='hidden'
name='amount' value=''> <input type='hidden'
name='no_shipping' value=''> <input type='hidden'
name='currency_code' value=''> <input type='hidden'
name='notify_url'
value=''>
<input type='hidden' name='cancel_return'
value=''>
<input type='hidden' name='return'
value=''>
<input type="hidden" name="cmd" value="_xclick">
</form>
自动返回 URL 的重定向也无法正常工作,交易后,用户无法返回商店,直到单击返回按钮。
这始终是您使用的纯 HTML(无 API)集成的问题,永远无法保证一定会发生退货。 PayPal 可能有法律义务向付款人出示收据,因此他们可能不会点击返回,或者他们的浏览器可能会在返回之前崩溃。
为了确保返回并确保您的服务器收到已完成交易的通知,您应该切换到适当的基于 API 的服务器端集成。
在您的服务器上设置两条路由,一条用于“创建订单”,一条用于“捕获订单”,记录如下:https://developer.paypal.com/docs/business/checkout/server-side-api-来电/。成功后,“捕获订单”路线应更新您的数据库。
与两条路线配对的最佳审批流程是:https://developer.paypal.com/demo/checkout/#/pattern/server