webtechnick/cakephp-paypal-ipn-plugin

CakePHP Paypal 即时支付通知插件

dev-master / 2.x-dev 2019-02-02 07:09 UTC

This package is not auto-updated.

Last update: 2024-09-23 16:21:42 UTC


README

获取

需求

CakePHP 2.x

注意:CakePHP 1.3 使用 cakephp1.3 分支

更多来自 WebTechNick

http://github.com/webtechnick

变更日志

  • 1.0: 初始发布
  • 1.1: 添加更简洁的路由
  • 2.0: 添加了助手函数
  • 2.1: 添加了 cake schema 安装脚本
  • 2.2: 添加了 paypal 取消订阅类型
  • 2.2.1: 修复了订阅问题的错误
  • 2.2.2: 修复了在严格 doctype 中的 paypal 按钮验证问题
  • 3.0: 添加了新的基本 Paypal IPN 邮件功能。
  • 3.5 添加了多项目 paypal 按钮的结账功能。以下为文档
  • 3.5.1: 将列选项 option_name_1 和 option_name_2 分别重命名为 option_name1 和 option_name2
  • 3.5.2: 更新到 CakePHP 1.3 的最新约定,不再需要 Auth,所有购物车项目都将可在 paypal_items 表中查看
  • 3.6.0: 添加了查看购物车按钮选项
  • 4.0.0: CakePHP 2.x 就绪。

安装

  1. 将插件复制到您的 app/Plugin/PaypalIpn 目录

  2. bootstrap.php 中加载插件

     CakePlugin::load('PaypalIpn');
    
  3. 运行模式以创建所需的表。

     $ cake schema create --plugin PaypalIpn
    
  4. 将以下内容添加到您的 /app/Config/Routes.php 文件中(可选)

     /* Paypal IPN plugin */
     Router::connect('/paypal_ipn/process', array('plugin' => 'paypal_ipn', 'controller' => 'instant_payment_notifications', 'action' => 'process'));
     /* Optional Route, but nice for administration */
     Router::connect('/paypal_ipn/:action/*', array('admin' => 'true', 'plugin' => 'paypal_ipn', 'controller' => 'instant_payment_notifications', 'action' => 'index'));
     /* End Paypal IPN plugin */
    

Paypal 设置

  1. 建议您在 https://developer.paypal.com 开始一个沙盒账户
  2. 在您的账户中启用 IPN。

管理:可选)如果您想使用内置的管理员访问 IPN

  1. 请确保您通过 Auth 组件以管理员身份登录。
  2. 导航到 www.yoursite.com/paypal_ipn

Paypal 按钮助手:可选)如果您计划使用 paypal 助手来创建 PayNow 或 Subscribe 按钮

  1. Config/paypal_ipn_config.php 中更新您的 paypal 信息

  2. PaypalIpn.Paypal 添加到您的助手列表中的 AppController.php

     public $helpers = array('Html','Form','PaypalIpn.Paypal');
    

用法:(查看实际 Plugin/PaypalIpn/View/Helper/PaypalHelper.php 以获取更多信息)

	$this->Paypal->button(String tittle, Options array); 
 
	$this->Paypal->button('Pay Now', array('amount' => '12.00', 'item_name' => 'test item'));
	$this->Paypal->button('Subscribe', array('type' => 'subscribe', 'amount' => '60.00', 'term' => 'month', 'period' => '2'));
	$this->Paypal->button('Donate', array('type' => 'donate', 'amount' => '60.00'));
	$this->Paypal->button('Add To Cart', array('type' => 'addtocart', 'amount' => '15.00'));
	$this->Paypal->button('View Cart', array('type' => 'viewcart', 'amount' => '15.00'));
	$this->Paypal->button('Unsubscribe', array('type' => 'unsubscribe'));
	$this->Paypal->button('Checkout', array(
		'type' => 'cart',
		'items' => array(
			array('item_name' => 'Item 1', 'amount' => '120', 'quantity' => 2, 'item_number' => '1234'),
			array('item_name' => 'Item 2', 'amount' => '50'),
			array('item_name' => 'Item 3', 'amount' => '80', 'quantity' => 3),
		)
	));
	
	//Test Example
	$this->Paypal->button('Pay Now', array('test' => true, 'amount' => '12.00', 'item_name' => 'test item'));

或者使用 Paypal 助手

除了 Paypal 助手之外,您还可以使用您自定义的按钮,但请确保将 notify_url 设置为您的配置路由。

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
	...
	<input type="hidden" name="notify_url" value="http://www.yoursite.com/paypal_ipn/process" />
	...
</form>

通常建议使用 paypal 助手,因为它将根据您的配置为您生成一切

Paypal 通知回调

在您的 app/Controller/AppController.php 中创建一个函数,如下所示

function afterPaypalNotification($txnId){
	//Here is where you can implement code to apply the transaction to your app.
	//for example, you could now mark an order as paid, a subscription, or give the user premium access.
	//retrieve the transaction using the txnId passed and apply whatever logic your site needs.
	
	$transaction = ClassRegistry::init('PaypalIpn.InstantPaymentNotification')->findById($txnId);
	$this->log($transaction['InstantPaymentNotification']['id'], 'paypal');
	
	//Tip: be sure to check the payment_status is complete because failure 
	//     are also saved to your database for review.
	
	if ($transaction['InstantPaymentNotification']['payment_status'] == 'Completed') {
		//Yay!  We have monies!
	}	else {
		//Oh no, better look at this transaction to determine what to do; like email a decline letter.
	}
} 

基本电子邮件功能

基于 paypal IPN 交易发送基本电子邮件的实用方法。此方法非常基础,如果您需要更复杂的功能,我建议在您在 app_controller.php 中构建的 afterPaypalNotification 函数中创建自己的方法

$IPN = ClassRegistry::init('PaypalIpn.InstantPaymentNotification');
$IPN->id = '4aeca923-4f4c-49ec-a3af-73d3405bef47';
$IPN->email('Thank you for your transaction!');

//OR passed in as an array of options
$IPN->email(array(
	'id' => '4aeca923-4f4c-49ec-a3af-73d3405bef47',
	'subject' => 'Donation Complete!',
	'message' => 'Thank you for your donation!',
	'sendAs' => 'text'
));

提示:在您的 AppController.php 中的 afterPaypalNotification 回调中使用此方法

function afterPaypalNotification($txnId){
	ClassRegistry::init('PaypalIpn.InstantPaymentNotification')->email(array(
		'id' => $txnId,
		'subject' => 'Thanks!',
		'message' => 'Thank you for the transaction!'
	));
}

电子邮件选项

  • id:基于即时支付通知的电子邮件 id
  • subject:电子邮件主题(默认:感谢您的 paypal 交易)
  • sendAs:html | text(默认:html)
  • to:发送电子邮件的电子邮件地址(默认:ipn payer_email)
  • from:发送电子邮件的电子邮件地址(默认:ipn business)
  • cc: 要抄送(carbon copy)的电子邮件地址数组(默认:数组)
  • bcc: 要秘密抄送(blind carbon copy)的电子邮件地址数组(默认:数组)
  • layout: 要发送的电子邮件的布局(默认:默认)
  • template: 要发送的电子邮件的模板(默认:null)
  • log: 布尔值 true | false,表示是否记录发送的电子邮件。默认:true
  • message: 要发送的消息的实际正文(默认:null)