forked from dualcube/moodle-enrol_authorizedotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpay_process.php
152 lines (121 loc) · 6.09 KB
/
pay_process.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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Listens for Instant Payment Notification from Authorize.net
*
* This script waits for Payment notification from Authorize.net,
* then it sets up the enrolment for that user.
*
* @package enrol_authorizedotnet
* @copyright 2015 Dualcube, Moumita Ray, Parthajeet Chakraborty
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// Disable moodle specific debug messages and any errors in output,
// comment out when debugging or better look into error log!
define('NO_DEBUG_DISPLAY', true);
require("../../config.php");
require_once("lib.php");
//require_once($CFG->libdir.'/eventslib.php');
require_once($CFG->libdir.'/enrollib.php');
require_once($CFG->libdir . '/filelib.php');
//echo "<pre>"; print_r($CFG); die;
//require_once($CFG->dirroot . '/admin/environment.php');
require_login();
global $DB, $CFG;
if (empty($_POST) or !empty($_GET)) {
print_error("Sorry, you can not use the script that way."); die;
}
$PAGE->set_pagelayout('admin');
$PAGE->set_url($CFG->wwwroot.'/enrol/authorizedotnet/pay_process.php');
echo $OUTPUT->header();
echo $OUTPUT->heading("Your Payment is in Process....");
echo $OUTPUT->heading("Don't Reload or Leave This Page. This Page Will Automatically Redirect You To The Course Page. ");
echo $OUTPUT->footer();
$transRequestXmlStr=<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<createTransactionRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
<merchantAuthentication></merchantAuthentication>
<transactionRequest>
<transactionType>authCaptureTransaction</transactionType>
<amount>assignAMOUNT</amount>
<currencyCode>assignCURRENCY</currencyCode>
<payment>
<opaqueData>
<dataDescriptor>assignDD</dataDescriptor>
<dataValue>assignDV</dataValue>
</opaqueData>
</payment>
</transactionRequest>
</createTransactionRequest>
XML;
$transRequestXml=new SimpleXMLElement($transRequestXmlStr);
$loginId = $_POST['loginkey'];
$transactionKey = $_POST['transactionkey'];
$transRequestXml->merchantAuthentication->addChild('name',$loginId);
$transRequestXml->merchantAuthentication->addChild('transactionKey',$transactionKey);
$transRequestXml->transactionRequest->amount=$_POST['amount'];
$transRequestXml->transactionRequest->currencyCode=$_POST['x_currency_code'];
$transRequestXml->transactionRequest->payment->opaqueData->dataDescriptor=$_POST['dataDesc'];
$transRequestXml->transactionRequest->payment->opaqueData->dataValue=$_POST['dataValue'];
if($_POST['dataDesc'] === 'COMMON.VCO.ONLINE.PAYMENT')
{
$transRequestXml->transactionRequest->addChild('callId',$_POST['callId']);
}
if(isset($_POST['paIndicator'])){
//$transRequestXml->transactionRequest->addChild('cardholderAuthentication');
//$transRequestXml->transactionRequest->cardholderAuthentication->addChild('authenticationIndicator',$_POST['paIndicator']);
//$transRequestXml->transactionRequest->cardholderAuthentication->addChild('cardholderAuthenticationValue',$_POST['paValue']);
}
$url="https://api.authorize.net/xml/v1/request.api";
//print_r($transRequestXml->asXML());
try{ //setting the curl parameters.
$ch = curl_init();
if (FALSE === $ch)
throw new Exception('failed to initialize');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $transRequestXml->asXML());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
// The following two curl SSL options are set to "false" for ease of development/debug purposes only.
// Any code used in production should either remove these lines or set them to the appropriate
// values to properly use secure connections for PCI-DSS compliance.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //for production, set value to true or 1
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //for production, set value to 2
curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
$content = curl_exec($ch);
if (FALSE === $content)
throw new Exception(curl_error($ch), curl_errno($ch));
curl_close($ch);
$xmlResult= @simplexml_load_string($content);
$jsonResult=json_encode($xmlResult);
$jsonResult=json_decode($jsonResult);
$auth_c = $jsonResult->transactionResponse->authCode;
$res_c = $jsonResult->transactionResponse->responseCode;
$trans_i = $jsonResult->transactionResponse->transId;
$enrolauthorizedotnet = new stdClass();
$postdata = array_map('utf8_encode', $_POST);
$existing_array = array('authCode'=>$auth_c, 'responseCode'=>$res_c, 'transId'=>$trans_i);
$postdata = array_merge($postdata, $existing_array);
$enrolauthorizedotnet->auth_json = json_encode($postdata);
$enrolauthorizedotnet->timeupdated = time();
$ret1 = $DB->insert_record("enrol_authorizedotnet", $enrolauthorizedotnet, true);
echo '<script type="text/javascript">
window.location.href="'.$CFG->wwwroot.'/enrol/authorizedotnet/update.php?id='.$ret1.'";
</script>';
}catch(Exception $e) {
trigger_error(sprintf('Curl failed with error #%d: %s', $e->getCode(), $e->getMessage()), E_USER_ERROR);
}