by
ewanmc » Fri Jul 20, 2018 1:03 am
I've recently managed to develop a system with couchcart that uses the new checkout.js PayPal flow. I haven't used it to do exactly what you're after, but I have managed to store details about specific purchases in couch, and send a success email to the customer and admin. It's a simplified version of couchcart I'm using (no coupons or variations, and the shipping is standard per item), but these could be reintegrated. I'm sure it could be modified so the product had the option of being physical or download.
Basically you replace the paypal button that couch uses with the paypal button.
You'll need to sign up to the Paypal developer API to get your REST API credentials
Then I used this script when the paypal button is clicked:
- Code: Select all
<script src="https://www.paypalobjects.com/api/checkout.js"></script>
<script>
paypal.Button.render({
env: 'production', // 'sandbox | production
client: {
sandbox: 'XXXSANDBOXCODEXXX',
production: 'XXXPRODUCTIONCODEXXX'
},
commit: true, // Show a 'Pay Now' button
style: {
color: 'gold',
size: 'small'
},
payment: function(data, actions) {
return actions.payment.create({
payment: {
"transactions": [
{
"amount": {
"total": "<cms:number_format "<cms:pp_total />" />",
"currency": "GBP",
},
"description": "Order Details",
"item_list": {
"items": [
<cms:pp_cart_items>
{
"name": "<cms:show title />",
"quantity": "<cms:show quantity/>",
"price": "<cms:number_format price />",
"sku": "<cms:show id />",
"currency": "GBP"
},
</cms:pp_cart_items>
{
"name": "postage",
"quantity": "1",
"price": "<cms:number_format "<cms:pp_shipping />" />",
"currency": "GBP"
}
]
}
}
]
}
});
},
onAuthorize: function(data, actions) {
// Make a call to the REST api to execute the payment
return actions.payment.execute().then(function(payment) {
var email = payment.payer.payer_info.email;
var tid = payment.transactions[0].related_resources[0].sale.id;
var shipping = payment.payer.payer_info.shipping_address;
var uname= (shipping.recipient_name);
var address_details= (shipping.line1+", "+shipping.postal_code+", "+shipping.city+", "+shipping.state+", "+shipping.country_code);
var itemsarray= '<cms:show basket as_json="1"/>';
$.ajax({
method: "GET",
url: "<cms:show k_site_link />purchases.php",
data: { pp_item_customer: uname, pp_item_email: email, pp_item_id: tid, pp_item_address: address_details, pp_item_name: itemsarray, pp_amount: "<cms:pp_total />" },
success: function(data) {
window.alert('Payment Complete!');
window.location.href = "purchasesuccess.php?bkt=1&pid="+tid;
}
})
});
},
onCancel: function(data, actions) {
window.alert('Payment Cancelled!');
},
onError: function(err) {
window.alert('Payment Error');
}
}, '#paypal-button');
</script>
Purchases.php is a clonable template
- Code: Select all
<cms:template title='Purchases' clonable='1' order="1300">
<cms:editable type='text' name='purchase_name' label='Customer' order="1"/>
<cms:editable type='text' name='purchase_email' label='Email' order="2"/>
<cms:editable type='text' name='purchase_address' label='Address' order="3"/>
<cms:repeatable name='purchase_items' order='4'>
<cms:editable type='text' name='purchase_item_id' label='Item ID'/>
<cms:editable type='text' name='purchase_item_quantity' label='Item Quantity'/>
</cms:repeatable>
<cms:editable type='text' name='purchase_id' label='Paypal Transaction ID' order="5"/>
<cms:editable type='text' name="purchase_price" label="Total" order="6"/>
<cms:editable type='dropdown' name="purchase_status" label="Status" opt_values="confirmed|unconfirmed" order="7"/>
<cms:editable type='textarea' name="purchase_notes" label="Notes" order="8"/>
</cms:template>
The purchase is sent via ajax to create a new purchase once the paypal transaction returns completed. I used KK's new databound repeatable json function here for each item (
viewtopic.php?f=8&t=11400&p=30177):
- Code: Select all
<cms:set the_customer = "<cms:gpc 'pp_item_customer' />" scope="global"/>
<cms:set the_email = "<cms:gpc 'pp_item_email' />" scope="global"/>
<cms:set the_address = "<cms:gpc 'pp_item_address' />" scope="global"/>
<cms:set the_price = "<cms:gpc 'pp_amount' />" scope="global"/>
<cms:set the_id = "<cms:gpc 'pp_item_id' />" scope="global"/>
<cms:show the_customer/><br>
<cms:show the_email/><br>
<cms:show the_address/><br>
<cms:show the_price/><br>
<cms:show the_id/><br>
<cms:set the_items = "<cms:gpc 'pp_item_name'/>" is_json="0"/>
<cms:set the_itemsj = "<cms:php>$str = '<cms:show the_items />'; echo htmlspecialchars_decode($str);</cms:php>" is_json="1"/>
<cms:capture into='p_items' is_json='1' >
<cms:show the_itemsj as_json="1" />
</cms:capture >
<cms:show p_items as_json="1" />
<cms:if the_id>
<cms:db_persist
_masterpage=k_template_name
_mode='create'
_invalidate_cache='0'
_auto_title='1'
purchase_name=the_customer
purchase_email=the_email
purchase_address=the_address
purchase_items=p_items
purchase_price=the_price
purchase_id=the_id
purchase_status="unconfirmed"
/>
</cms:if>
Then once the new purchase is created in couch, the user is redirected to a success page which sends an email. This could be modified to send a download link fairly easily.
- Code: Select all
<cms:set bkt = "<cms:gpc 'bkt' />" scope="global"/>
<cms:set pid = "<cms:gpc 'pid' />" scope="global"/>
<cms:if bkt="1">
<cms:pages masterpage="purchases.php" custom_field="purchase_id=<cms:show pid />">
<cms:set cname="<cms:show purchase_name/>" scope="global"/>
<cms:set cemail="<cms:show purchase_email/>" scope="global"/>
<cms:set ceaddress="<cms:show purchase_address/>" scope="global"/>
<cms:set ctotal="<cms:show purchase_price/>" scope="global"/>
</cms:pages>
<br>
<div class="message">
<h3>Thanks for your order.</h3>
<table class="tablestyle"><thead><th colspan="2">Order Details:</th></thead>
<cms:pages masterpage="purchases.php" custom_field="purchase_id=<cms:show pid />">
<tr><td>Name: </td><td><cms:show purchase_name/></td></tr>
<tr><td>Email: </td><td><cms:show purchase_email/></td></tr>
<tr><td>Address: </td><td><cms:show purchase_address/></td></tr>
<tr><td>Items: </td>
<td>
<cms:show_repeatable 'purchase_items'>
<cms:pages masterpage="book.php" id="<cms:show purchase_item_id />"><cms:show k_page_title /></cms:pages> - Quantity: <cms:show purchase_item_quantity /><br>
</cms:show_repeatable>
</td>
</tr>
<tr><td>Total Paid: </td><td>£<cms:show purchase_price/></td></tr>
</cms:pages>
</table>
<hr><p><cms:get_field 'email_message' masterpage="global.php"/></p>
</div>
<cms:send_mail from="noreply@yoursite.co.uk" to="<cms:show cemail/>" bcc="youremail" subject='Purchase success' html='1'>
<h3>Thanks for your order.</h3>
<h4>Order Details:</h4>
<cms:pages masterpage="purchases.php" custom_field="purchase_id=<cms:show pid />">
<p>Name: <cms:show purchase_name/></p>
<p>Email: <cms:show purchase_email/></p>
<p>Address: <cms:show purchase_address/></p>
<p>Items: <br><cms:show_repeatable 'purchase_items'>
<cms:pages masterpage="item.php" id="<cms:show purchase_item_id />"><cms:show k_page_title /></cms:pages> - Quantity: <cms:show purchase_item_quantity /><br>
</cms:show_repeatable></p>
<p>Total Paid: £<cms:show purchase_price/></p>
</cms:send_mail>
It still needs some work(!), I'll be trying to integrate it with member accounts and possibly stock control next.
Sorry this has been so lengthy, hopefully it helps! I'll maybe try to put together the files in a working template tomorrow and attach it here if I have time (going on holiday Saturday morning),