Skip to content

Instantly share code, notes, and snippets.

@crushcafeteria
Created June 3, 2016 11:06
Show Gist options
  • Save crushcafeteria/f0cb73182867d70ad638e1f60ddd2090 to your computer and use it in GitHub Desktop.
Save crushcafeteria/f0cb73182867d70ad638e1f60ddd2090 to your computer and use it in GitHub Desktop.
<?php
# Final storage for the products our script
# will send to BlackPay for billing
$cartItems = array(); # or [], who cares?
# Get an array of all items the customer has
# placed in the shopping basket
$basket = $this->getShoppingBasketContents('basket_uuid');
# If the previous array has extra information, you can
# filter it out so you are left with the required
# fields only as specified in the docs
foreach($basket as $key => $item){
# Make empty array to hold the required fields only
$tmp = [
'name' => $item['name'], // Product name
'qty' => $item['quantity'], // How many?
'price' => $item['price'], // Price per item
];
# Push each product to $cartItems
$cartItems[] = $tmp;
}
# Encode to JSON string. Recommended to eliminate
# all whitespace from the JSON string.
$cartItems = json_encode($cartItems);
# Encode JSON string to BASE64 string for easy
# transport to BlackPay Express
$cartItems = base64_encode($cartItems);
?>
<!--Send your request with an normal POST payload-->
<form method="POST" action="[ENTER BLACKPAY EXPRESS ENDPOINT]">
<input type="hidden" name="store_id" value="[ENTER STORE ID]">
<input type="hidden" name="cart_id" value="[CART UNIQUE ID]">
<input type="hidden" name="cart_items" value="<?php echo $cartItems?>"> <!-- $cartItems string here -->
<input type="hidden" name="grand_total" value="[GRAND TOTAL]">
<input type="hidden" name="success_url" value="[LINK TO REDIRECT ON SUCCESS]">
<input type="hidden" name="failed_url" value="[LINK TO REDIRECT ON FAIL]">
<input type="hidden" name="cancel_url" value="[LINK TO REDIRECT ON CANCEL]">
<input type="hidden" name="customer_email" value="[CUSTOMER EMAIL]">
<button type="submit" class="btn btn-success btn-lg btn-block">Pay with M-PESA</button>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment