-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron_paypal.php
130 lines (104 loc) · 4.17 KB
/
cron_paypal.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
<?php
// you may need to hard code this..
require_once('../config.php');
require_once('fitbitphp.php');
$environment = 'live'; // or 'beta-sandbox' or 'live'
//first we check fitbit for out data...
//we have copied and hard coded our OAuth data here...
$fitbit = new FitBitPHP(
$GLOBALS['FitbitConsumerKey'],
$GLOBALS['FitbitConsumerSecret']
);
$fitbit->setOAuthDetails(
$GLOBALS['FitbitOAuthToken'],
$GLOBALS['FitbitOAuthSecret']
);
$fitbit->setResponseFormat('json');
try{
$recent = json_decode($fitbit->getRecentActivities());
} catch (Exception $e) {
echo "Shit, we could not get to FitBit... lets give you the benifit of the doubt\n";
exit();
}
$steps = $recent['activities']['goals'];
if($steps < $GLOBALS['']){
lets_pay("[email protected]",1.50);
}else{
//perhaps an email or tweet for success...
}
//this is my function that simply sends a payment using the PayPal MassPay API...
function lets_pay($to_paypal,$amount){
// Set request-specific fields.
// these are static from paypals examples...
$emailSubject =urlencode('Made an automatic payment');
$receiverType = urlencode('EmailAddress');
$currency = urlencode('USD'); // or other currency ('GBP', 'EUR', 'JPY', 'CAD', 'AUD')
// Add request-specific fields to the request string.
$nvpStr="&EMAILSUBJECT=$emailSubject&RECEIVERTYPE=$receiverType&CURRENCYCODE=$currency";
$receiverEmail = urlencode($to_paypal);
$amount = urlencode($amount);
$uniqueID = urlencode(uniqid());
$note = urlencode("An Automatic Payment");
$nvpStr .= "&L_EMAIL0=$receiverEmail&L_Amt0=$amount&L_UNIQUEID0=$uniqueID&L_NOTE0=$note";
// Execute the API operation; see the PPHttpPost function above.
$httpParsedResponseAr = PPHttpPost('MassPay', $nvpStr);
if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"])) {
return('MassPay Completed Successfully: '.print_r($httpParsedResponseAr, true));
} else {
exit('MassPay failed: ' . print_r($httpParsedResponseAr, true));
}
}
//this function comes from paypal...
//it has been altered to get credentials from global config values...
//bad design, but what the hell...
/**
* Send HTTP POST Request
*
* @param string The API method name
* @param string The POST Message fields in &name=value pair format
* @return array Parsed HTTP Response body
*/
function PPHttpPost($methodName_, $nvpStr_) {
global $environment;
// Set up your API credentials, PayPal end point, and API version.
$API_UserName = urlencode($GLOBALS['API_UserName']);
$API_Password = urlencode($GLOBALS['API_Password']);
$API_Signature = urlencode($GLOBALS['API_Signature']);
$API_Endpoint = "https://api-3t.paypal.com/nvp";
if("sandbox" === $environment || "beta-sandbox" === $environment) {
$API_Endpoint = "https://api-3t.$environment.paypal.com/nvp";
}
$version = urlencode('51.0');
// Set the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $API_Endpoint);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Turn off the server and peer verification (TrustManager Concept).
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
// Set the API operation, version, and API signature in the request.
$nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$API_Password&USER=$API_UserName&SIGNATURE=$API_Signature$nvpStr_";
// Set the request as a POST FIELD for curl.
curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
// Get response from the server.
$httpResponse = curl_exec($ch);
if(!$httpResponse) {
exit("$methodName_ failed: ".curl_error($ch).'('.curl_errno($ch).')');
}
// Extract the response details.
$httpResponseAr = explode("&", $httpResponse);
$httpParsedResponseAr = array();
foreach ($httpResponseAr as $i => $value) {
$tmpAr = explode("=", $value);
if(sizeof($tmpAr) > 1) {
$httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1];
}
}
if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr)) {
exit("Invalid HTTP Response for POST request($nvpreq) to $API_Endpoint.");
}
return $httpParsedResponseAr;
}
?>