/
instant_payment_notification.php
155 lines (145 loc) · 5.6 KB
/
instant_payment_notification.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
class InstantPaymentNotification extends PaypalIpnAppModel {
/**
* name is the name of the model
*
* @var $name string
* @access public
*/
var $name = 'InstantPaymentNotification';
var $hasMany = array(
'PaypalItem' => array(
'className' => 'PaypalIpn.PaypalItem'
)
);
/**
* the PaypalSource
*/
var $paypal = null;
/**
* verifies POST data given by the paypal instant payment notification
* @param array $data Most likely directly $_POST given by the controller.
* @return boolean true | false depending on if data received is actually valid from paypal and not from some script monkey
*/
function isValid($data){
if(!empty($data)){
App::import(array('type' => 'File', 'name' => 'PaypalIpn.PaypalIpnSource', 'file' => 'models'.DS.'datasources'.DS.'paypal_ipn_source.php'));
$this->paypal = new PaypalIpnSource();
return $this->paypal->isValid($data);
}
return false;
}
/**
* Utility method to send basic emails based on a paypal IPN transaction.
* This method is very basic, if you need something more complicated I suggest
* creating your own method in the afterPaypalNotification function you build
* in the app_controller.php
*
* Example Usage: (InstantPaymentNotification = IPN)
* IPN->id = '4aeca923-4f4c-49ec-a3af-73d3405bef47';
* IPN->email('Thank you for your transaction!');
*
* IPN->email(array(
* 'id' => '4aeca923-4f4c-49ec-a3af-73d3405bef47',
* 'subject' => 'Donation Complete!',
* 'message' => 'Thank you for your donation!',
* 'sendAs' => 'text'
* ));
*
* Hint: use this in your afterPaypalNotification callback in your app_controller.php
* function afterPaypalNotification($txnId){
* ClassRegistry::init('PaypalIpn.InstantPaymentNotification')->email(array(
* 'id' => $txnId,
* 'subject' => 'Thanks!',
* 'message' => 'Thank you for the transaction!'
* ));
* }
*
* Options:
* id: id of instant payment notification to base email off of
* subject: subject of email (default: Thank you for your paypal transaction)
* sendAs: html | text (default: html)
* to: email address to send email to (default: ipn payer_email)
* from: from email address (default: ipn business)
* cc: array of email addresses to carbon copy to (default: array())
* bcc: array of email addresses to blind carbon copy to (default: array())
* layout: layout of email to send (default: default)
* template: template of email to send (default: null)
* log: boolean true | false if you'd like to log the email being sent. (default: true)
* message: actual body of message to be sent (default: null)
*
* @param array $options of the ipn to send
*
*/
function email($options = array()){
if(!is_array($options)){
$message = $options;
$options = array();
$options['message'] = $message;
}
if(isset($options['id'])){
$this-> id = $options['id'];
}
$this->read();
$defaults = array(
'subject' => 'Thank you for your paypal transaction',
'sendAs' => 'html',
'to' => $this->data['InstantPaymentNotification']['payer_email'],
'from' => $this->data['InstantPaymentNotification']['business'],
'cc' => array(),
'bcc' => array(),
'layout' => 'default',
'template' => null,
'log' => true,
'message' => null
);
$options = array_merge($defaults, $options);
//debug($options);
if($options['log']){
$this->log("Emailing: {$options['to']} through the PayPal IPN Plugin. ",'email');
}
App::import('Component','Email');
$Email = new EmailComponent;
$Email->to = $options['to'];
$Email->from = $options['from'];
$Email->bcc = $options['bcc'];
$Email->cc = $options['cc'];
$Email->subject = $options['subject'];
$Email->sendAs = $options['sendAs'];
$Email->template = $options['template'];
$Email->layout = $options['layout'];
//Send the message.
if($options['message']){
$Email->send($options['message']);
}
else{
$Email->send();
}
}
/**
* builds the associative array for paypalitems only if it was a cart upload
*
* @param raw post data sent back from paypal
* @return array of cakePHP friendly association array.
*/
function buildAssociationsFromIPN($post){
$retval = array();
$retval['InstantPaymentNotification'] = $post;
if(isset($post['num_cart_items']) && $post['num_cart_items'] > 0){
$retval['PaypalItem'] = array();
for($i=1;$i<=$post['num_cart_items'];$i++){
$key = $i - 1;
$retval['PaypalItem'][$key]['item_name'] = $post["item_name$i"];
$retval['PaypalItem'][$key]['item_number'] = $post["item_number$i"];
$retval['PaypalItem'][$key]['item_number'] = $post["item_number$i"];
$retval['PaypalItem'][$key]['quantity'] = $post["quantity$i"];
$retval['PaypalItem'][$key]['mc_shipping'] = $post["mc_shipping$i"];
$retval['PaypalItem'][$key]['mc_handling'] = $post["mc_handling$i"];
$retval['PaypalItem'][$key]['mc_gross'] = $post["mc_gross_$i"];
$retval['PaypalItem'][$key]['tax'] = $post["tax$i"];
}
}
return $retval;
}
}
?>