-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2babbb5
commit 3edf34a
Showing
5 changed files
with
149 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
/* | ||
Pagamentos MbWay | ||
Author: github.com/vladyslavkotyk/ifthenpay | ||
2021 | ||
*/ | ||
|
||
class MBway { | ||
|
||
private $mbway_key; | ||
private $channel; | ||
private $url_protocol; | ||
|
||
function __construct($mbway_key, $channel, $url_protocol) { | ||
|
||
$this->mbway_key = $mbway_key; | ||
$this->channel = $channel; | ||
$this->url_protocol = $url_protocol; | ||
} | ||
|
||
/* | ||
Criar um pagamento MBWay com o número de telemóvel do client | ||
$phone_number - Número de telemóvel do cliente | ||
$internal_reference - Referência interna para ser usada depois no callback e identificar o pagamento ( MAX 15 caráters ) | ||
$order_value - Valor da encomenda em € | ||
$customer_email - Email do utilizador ( OPCIONAL ) | ||
$description - Descrição do pagamento ( MAX 50 caráters ) ( OPCIONAL ) | ||
*/ | ||
|
||
function create($phone_number, $internal_reference, $order_value, $customer_email = "", $description = "") { | ||
|
||
$url = $this->url_protocol . "://www.ifthenpay.com/mbwayws/IfthenPayMBW.asmx/SetPedidoJSON"; | ||
|
||
// Dados que vão ser enviados para o Ifthenpay | ||
$post = http_build_query(array( | ||
'MbWayKey' => $this->mbway_key, | ||
'canal' => $this->channel, | ||
"referencia" => $internal_reference, | ||
"valor" => $order_value, | ||
"nrtlm" => $phone_number, | ||
"email" => $customer_email, | ||
"descricao" => $description | ||
)); | ||
|
||
$curl = curl_init(); | ||
|
||
curl_setopt($curl, CURLOPT_URL, $url); | ||
curl_setopt($curl, CURLOPT_POSTFIELDS, $post); | ||
curl_setopt($curl, CURLOPT_POST, true); | ||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | ||
|
||
$data = curl_exec($curl); | ||
|
||
curl_close($curl); | ||
|
||
var_dump($data); | ||
|
||
// Limpar e converter a resposta da api em JSON | ||
$data = str_replace('<?xml version="1.0" encoding="utf-8"?>', "", $data); | ||
$data = str_replace('<string xmlns="https://www.ifthenpay.com/">', "", $data); | ||
$data = str_replace("</string>", "", $data); | ||
|
||
$data = json_decode($data, true); | ||
|
||
return $data; | ||
} | ||
|
||
/* | ||
Obter o status do pagamento com uma API call à ifthenpay | ||
( sem usar o callback ) | ||
*/ | ||
|
||
function status($payment_id) { | ||
|
||
$url = $this->url_protocol . "://www.ifthenpay.com/mbwayws/IfthenPayMBW.asmx/EstadoPedidosJSON"; | ||
$url .= "?MbWayKey=" . $this->mbway_key . "&canal=" . $this->channel . "&idspagamento=" . $payment_id; | ||
|
||
$curl = curl_init(); | ||
|
||
curl_setopt($curl, CURLOPT_URL, $url); | ||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | ||
|
||
$data = curl_exec($curl); | ||
|
||
curl_close($curl); | ||
|
||
// Limpar e converter a resposta da api em JSON | ||
$data = str_replace('<?xml version="1.0" encoding="utf-8"?>', "", $data); | ||
$data = str_replace('<string xmlns="https://www.ifthenpay.com/">', "", $data); | ||
$data = str_replace("</string>", "", $data); | ||
|
||
$data = json_decode($data, true); | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
require("class/mbway.php"); | ||
|
||
define("MBWAY_KEY", "DTM-0000000"); // Key MbWay fornecida pelo IfThenpay | ||
define("IFTHENPAY_MBWAY_CHANNEL", "03"); // Canal predefinido na documentação do Ifthenpay | ||
|
||
define("URL_PROTOCOL", "http"); | ||
|
||
$mbway = new MBWay(MBWAY_KEY, IFTHENPAY_MBWAY_CHANNEL, URL_PROTOCOL); | ||
|
||
// Id da compra, neste caso um número random | ||
$phone_number = "910000000"; | ||
$customer_email = "[email protected]"; | ||
$description = "Compra teste"; | ||
$internal_reference = rand(1000, 10000); | ||
$order_value = 0.01; | ||
|
||
// Obter a referência | ||
$status = $mbway->create($phone_number, $internal_reference, $order_value, $customer_email, $description); | ||
|
||
// Guarda este ID numa database para futuro processamento de dados | ||
$payment_id = $status["IdPedido"]; | ||
|
||
// Obter o status do pagamento | ||
var_dump($mbway->status($payment_id)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters