PayPal 自动发码、短信提示、提示用户购买成功的实战总结,写给新人

Posted by Admin L in Shareware on 19-07-2011.

作者:牧山道人
原文地址:https://www.seeksunslowly.com/paypal-auto-register-code-sms-prompt-sc
转载请注明出处,谢谢。
_____________________________________

缩略词参考
PP:PayPal。
PDT:Payment Data Transfer(付款数据传输)。
IPN:Instant Payment Notification (即时付款通知)。

背景
之前的岁月里,为了简单,只用了 PDT,也实现了标题所述功能。

但最近,有 3 个用户没能收到码,我也没收到短信。
大致原因是:现在(以前没试过,不知道。但之前没遇到过,说明以前应该不是或不完全是这样)用户购买成功后,PP 会给个自动跳转页面(有延迟),如果用户等不急或不知道,可能没跳转就关掉了,此时,PDT 便没调起来,所以对应脚本里的功能一样都没实现。

问题很严重,必须解决。

铜锭私通后,狠了心,今上午安排专项时间思考且找到了完美解决方法——PDT 与 IPN 结合,并编码实现。

结果
很好,超过预期(预计要调个三二回)。一次搞定,没有任何修改。

分享(粗)
1、自动发码用 IPN,IPN 是后台处理,你指定的 URL 不会返回给用户,所以不要在对应脚本中输出任何信息。
2、给自己发短信,也写在 IPN 对应脚本中。
3、给购买者提示,写在 PDT 对应脚本/页面中。
4、假如你有多个产品在同一 PP 帐户中卖,如果将注册机写到一个脚本里(需要在 PP 帐户选项里作全局设置),代码中再根据商品名称作 switch(),比较繁琐,且也不易维护、不安全。下面的细节中会告诉你如何为指定产品设置特定 URL。

分享(细)
1、IPN 自动发码 PHP 脚本:
为了方便大家使用,我把注释甚至销售邮件都分享出来了。
[cc lang=”php”]
$value) {
$value = urlencode(stripslashes($value));
$req .= “&$key=$value”;
}

// Post back to PayPal system to validate.
$header .= “POST /cgi-bin/webscr HTTP/1.0\r\n”;
$header .= “Content-Type: application/x-www-form-urlencoded\r\n”;
$header .= “Content-Length: ” . strlen($req) . “\r\n\r\n”;
$fp = fsockopen (‘ssl://www.paypal.com’, 443, $errno, $errstr, 30);

// Assign posted variables to local variables.
$item_name = $_POST[‘item_name’];
$payment_status = $_POST[‘payment_status’];
$receiver_email = strtolower($_POST[‘receiver_email’]);
$payer_email = $_POST[‘payer_email’];

if (!$fp) {
// HTTP ERROR
}

else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, “VERIFIED”) == 0) {
// check the payment_status is Completed.
if(strcmp($payment_status, “Completed”) == 0) {
// check that receiver_email is your Primary PayPal email
if(strcmp($receiver_email, “[email protected]”) == 0) {

// 生成 license key.
$licenseKey = rm($item_name);

//// 给用户发 key.
$subject = “Your license key of $item_name – from XXXX.”;
$sign = “Please let us know if you have any questions about this.\r\n” .
“Thanks and best regards!\r\n\r\n” .
“——————————-\r\n” .
“XXXX, YYYY Sales.\r\n” .
“ZZZZZZ Team\r\n” .
“http://www.ZZZZZ.com\r\n\r\n” .
“ZZZZZZZZ.\r\n” .
“http://www.ZZZZZZZZ.com\r\n” .
“——————————-“;
$content = “Dear user:\r\n\r\n” .
“Thank you for choosing our product – $item_name.\r\n\r\n” .
“Your license key of $item_name is:\r\n\r\n$licenseKey\r\n\r\n” .
“Please use it to register your copy and keep it well.\r\n\r\n” .
“Additional:\r\n” .
“1. If your copy is not the latest version, please download it at\r\n” .
“http://www.XXXXX.com/download.htm\r\n” .
“and reinstall it first.\r\n” .
“2. If you lost the license key, please click ‘Retrieve License Key’ menu item to get it again.\r\n\r\n$Sign”;
mail($payer_email, $subject, $content, “From: [email protected]”);
// 抄送一份给自己。
mail(“[email protected]”, $subject, $content, “From: [email protected]”);
////

// 短信提醒(139 邮箱可免费发送邮件提醒到对应手机)。
mail(“[email protected]”, “Congratulations, got PayPal order!”, $item_name, “From: [email protected]”);
}
}
}
else if (strcmp ($res, “INVALID”) == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
?>
[/cc]
2、给自己发短信,参考上述代码以下内容:
[cc lang=”php”]
// 短信提醒(139 邮箱可免费发送邮件提醒到对应手机)。
mail(“[email protected]”, “Congratulations, got PayPal order!”, $item_name, “From: [email protected]”);
[/cc]
3、给购买者提示,写在 PDT 对应脚本/页面中。
非常简单,一个静态页面即可,这是我的,供参考(这页面没有什么 SEO 功能,越简单越好):
[cc lang=”html”]

Thank you for choosing XXXX’s product – YYYYYYY.

Your transaction has been completed, and you will receive the LICENSE KEY INSTANTLY by email.

You may log into your account at www.paypal.com to view details of this transaction.

You can also visit XXXX’s official site at www.XXXXXX.com to know more products and services.

[/cc]
4、PDT、IPN 的 URL 的全局设定(每个产品都会去调),分别在“Website Payment Preferences(网站付款习惯设定)”和“Instant payment Notification Preferences(及时付 款通知习惯设定)”里,操作很简单,相信都没问题,这里就不讲了;若还是有问题,请联系一下 PP 支持。
这里主要说明一下“为指定产品设置特定 URL”的方法(注意:特定 URL 会覆盖全局 URL):
编辑你的按钮,点“第 3 步:自定义高级功能(可选)”,拖到最后,勾选(其实会自动勾选)“添加高级变量”,在其下文本框中输入:
notify_url=http://www.XXX.com/YYYYrm.php
return=http://www.XXX.com/ppreturn.htm
亦可参见下图:
PayPal 中为单个产品设置特定 IPN 和 PDT。
其中,第一行就是上面给你的 IPN 代码 URL;第二行就是第 3 点提到的 PDT URL。

效果预览
用户收到的含注册码的许可证邮件
用户收到的含注册码的许可证邮件
购买成功后返回给用户的提示页面
购买成功后返回给用户的提示页面

__________________________________________________________________________________
分享到此结束,很简单吧!引用一句成都华德福学校校歌歌词收尾:每当我发现需要做的事儿,如果我去做,就实现。

【赞赏 / Reward】

微信         支付宝         PayPal

Post a comment