-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPluginBitpayCallback.php
More file actions
76 lines (62 loc) · 2.63 KB
/
PluginBitpayCallback.php
File metadata and controls
76 lines (62 loc) · 2.63 KB
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
<?php
require_once 'modules/admin/models/PluginCallback.php';
require_once 'modules/admin/models/StatusAliasGateway.php' ;
require_once 'modules/billing/models/class.gateway.plugin.php';
require_once 'modules/billing/models/Invoice_EventLog.php';
require_once 'modules/admin/models/Error_EventLog.php';
class PluginBitpayCallback extends PluginCallback
{
function processCallback()
{
$data = file_get_contents("php://input");
$json = json_decode($data, true);
CE_Lib::log(4, "Callback: ". print_r($json, true));
$bpInvoiceId = $json['id'];
$invoiceData = $this->getInvoice($bpInvoiceId);
$cPlugin = new Plugin($invoiceData['posData'], "bitpay", $this->user);
$cPlugin->setAmount($invoiceData['price']);
$cPlugin->setAction('charge');
switch ($invoiceData['status']) {
case 'paid':
$transaction = "BitPay payment of {$invoiceData['price']} has been received.";
$cPlugin->PaymentPending($transaction, $bpInvoiceId);
break;
case 'confirmed':
$transaction = "BitPay payment of {$invoiceData['price']} has been confirmed.";
$cPlugin->PaymentPending($transaction, $bpInvoiceId);
break;
case 'complete':
$transaction = "BitPay payment of {$invoiceData['price']} has been completed.";
$cPlugin->PaymentAccepted($invoiceData['price'], $transaction, $bpInvoiceId);
break;
case 'expired':
case 'invalid':
$transaction = 'Invalid Transaction';
$cPlugin->PaymentRejected($transaction);
break;
}
}
private function getInvoice($invoiceId)
{
$url = 'https://bitpay.com/api/invoice/';
if ($this->settings->get('plugin_bitpay_Use Testing Environment?') == '1') {
$url = 'https://test.bitpay.com/api/invoice/';
}
$url .= $invoiceId;
$ch = curl_init($url);
$header = [
'Content-Type: application/json',
'Authorization: Basic ' . base64_encode($this->settings->get('plugin_bitpay_Legacy API Key')),
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
if (!$response) {
throw new CE_Exception('cURL BitPay Error: ' . curl_error($ch) . '( ' .curl_errno($ch) . ')');
}
curl_close($ch);
$response = json_decode($response, true);
return $response;
}
}