Permalink
Browse files

Now redirects to a confirmation page and clears the shopping cart.

README updated
  • Loading branch information...
1 parent bd433a0 commit 0bae575837e53209df42b6b3109093dd8f72808e @jaafit jaafit committed Nov 15, 2012
Showing with 118 additions and 58 deletions.
  1. +2 −2 README
  2. BIN bitpay/bitcoin25.png
  3. +2 −39 bitpay/callback.php
  4. +6 −15 bitpay/config.php
  5. +76 −0 bitpay/functions.php
  6. +6 −2 bitpay/redirect2bitpay.php
  7. +26 −0 bitpay/redirect2ecwid.php
View
4 README
@@ -4,7 +4,7 @@ Instructions:
In config.php:
Set $storeURL to the URL of your store's homepage.
Set $storeID to your ecwid store ID found in the bottom-right of the Ecwid control panel.
- Set $callbackURL to the URL of bitpay/callback.php on your server.
+ Set $bitpayURL to the URL of the bitpay/ folder which you extracted from this plugin.
Set $apiKey to the key you created at bitpay.com in the "My Account > API Access Keys" section.
Adjust $speed if desired.
@@ -27,7 +27,7 @@ In your Ecwid control panel:
/* bitpay checkout image */
img.defaultCCImage {
padding: 25px 263px 0px 0px;
- background: url('http://fde.minethings.com/ecwid/bitcoin25.png');
+ background: url('http://fde.minethings.com/ecwid/bitpay/bitcoin25.png');
background-size:auto;
background-repeat:no-repeat;
width:0px;
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -2,6 +2,7 @@
require 'config.php';
require 'bp_lib.php';
+require 'functions.php';
$notice = bpVerifyNotification($apiKey);
@@ -10,44 +11,6 @@
die;
}
-$x_response_code = '1'; // 1=approved, 2=declined
-$x_response_reason_code = '1'; // 1=approved, 2= declined
-$x_trans_id = $notice['id'];
-$x_invoice_num = $notice['posData'][1];
-$x_amount = $notice['posData'][0];
-
-$string = $hashValue.$login.$x_trans_id.$x_amount;
-$x_MD5_Hash = md5($string);
-$datatopost = array (
- "x_response_code" => $x_response_code,
- "x_response_reason_code" => $x_response_reason_code,
- "x_trans_id" => $x_trans_id,
- "x_invoice_num" => $x_invoice_num,
- "x_amount" => $x_amount,
- "x_MD5_Hash" => $x_MD5_Hash,
- );
-
-switch($notice['status']){
- case 'completed':
- case 'confirmed':
- $url = 'http://app.ecwid.com/authorizenet/'.$storeId;
- $ch = curl_init($url);
-
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $datatopost);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-
- $response = curl_exec($ch);
- if ($response === false){
- debuglog('request to ecwid.com failed');
- debuglog($url);
- debuglog($notice);
- debuglog($datatopost);
- debuglog(curl_error($ch));
- }
-
- curl_close($ch);
- break;
- }
+postToEcwid($notice);
?>
View
@@ -5,27 +5,18 @@
$storeId = ''; // found in your ecwid control panel, bottom-right
// bitpay settings
-// url of callback.php on your server. example: 'http://www.example.com/ecwid/bitpay/callback.php
-$callbackURL = '';
-// create this at bitpay.com in your account settings and paste it here
+// url of bitpay folder on your server. example: 'http://www.example.com/ecwid/bitpay/
+$bitpayURL = '';
+// apiKey: create this at bitpay.com in your account settings and paste it here
$apiKey = ''; // ex 'DNboT9fVNpW7usAuDNboT9fVNpW7usAu'
+// speed: Warning: on medium/low, customers will not see an order confirmation page.
$speed = 'high'; // can be 'high', 'medium' or 'low'. See bitpay API doc for more details.
//payment method settings
$login = ''; // see README
$hashValue = ''; // see README
-
-function debuglog($contents)
-{
- $file = 'log.txt';
- file_put_contents($file, date('m-d H:i:s').": ", FILE_APPEND);
- if (is_array($contents))
- file_put_contents($file, var_export($contents, true)."\n", FILE_APPEND);
- else if (is_object($contents))
- file_put_contents($file, json_encode($contents)."\n", FILE_APPEND);
- else
- file_put_contents($file, $contents."\n", FILE_APPEND);
-}
+// add trailing slash to url
+$bitpayURL = preg_replace('#([^\/])$#', '\1/', $bitpayURL);
?>
View
@@ -0,0 +1,76 @@
+<?php
+
+function debuglog($contents)
+{
+ $file = 'log.txt';
+ file_put_contents($file, date('m-d H:i:s').": ", FILE_APPEND);
+ if (is_array($contents))
+ file_put_contents($file, var_export($contents, true)."\n", FILE_APPEND);
+ else if (is_object($contents))
+ file_put_contents($file, json_encode($contents)."\n", FILE_APPEND);
+ else
+ file_put_contents($file, $contents."\n", FILE_APPEND);
+}
+
+function postToEcwid($notice)
+{
+ require 'config.php';
+
+ $x_response_code = '1'; // 1=approved, 2=declined
+ $x_response_reason_code = '1'; // 1=approved, 2= declined
+ $x_trans_id = $notice['id'];
+ $x_invoice_num = $notice['posData'][1];
+ $x_amount = $notice['posData'][0];
+
+ $string = $hashValue.$login.$x_trans_id.$x_amount;
+ $x_MD5_Hash = md5($string);
+ $datatopost = array (
+ "x_response_code" => $x_response_code,
+ "x_response_reason_code" => $x_response_reason_code,
+ "x_trans_id" => $x_trans_id,
+ "x_invoice_num" => $x_invoice_num,
+ "x_amount" => $x_amount,
+ "x_MD5_Hash" => $x_MD5_Hash,
+ );
+
+ switch($notice['status']){
+ case 'completed':
+ case 'confirmed':
+ $url = 'http://app.ecwid.com/authorizenet/'.$storeId;
+ $ch = curl_init($url);
+
+ curl_setopt($ch, CURLOPT_POST, 1);
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $datatopost);
+ //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+
+ $response = curl_exec($ch);
+ if ($response === false){
+ debuglog('request to ecwid.com failed');
+ debuglog($url);
+ debuglog($notice);
+ debuglog($datatopost);
+ debuglog(curl_error($ch));
+ }
+
+ curl_close($ch);
+ return $response;
+ default:
+ return false;
+ }
+}
+
+// delete .inv files that are older than 24 hrs
+function deleteOldInvs() {
+ if ($handle = opendir('./')) {
+ while (false !== ($file = readdir($handle))) {
+ $ext = substr($file, -3);
+ if ($ext != 'inv')
+ continue;
+ if((time() - filemtime($file)) > 86400)
+ unlink($file);
+
+ }
+ closedir($handle);
+ }
+}
+?>
@@ -2,6 +2,7 @@
require 'bp_lib.php';
require 'config.php';
+require 'functions.php';
if ($_POST['x_login'] != $login) {
debuglog('ecwid login does not match that found in config.php');
@@ -14,12 +15,12 @@
$_POST['x_invoice_num']);
$options = array(
'apiKey' => $apiKey,
- 'notificationURL' => $callbackURL,
+ 'notificationURL' => $bitpayURL.'callback.php',
'transactionSpeed' => $speed,
'fullNotifications' => true,
'itemDesc' => $_POST['x_description'],
'currency' => $_POST['x_currency_code'],
- 'redirectURL' => $storeURL,
+ 'redirectURL' => $bitpayURL.'redirect2ecwid.php?ecwidInvoiceId='.$_POST['x_invoice_num'],
'buyerEmail' => $_POST['x_email'],
'buyerName' => $_POST['x_first_name'].' '.$_POST['x_last_name'],
'buyerAddress1' => $_POST['x_address'],
@@ -37,6 +38,9 @@
die;
}
+// save bitpay invoice id in a file named after the ecwid invoice id
+file_put_contents($_POST['x_invoice_num'].'.inv', $invoice['id']);
+
// redirect to bitpay
header('Location: '.$invoice['url']);
View
@@ -0,0 +1,26 @@
+<?php
+require 'config.php';
+require 'bp_lib.php';
+require 'functions.php';
+
+// get invoice number from file
+$file = $_GET['ecwidInvoiceId'].'.inv';
+
+if (file_exists($file))
+{
+ $invoiceId = file_get_contents($file);
+ deleteOldInvs();
+
+ $invoice = bpGetInvoice($invoiceId, $apiKey);
+ if ($invoice['status'] == 'confirmed' or $invoice['status'] == 'completed')
+ {
+ postToEcwid($invoice); // this will redirect to a confirmation page
+ die;
+ }
+}
+else
+ debuglog('no file found for invoice '.$_GET['ecwidInvoiceId']);
+
+header('Location: '.$storeURL); // if the transaction speed is medium/low, they'll be redirected w/o confirmation
+
+?>

0 comments on commit 0bae575

Please sign in to comment.