Last active
August 6, 2018 12:23
-
-
Save murilohns/5aad2c772d5b80a15155800eaaa77646 to your computer and use it in GitHub Desktop.
PHP - Create Subscription with previous created card
This file contains hidden or 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
<html> | |
<head> | |
<html> | |
<head> | |
<!-- SCRIPT PAGAR.ME --> | |
<script src="https://assets.pagar.me/checkout/checkout.js"></script> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
</head> | |
<body> | |
<button id="pay-button">Abrir modal de pagamento</button> | |
<script> | |
(function(win, doc){ | |
var button = $('#pay-button'); | |
button.click(function(event) { | |
event.preventDefault(); | |
// INICIAR A INSTÂNCIA DO CHECKOUT | |
// declarando um callback de sucesso | |
var checkoutAPI = new PagarMeCheckout.Checkout({ | |
"encryption_key":"SUA_CHAVE_DE_CRIPTOGRAFIA", | |
success: function(data) { | |
$.ajax({ | |
type: "POST", | |
url: "php/create-subscription.php", | |
data: data, | |
success: function( data ){ | |
alert ( data ); | |
} | |
}); | |
} | |
}); | |
// DEFINIR AS OPÇÕES | |
// e abrir o modal | |
// É necessário passar os valores boolean em "var params" como string | |
var params = { | |
"amount":1000, | |
"buttonText":"Pagar", | |
"customerData":"false", | |
"paymentMethods":"boleto, credit_card", | |
"maxInstallments":12, | |
"uiColor":"#bababa", | |
"postbackUrl":"requestb.in/1234", | |
"createToken":"false", | |
"interestRate":12, | |
"freeInstallments":3, | |
"defaultInstallment":1, | |
"headerText":"Título", | |
"customerName": "Murilo Teste", | |
"customerDocumentNumber": "66685184868", | |
"customerEmail": "[email protected]", | |
"customerAddressStreet": "Rua Teste", | |
"customerAddressStreetNumber": "123", | |
"customerAddressComplementary": "Complemento", | |
"customerAddressNeighborhood": "Bairro", | |
"customerAddressCity": "Cidade", | |
"customerAddressState": "Estado", | |
"customerAddressZipcode": "04250000", | |
"customerPhoneDdd": "11", | |
"customerPhoneNumber": "852321459" | |
}; | |
checkoutAPI.open(params); | |
}) | |
})(window, document) | |
</script> | |
</body> | |
</html> |
This file contains hidden or 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
<?php | |
require("pagarme-php/Pagarme.php"); | |
$api_key = "SUA_CHAVE_DE_API"; | |
Pagarme::setApiKey($api_key); | |
$plan_id = "246102"; | |
$plan = PagarMe_Plan::findById($plan_id); //encontra um plano | |
$subscriptionData = array( | |
'plan' => $plan, | |
'customer' => $_POST['customer'] | |
); | |
if(isset($_POST['card_hash'])) { | |
$card = new PagarMe_Card(array( | |
'card_hash' => $_POST['card_hash'] | |
)); | |
$card->create(); | |
$subscriptionData['payment_method'] = 'credit_card'; | |
$subscriptionData['card_id'] = $card->id; | |
} else { | |
$subscriptionData['payment_method'] = 'boleto'; | |
} | |
$subscription = new PagarMe_Subscription($subscriptionData); | |
$subscription->create(); | |
echo "Subscription ID: " . $subscription->id; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment