Skip to content

Commit

Permalink
Paypal Express Checkout implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
PaylineGitHub committed Sep 19, 2017
1 parent 7f6e9f9 commit 92bd99f
Show file tree
Hide file tree
Showing 3 changed files with 291 additions and 0 deletions.
56 changes: 56 additions & 0 deletions examples/demos/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
$array = array(
'doWebPayment',
'fullWebPayment',
'paypalExpressCheckout',
'widgetPayment',
'getWebPaymentDetails'
);
Expand All @@ -14,6 +15,7 @@
$links .= ( 'doWebPayment'==$selected ) ? "<a class='backtoform' href='?e=doWebPayment'>doWebPayment (light)</a> - " : "<a href='?e=doWebPayment'>doWebPayment (light)</a> - ";
$links .= ( 'fullWebPayment'==$selected ) ? "<a class='backtoform' href='?e=fullWebPayment'>doWebPayment (full)</a> - " : "<a href='?e=fullWebPayment'>doWebPayment (full)</a> - ";
$links .= ( 'widgetPayment'==$selected ) ? "<a class='backtoform' href='?e=widgetPayment'>doWebPayment (advanced widget)</a> - " : "<a href='?e=widgetPayment'>doWebPayment (advanced widget)</a> - ";
$links .= ( 'paypalExpressCheckout'==$selected ) ? "<a class='backtoform' href='?e=paypalExpressCheckout'>Paypal Express Checkout</a> - " : "<a href='?e=paypalExpressCheckout'>Paypal Express Checkout</a> - ";
$links .= ( 'getWebPaymentDetails'==$selected ) ? "<a class='backtoform' href='?e=getWebPaymentDetails'>getWebPaymentDetails</a>" : "<a href='?e=getWebPaymentDetails'>getWebPaymentDetails</a>";
$links .= '</h3>';

Expand Down Expand Up @@ -181,6 +183,60 @@ function sessionUpdated(token){
document.getElementById('widgetPaymentForm').style.display="none";
Payline.Api.show();
}

function showStateFunction(state) {
if ("PAYMENT_METHODS_LIST_FAST_CHECKOUT" == state.state) {
// specific process if needed
}
if ("PAYMENT_TRANSITIONAL_FAST_CHECKOUT" == state.state) {
var buyer = Payline.Api.getBuyerFastCheckout();
document.getElementById("paypalBuyerEmail").value = buyer.email;
document.getElementById("paypalBuyerFirstName").value = buyer.firstName;
document.getElementById("paypalBuyerLastName").value = buyer.lastName;
document.getElementById("paypalBuyerStreet1").value = buyer.street1;
document.getElementById("paypalBuyerStreet2").value = buyer.street2;
document.getElementById("paypalBuyerZipcode").value = buyer.zipCode;
document.getElementById("paypalBuyerCity").value = buyer.cityName;
document.getElementById("paypalBuyerCountry").value = buyer.country;
}
if ("PAYMENT_SUCCESS" == state.state) {
// specific process if needed
}
}

function finalizeExpressCheckout(token){
var shippingOption = document.getElementsByName('shippingOption');
var shippingCost = 1;
for (var i = 0, len = shippingOption.length; i < len; i++) {
if(shippingOption[i].checked){
shippingCost = document.getElementsByName('shippingOption')[i].value;
}
}
var finalPaymentAmount = parseInt(shippingCost)+parseInt(document.getElementById("paypalExpressCheckoutAmount").value);

var datasPayline = {
'payment':{
'amount':finalPaymentAmount,
},'order':{
'amount':finalPaymentAmount,
},'buyer':{
'shippingAddress':{
'lastName':document.getElementById('paypalBuyerLastName').value,
'firstName':document.getElementById('paypalBuyerFirstName').value,
'street1':document.getElementById('paypalBuyerStreet1').value,
'street2':document.getElementById('paypalBuyerStreet2').value,
'cityName':document.getElementById('paypalBuyerCity').value,
'zipCode':document.getElementById('paypalBuyerZipcode').value,
'country':document.getElementById('paypalBuyerCountry').value
}
}
};
Payline.Api.updateWebpaymentData(token, datasPayline, function (response) {
// TODO
});
Payline.Api.finalizeFastCheckout();
}

</script>
<!--SCRIPTS END-->
</head>
Expand Down
205 changes: 205 additions & 0 deletions examples/web/paypalExpressCheckout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
<?php
include '../initSDK.php';
use Payline\PaylineSDK;

if(isset($_POST['submit'])){ // display Paypal button

//VERSION
$array['version'] = $_SESSION['WS_VERSION'];

// PAYMENT
$array['payment']['amount'] = $_POST['amount'];
$array['payment']['currency'] = $_POST['currency'];
$array['payment']['action'] = $_SESSION['PAYMENT_ACTION'];
$array['payment']['mode'] = $_SESSION['PAYMENT_MODE'];

$_SESSION['paypalExpressCheckoutAmount'] = $_POST['amount'];

// ORDER
$array['order']['ref'] = $_POST['ref'];
$array['order']['amount'] = $array['payment']['amount'];
$array['order']['currency'] = $array['payment']['currency'];
$array['order']['date'] = date('d/m/Y H:i');
$array['order']['origin'] = $_SESSION['orderOrigin'];
$array['order']['country'] = $_SESSION['orderCountry'];
$array['order']['taxes'] = $_SESSION['orderTaxes'];
$array['order']['deliveryTime'] = $_SESSION['deliveryTime'];
$array['order']['deliveryMode'] = $_SESSION['deliveryMode'];

// ORDER DETAILS
$item1 = array();
$item1['ref'] = $_SESSION['orderDetailRef1'];
$item1['price'] = $_SESSION['orderDetailPrice1'];
$item1['quantity'] = $_SESSION['orderDetailQuantity1'];
$item1['comment'] = $_SESSION['orderDetailComment1'];
$item1['category'] = $_SESSION['orderDetailCategory1'];
$item1['brand'] = $_SESSION['orderDetailBrand1'];
$item1['subcategory1'] = $_SESSION['orderDetailSubcategory1_1'];
$item1['subcategory2'] = $_SESSION['orderDetailSubcategory2_1'];
$item1['additionalData'] = $_SESSION['orderDetailAdditionalData1'];
$item1['taxRate'] = $_SESSION['orderDetailTaxRate1'];
$payline->addOrderDetail($item1);

$item2 = array();
$item2['ref'] = $_SESSION['orderDetailRef2'];
$item2['price'] = $_SESSION['orderDetailPrice2'];
$item2['quantity'] = $_SESSION['orderDetailQuantity2'];
$item2['comment'] = $_SESSION['orderDetailComment2'];
$item2['category'] = $_SESSION['orderDetailCategory2'];
$item2['brand'] = $_SESSION['orderDetailBrand2'];
$item2['subcategory1'] = $_SESSION['orderDetailSubcategory1_2'];
$item2['subcategory2'] = $_SESSION['orderDetailSubcategory2_2'];
$item2['additionalData'] = $_SESSION['orderDetailAdditionalData2'];
$item2['taxRate'] = $_SESSION['orderDetailTaxRate2'];
$payline->addOrderDetail($item2);

// CONTRACT NUMBERS
$array['payment']['contractNumber'] = $_POST['paypalContractNumber'];
$contracts = explode(";",$_POST['paypalContractNumber']);
$array['contracts'] = $contracts;
$array['secondContracts'] = null;
$array['walletContracts'] = null;

// URLS
$array['notificationURL'] = $_SESSION['NOTIFICATION_URL'];
$array['returnURL'] = $_SESSION['RETURN_URL'];
$array['cancelURL'] = $_SESSION['CANCEL_URL'];

// options
$array['customPaymentPageCode'] = $_SESSION['CUSTOM_PAYMENT_PAGE_CODE'];
$array['customPaymentTemplateURL'] = $_SESSION['CUSTOM_PAYMENT_TEMPLATE_URL'];
$array['languageCode'] = $_SESSION['LANGUAGE_CODE'];

// BUYER
/*
$array['buyer']['legalStatus'] = $_SESSION['buyerLegalStatus'];
$array['buyer']['title'] = $_SESSION['buyerTitle'];
$array['buyer']['lastName'] = $_SESSION['buyerLastName'];
$array['buyer']['firstName'] = $_SESSION['buyerFirstName'];
$array['buyer']['email'] = $_SESSION['buyerEmail'];
$array['buyer']['mobilePhone'] = $_SESSION['mobilePhone'];
$array['buyer']['customerId'] = $_SESSION['customerId'];
$array['buyer']['accountCreateDate'] = $_SESSION['buyerAccountCreateDate'];
$array['buyer']['accountAverageAmount'] = $_SESSION['buyerAverageAmount'];
$array['buyer']['accountOrderCount'] = $_SESSION['buyerOrderCount'];
$array['buyer']['walletId'] = $_SESSION['buyerWalletId'];
$array['buyer']['walletDisplayed'] = $_SESSION['buyerWalletDisplayed'];
$array['buyer']['walletSecured'] = $_SESSION['buyerWalletSecured'];
$array['buyer']['walletCardInd'] = $_SESSION['buyerWalletCardInd'];
$array['buyer']['ip'] = $_SESSION['buyerIp'];
$array['buyer']['legalDocument'] = $_SESSION['legalDocument'];
$array['buyer']['birthDate'] = $_SESSION['birthDate'];
$array['buyer']['fingerprintID'] = $_SESSION['fingerprintID'];
// BILLING ADDRESS
$array['billingAddress']['title'] = $_SESSION['billingAddressTitle'];
$array['billingAddress']['firstName'] = $_SESSION['billingAddressFirstName'];
$array['billingAddress']['lastName'] = $_SESSION['billingAddressLastName'];
$array['billingAddress']['name'] = $_SESSION['billingAddressName'];
$array['billingAddress']['street1'] = $_SESSION['billingAddressStreet1'];
$array['billingAddress']['street2'] = $_SESSION['billingAddressStreet2'];
$array['billingAddress']['county'] = $_SESSION['billingAddressCounty'];
$array['billingAddress']['cityName'] = $_SESSION['billingAddressCity'];
$array['billingAddress']['zipCode'] = $_SESSION['billingAddressZipCode'];
$array['billingAddress']['country'] = $_SESSION['billingAddressCountry'];
$array['billingAddress']['state'] = $_SESSION['billingAddressState'];
$array['billingAddress']['phoneType'] = $_SESSION['billingAddressPhoneType'];
$array['billingAddress']['phone'] = $_SESSION['billingAddressPhone'];
// SHIPPING ADDRESS
$array['shippingAddress']['title'] = $_SESSION['shippingAddressTitle'];
$array['shippingAddress']['firstName'] = $_SESSION['shippingAddressFirstName'];
$array['shippingAddress']['lastName'] = $_SESSION['shippingAddressLastName'];
$array['shippingAddress']['name'] = $_SESSION['shippingAddressName'];
$array['shippingAddress']['street1'] = $_SESSION['shippingAddressStreet1'];
$array['shippingAddress']['street2'] = $_SESSION['shippingAddressStreet2'];
$array['shippingAddress']['county'] = $_SESSION['shippingAddressCounty'];
$array['shippingAddress']['cityName'] = $_SESSION['shippingAddressCity'];
$array['shippingAddress']['zipCode'] = $_SESSION['shippingAddressZipCode'];
$array['shippingAddress']['country'] = $_SESSION['shippingAddressCountry'];
$array['shippingAddress']['state'] = $_SESSION['shippingAddressState'];
$array['shippingAddress']['phoneType'] = $_SESSION['shippingAddressPhoneType'];
$array['shippingAddress']['phone'] = $_SESSION['shippingAddressPhone'];
*/

// MERCHANT NAME
$array['merchantName'] = $_SESSION['MERCHANT_NAME'];

// PRIVATE DATA
for($i=1;$i<=8;$i++){
$privateData = array();
$privateData['key'] = $_SESSION['pvdKey'.$i] ;
$privateData['value'] = $_SESSION['pvdValue'.$i];
$payline->addPrivateData($privateData);
}

// EXECUTE
$response = $payline->doWebPayment($array);
if(isset($response) && $response['result']['code'] == '00000'){
echo "<span>&nbsp;</span>";
echo "<div id='PaylineWidget' data-token='".$response['token']."' data-template='fastcheckout' data-event-didshowstate='showStateFunction'></div>";
} elseif(isset($response)) {
echo '<span>ERROR : '.$response['result']['code']. ' '.$response['result']['longMessage'].' </span>';
}
}else{ // back from Paypal pages
?>
<form class="payline-form">
<input type='hidden' name='paypalExpressCheckoutAmount' id='paypalExpressCheckoutAmount' value= '<?php echo $_SESSION['paypalExpressCheckoutAmount']; ?>'/>
<fieldset>
<h4>Information retrieved from Paypal</h4>
<div class="row">
<label for="paypalBuyerFirstName">First name</label>
<input type='text' name='paypalBuyerFirstName' id='paypalBuyerFirstName' />
</div>
<div class="row">
<label for="paypalBuyerLastName">Last name</label>
<input type='text' name='paypalBuyerLastName' id='paypalBuyerLastName' />
</div>
<div class="row">
<label for="paypalBuyerEmail">email</label>
<input type='text' name='paypalBuyerEmail' id='paypalBuyerEmail' />
</div>
<div class="row">
<label for="paypalBuyerStreet1">Address (street 1)</label>
<input type='text' name='paypalBuyerStreet1' id='paypalBuyerStreet1' />
</div>
<div class="row">
<label for="paypalBuyerStreet2">Address (street 2)</label>
<input type='text' name='paypalBuyerStreet2' id='paypalBuyerStreet2' />
</div>
<div class="row">
<label for="paypalBuyerZipcode">Zip code</label>
<input type='text' name='paypalBuyerZipcode' id='paypalBuyerZipcode' />
</div>
<div class="row">
<label for="paypalBuyerCity">City</label>
<input type='text' name='paypalBuyerCity' id='paypalBuyerCity' />
</div>
<div class="row">
<label for="paypalBuyerCountry">Country code</label>
<input type='text' name='paypalBuyerCountry' id='paypalBuyerCountry' />
</div>
</fieldset>
<fieldset>
<h4>Choose your shipping option</h4>
<div class="row">
<input type="radio" id="shippingOption1" name="shippingOption" value="0">
<label for="shippingOption1">Pick to store (free)</label>
</div>
<div class="row">
<input type="radio" id="shippingOption2" name="shippingOption" value="299">
<label for="shippingOption2">Standard (2.99&euro;)</label>
</div>
<div class="row">
<input type="radio" id="shippingOption3" name="shippingOption" value="599">
<label for="shippingOption3">Express (5.99&euro;)</label>
</div>
</fieldset>
</form>
<div id='PaylineWidget' data-token='<?php echo $_GET['paylinetoken'];?>' data-template='fastcheckout' data-event-didshowstate='showStateFunction'></div>
<div class='PaylineWidget pl-pay-btn-container' style='text-align:center;'>
<button class='PaylineWidget pl-pay-btn' type='button' onclick='finalizeExpressCheckout();'>Finalize Express Checkout</button>
</div>
<?php
}
?>
30 changes: 30 additions & 0 deletions examples/web/paypalExpressCheckoutForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<form action="../demos/web.php?e=paypalExpressCheckout" method="post" class="payline-form">
<fieldset>
<h4>Do Web Payment minimal informations</h4>
<div class="row">
<label for="ref">Order reference</label>
<input type="text" name="ref" id="ref" value="<?php echo 'PHP-'.time()?>">
<span class="help">(required)</span>
</div>
<div class="row">
<label for="amount">Payment amount</label>
<input type="text" name="amount" id="amount" value="<?php echo $_SESSION['PAYMENT_AMOUNT'];?>">
<span class="help">(required)</span>
</div>
<div class="row">
<label for="currency">Payment currency</label>
<input type="text" name="currency" id="currency" value="<?php echo $_SESSION['PAYMENT_CURRENCY'];?>">
<span class="help">(required)</span>
</div>
</fieldset>
<fieldset>
<h4>Paypal informations</h4>
<div class="row">
<label for="ref">Paypal contract number</label>
<input type="text" name="paypalContractNumber" id="paypalContractNumber" value="">
<span class="help">(required)</span>
</div>
</fieldset>
<fieldset>
<input type="submit" name="submit" class="submit" value="paypalExpressCheckout">
</form>

0 comments on commit 92bd99f

Please sign in to comment.