Skip to content

Commit

Permalink
More changes to make WebsiteFee Zikula 3.0 compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
paustian committed Jan 22, 2021
1 parent d0edc2b commit d1d32a3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
29 changes: 19 additions & 10 deletions Api/IpnListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,23 @@ protected function curlPost(string $encoded_data) : void {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded_data);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . "/cacert.pem");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: PHP-IPN-Verification-Script', 'Connection: Close'));
$cert_directory = dirname(__FILE__) . "/cacert.pem";
curl_setopt($ch, CURLOPT_CAINFO, $cert_directory);

$this->response = curl_exec($ch);
$this->response_status = strval(curl_getinfo($ch, CURLINFO_HTTP_CODE));

if ($this->response === false || $this->response_status == '0') {
if (! ($this->response) || ($this->response_status == '0') ) {
$errno = curl_errno($ch);
$errstr = curl_error($ch);
curl_close($ch);
throw new Exception("cURL error: [$errno] $errstr");
throw new Exception("cURL error: [$errno] $errstr $cert_directory $uri");
}
curl_close($ch);
}
Expand Down Expand Up @@ -202,11 +205,17 @@ public function processIpn($post_data = null) : bool {
foreach($ray_post_array as $keyval){
$keyval = explode('=', $keyval);
if(count($keyval) == 2){
if ($keyval[0] === 'payment_date') {
if (substr_count($keyval[1], '+') === 1) {
$keyval[1] = str_replace('+', '%2B', $keyval[1]);
}
}
$myPost[$keyval[0]] = urldecode($keyval[1]);
}
}
foreach($myPost as $key => $value){
$encoded_data .= "&$key=" . urlencode($value);
foreach ($myPost as $key => $value) {
$value = urlencode($value);
$encoded_data .= "&$key=$value";
}
$this->entryData = $encoded_data;
} else {
Expand All @@ -215,7 +224,6 @@ public function processIpn($post_data = null) : bool {
} else {
// use provided data array
$this->post_data = $post_data;

foreach ($this->post_data as $key => $value) {
$encoded_data .= "&$key=" . urlencode($value);
}
Expand All @@ -226,13 +234,14 @@ public function processIpn($post_data = null) : bool {
} else {
$this->curlPost($encoded_data);
}
if (strpos($this->response_status, '200') === false) {

if ($this->response_status !== '200') {
throw new Exception("Invalid response status: " . $this->response_status . $encoded_data);
}

if (strpos($this->response, "VERIFIED") !== false) {
if ($this->response === "VERIFIED") {
return true;
} elseif (strpos($this->response, "INVALID") !== false) {
} elseif ($this->response === "INVALID") {
return false;
} else {
throw new Exception("Unexpected response from PayPal. " . $encoded_data);
Expand Down
16 changes: 11 additions & 5 deletions Controller/SubscribeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class SubscribeController extends AbstractController {
private $response;
private $request;
private $listener;
private $debug = true;
private $debug = false;

/**
* @Route("")
Expand Down Expand Up @@ -73,7 +73,7 @@ public function indexAction() : Response {
public function testsubscribeAction(Request $request) : Response {
if($this->debug){
return $this->render('@PaustianWebsiteFeeModule/Subscribe/websitefee_subscribe_testsubscribe.html.twig',
['txnID' => bin2hex(random_bytes(8))]);
['txnID' => bin2hex(random_bytes(8))]);
}
return new Response($this->render('@PaustianWebsiteFeeModule/Subscribe/websitefee_subscribe_index.html.twig'));
}
Expand Down Expand Up @@ -117,7 +117,9 @@ public function subscribepaypalAction(Request $request) : Response {
$this->_set_error($e->getMessage());
exit(0);
}
if ($verified || $this->listener->use_sandbox) {
$this->response = $this->listener->getResponse();

if ($verified) {
$uid = $request->get('custom');
$txn_id = $request->get('txn_id');
$reciever_email = urldecode($request->get('receiver_email'));
Expand All @@ -129,7 +131,7 @@ public function subscribepaypalAction(Request $request) : Response {
$this->response = $this->listener->getResponse();
$this->request = $this->listener->getPostUri();
//enter transcaction makes sure it has all the information that we need
if ($this->_enterTransaction($uid, $txn_id, $payer_email, $reciever_email, $payment_gross, $item_no, $txn_type)) {
if ($this->_enterTransaction((int)$uid, $txn_id, $payer_email, $reciever_email, $payment_gross, $item_no, $txn_type)) {
if ($txn_type === 'subscr_cancel') {
$this->_cancelSubscription($uid, $item_no);
} else if (($txn_type === 'subscr_payment' && $payment_status === 'Completed') || ($txn_type == 'web_accept')) {
Expand All @@ -144,6 +146,7 @@ public function subscribepaypalAction(Request $request) : Response {
$this->_set_error("Transaction not verified.");

}

if($this->debug){
return $this->render('@PaustianWebsiteFeeModule/Subscribe/websitefee_subscribe_testsubscribe.html.twig');
}
Expand Down Expand Up @@ -282,7 +285,6 @@ private function _enterTransaction(int $uid,
(!isset($txn_id))) {
throw new NotFoundHttpException($this->trans('Variable error in _enter_transaction'));
}

//see if we can find an item with the same txn_id in the
//database. If we can this is a problem.
$em = $this->getDoctrine()->getManager();
Expand All @@ -292,6 +294,8 @@ private function _enterTransaction(int $uid,
$qb->where('u.wsftxid = ?1');
$qb->setParameter(1, $txn_id);
$query = $qb->getQuery();


// execute query
$dup_trans = $query->getResult();

Expand All @@ -317,6 +321,7 @@ private function _enterTransaction(int $uid,
$this->_set_error("Incorrect reciever Email:" . $receiver_email . ", correct Email should be:" . $email);
return false;
}

if($subscr_type !== "subscr_cancel"){
$payment_amt = $subscript_info->getWsfpaymentamount();
//I added a range because Paypal was being cute and adding tax
Expand Down Expand Up @@ -345,6 +350,7 @@ private function _enterTransaction(int $uid,
$em->flush();
//Just record the information for now. Get rid of this code later
$this->_set_error("transaction worked.");

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<td>{{ item.wsftxid }}</td>
<td>{{ item.wsfsubtype }}</td>
<td>{{ item.wsfemail }}</td>
<td>{{ item.wsfpaydate.date }}</td>
<td>{{ item.wsferrdate | date('Y-m-d') }}</td>
<td class="actions">
<a href="{{ path('paustianwebsitefeemodule_admin_deletetrans') }}/{{ item.id }}" class="fas fa-trash" onclick="return confirm('Are you sure you want to delete this subscription information?')"></a>
</td>
Expand Down

0 comments on commit d1d32a3

Please sign in to comment.