四、充值
充值弹窗
<div id="fb-root"></div> <script src="http://connect.facebook.net/en_US/all.js"></script> <script> FB.init({ appId:'{{ app_id }}', cookie:true, status:true, xfbml:true }); </script> function buyit(rmb, pearls){ FB.ui({ method:'pay', //credits_purchase: true, purchase_type:'item', //redirect_uri: 'http://apps.facebook.com/bubblefish_tw/', order_info:{ item_id:'pearls', title:'泡泡魚珍珠'+ pearls +'顆 ', price:rmb, description:'獲取泡泡魚珍珠' + pearls + '顆', image_url:'{{ media_url }}images/payment_logo.jpg', product_url:'{{ media_url }}images/payment_logo.jpg' } }, function(resp){ if(resp.order_id) { //alert("ok! order_id," + resp.order_id); window.top.location='{{ sns_url }}'; } else { // alert(resp.error_message); } } ); return false; } FB.Canvas.setAutoResize(); </script>
充值后台
def pay(request): if request.POST.has_key('signed_request'): signed_request = request.POST.get("signed_request", '') data, sig_ok = parse_signed_request(signed_request) if not sig_ok: return HttpResponse("SIG_ERROR") if not data.has_key('user_id') or not data.has_key('oauth_token'): app_id = settings.SNS_APP_ID sns_url = settings.SNS_URL redirect_uri = urllib.quote_plus(sns_url) redirect_url = "https://www.facebook.com/dialog/oauth?client_id=%s&redirect_uri=%s&scope=email,read_stream" %(app_id, redirect_uri) return render_to_response('%s/freegift/redirect.html' % (settings.SNS), {'redirect_url': redirect_url}) access_token = data['oauth_token'] to_sns_id = data['user_id'] else: return HttpResponse("POST_ERROR") member = login_sns_user(request, access_token) if not member: HttpResponse("MEMBER_ERROR") index_file = '%s/payment_facebook_tw/index.html' % (settings.SNS) confirm_file = '%s/payment_facebook_tw/confirm.html' % (settings.SNS) ret = in_activity_time() if ret: index_file = '%s/payment_facebook_tw/index_activity.html' % (settings.SNS) confirm_file = '%s/payment_facebook_tw/confirm_activity.html' % (settings.SNS) return render_to_response(index_file, { 'media_url':settings.MEDIA_URL, 'sns_url':settings.SNS_URL, 'score': member.score, 'skey': access_token, 'app_id':settings.SNS_APP_ID, }, context_instance=RequestContext(request)) def callback(request): if request.POST.has_key('signed_request'): signed_request = request.POST.get("signed_request", '') data, sig_ok = parse_signed_request(signed_request) if not sig_ok: return HttpResponse("SIG_ERROR") payload = data['credits'] order_id = payload['order_id'] method = request.POST.get('method', None) return_data = {'method':method} if method == 'payments_status_update': status = payload['status'] return_data['content'] = {'status':'settled','order_id':order_id} if status == 'placed': order_details = simplejson.loads(payload['order_details']) buyer = order_details['buyer'] item = order_details['items'][0] rmb = item['price'] pearls = rmb_to_pearls(rmb) member = get_member_by_sns_id(buyer) record = RechargeRecord(member_id=member.id, pearls=pearls, rmb=rmb, tid=str(order_id), status='UP') record.save() #check and log here elif status == 'settled': order_details = simplejson.loads(payload['order_details']) buyer = order_details['buyer'] item = order_details['items'][0] #now save this transaction into our database record = RechargeRecord.objects.get(tid=str(order_id)) result = finish_payment(record) if not result: HttpResponse("ERROR") else: return_data['content']['status'] = status elif method == 'payments_get_items': order_info = payload['order_info'] item = simplejson.loads(order_info) return_data['content'] = [item] return HttpResponse(simplejson.dumps(return_data)) else: return HttpResponse("POST_ERROR")
五、邀请
邀请弹窗
message= "you_msg" redirect_url = "http://www.facebook.com/dialog/apprequests?app_id=%s&redirect_uri=%s&message=%s" %( app_id, redirect_uri, message) return render_to_response('/redirect.html', {'redirect_url': redirect_url})
获取邀请好友的IDs
res_get = request.GET.copy() request_ids = [res_get[i] for i in res_get if i.startswith('request_ids')] to_ids = [] for request_id in request_ids: from_id, to_id = get_request_info(access_token, request_id) if from_id == str(member.sns_id): to_ids.append(int(to_id))
备注:
redirect.html
<script> top.location.href='{{ redirect_url|safe }}'</script>
六、粉丝礼物
粉丝礼物选择朋友界面
<div id="friends_list"> <fb:serverfbml style="width: 500px; height:20px;"> <script type="text/fbml"> <fb:fbml> <fb:request-form action="{{ site_url }}/freegifts_callback/?v={{ style }}&skey={{ skey }}" method="POST" invite="true" type="BubbleFish"content="Hey there, I've Send a gift to you in the BubbleFish! <fb:req-choice url='{{ sns_url }}?s={{ f_sns_id }}&i={{ gift_id }}' label='Accept' />" > <fb:multi-friend-selector showborder="false" actiontext="Let's send free gifts to each other !" exclude_ids="" import_external_friends="false" bypass="cancel" /> </fb:request-form> </fb:fbml> </script> </fb:serverfbml> </div>
七、小应用
live-stream:
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId={{ app_id }}&xfbml=1"></script><fb:live-stream event_app_id="{{ app_id }}" width="745" height="500" xid="" always_post_to_friends="true"></fb:live-stream>
like button:
<iframe src="http://www.facebook.com/plugins/like.php?app_id={{ app_id }}&href=http%3A%2F%2Fwww.facebook.com%2Fapps%2Fapplication.php%3Fid%3D{{ app_id }}&send=false&layout=standard&width=450&show_faces=false&action=like&colorscheme=light&font&height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:550px; height:35px;" allowTransparency="true"></iframe>
bookmark:
<fb:bookmark />
八、原来的FBML充值实现
使用IFrame
def pay(request): ret = """ <div> <fb:iframe src="http://%s/payment/pay_index/" frameborder="0" width="760" height="500" scrolling="no"></fb:iframe> </div> """ % settings.SNS_APP_SERVER_NAME return HttpResponse(ret)
修改JS中method
method:'pay.prompt'
问题:
IE中赠送礼物无法出现好友列表:
解决:<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">