概述
集成paypal支付接口的,对象是海外用户,全部用US的信用卡结算; 项目结束的时候就有想法写一篇网站集成paypal的简易教程,方便以后的各位开发者
修改的文件路径是vendorcodeagesbiz-order-paysrcPayPayment;
增加PalPayGateway.php文件
<?php
namespace CodeagesBizPayPayment;
use CodeagesBizFrameworkServiceExceptionAccessDeniedException;
use CodeagesBizFrameworkServiceExceptionInvalidArgumentException;
use CodeagesBizFrameworkUtilArrayToolkit;
class PalPayGateway extends AbstractGateway
{
//**在线案例演示地址:http://all.zkdygl.com/course/20**
protected $url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';(沙盒环境)
protected $wapUrl = 'https://www.sandbox.paypal.com/cgi-bin/webscr';(沙盒环境)
// protected $url = 'https://www.paypal.com/cgi-bin/webscr';(正式环境)
// protected $wapUrl = 'https://www.paypal.com/cgi-bin/webscr';(正式环境)
protected $isWap = false;
public function createTrade($data)
{
if (!ArrayToolkit::requireds($data, array(
'goods_title',
'goods_detail',
'attach',
'trade_sn',
'amount',
'notify_url',
'return_url',
'create_ip',
))) {
throw new InvalidArgumentException('trade args is invalid.');
}
$platformType = empty($data['platform_type']) ? 'Web' : $data['platform_type'];
if ($platformType == 'Wap') {
$this->url = $this->wapUrl;
$this->isWap = true;
}
$data = $this->convertParams($data);
return array(
'url' => "",
'data' => $data
);
}
private function verified($data){
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) $get_magic_quotes_exists = true;
foreach ($data as $key => $value) {
if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1){
$value = urlencode(stripslashes($value));
}else{
$value = urlencode($value);
}
$req.= "&$key=$value";
}
$ch = curl_init($this->url);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
$res = curl_exec($ch);
if (strcmp ($res, "VERIFIED") == 0) {
return true;
} else if (strcmp ($res, "INVALID") == 0) {
return false;
}
}
public function converterNotify($data)
{
$res = $this->verified($data);
if (!$res) {
return array(
array(
'status' => 'failture',
'notify_data' => $data,
),
'fail'
);
}
if ($data['payment_status'] == 'Completed') {
return array(array(
'status' => 'paid',
'cash_flow' => $data['payer_id'],
'paid_time' => time(),
'pay_amount' => (int)($data['payment_gross'] * 100),
'cash_type' => 'USD',
'trade_sn' => $data['invoice'],
'attach' => array(),
'notify_data' => $data,
),
json_encode(array(
'ret_code' => '0000',
'ret_msg' => '交易成功'
))
);
}
return array(
array(
'status' => 'failture',
'notify_data' => $data,
),
'fail'
);
}
public function applyRefund($data)
{
throw new AccessDeniedException('can not apply refund with lianlianpay.');
}
public function queryTrade($tradeSn)
{
return null;
}
public function converterRefundNotify($data)
{
throw new AccessDeniedException('can not convert refund notify with Palpay.');
}
protected function signParams($params, $options)
{
return $this->getSetting();
// return $setting['signatureToolkit']->signParams($params, $options);
}
protected function makeForm($params){
$setting = $this->getSetting();
$formHtml = '<form style="text-align:center;" id="form1" name="form1" action="'.$this->url.'" method="post" class="paypal">' .
"<input type='hidden' name='cmd' value='_xclick'>" .//告诉paypal该表单是立即购买
"<input type='hidden' name='business' value='".$setting['account']."'>" .//卖家帐号 也就是收钱的帐号
"<input type='hidden' name='item_name' value='".$params['name_goods']."'>" .//商品名称 item_number
"<input type='hidden' name='item_number' value='1'>" .//物品号 item_number
"<input type='hidden' name='amount' value='".$params['money_order']."'>" .// 订单金额
"<input type='hidden' name='currency_code' value='USD'>" .// 货币
"<input type='hidden' name='return' value='".$params['url_return']."'>" .// 支付成功后网页跳转地址
"<input type='hidden' name='return' value='".$params['url_return']."'>" .// 支付成功后网页跳转地址
"<input type='hidden' name='notify_url' value='".$params['notify_url']."'>" .//支付成功后paypal后台发送订单通知地址
"<input type='hidden' name='cancel_return' value='".$params['url_return']."'>" .//用户取消交易返回地址
"<input type='hidden' name='invoice' value='".$params['no_order'] ."'>" .//自定义订单号
"<input type='hidden' name='charset' value='utf-8'>" .// 字符集
"<input type='hidden' name='no_shipping' value='1'>" .// 不要求客户提供收货地址
"<input type='hidden' name='no_note' value=''>" .// 付款说明
"<input type='hidden' name='rm' value='2'>" .
"</form><script type='text/javascript'>function load_submit(){document.form1.submit()}load_submit();</script>";
return $formHtml;
}
protected function convertParams($params)
{
$setting = $this->getSetting();
$converted = array();
$converted['busi_partner'] = '101001';
$converted['dt_order'] = date('YmdHis', time());
$converted['money_order'] = round($params['amount']/100/7.0755,2);
$converted['name_goods'] = $this->filterText($params['goods_title']);
$converted['no_order'] = $params['trade_sn'];
if (!empty($params['notify_url'])) {
$converted['notify_url'] = $params['notify_url'];
}
$converted['sign_type'] = 'RSA';
$converted['version'] = '1.0';
$converted['oid_partner'] = $setting['oid_partner'];
$converted['user_id'] = $params['attach']['identify_user_id'];
$converted['timestamp'] = date('YmdHis', time());
if (!empty($params['return_url'])) {
$converted['url_return'] = $params['return_url']."?sn=".$_POST['orderSn'];
}
if (empty($params['attach']['bindPhone'])) {
$params['attach']['bindPhone'] = '';
}
$converted['risk_item'] = json_encode(array(
'frms_ware_category'=>1008,
'user_info_mercht_userno'=>$params['attach']['identify_user_id'],
'user_info_dt_register'=>date('YmdHis', $params['attach']['user_created_time']),
'user_info_bind_phone' => $params['attach']['bindPhone']
));
$converted['userreq_ip'] = str_replace(".", "_", $params['create_ip']);
$converted['bank_code'] = '';
$converted['pay_type'] = '3';
$converted['sign'] = $this->signParams($converted, $setting);
$converted['formHtml'] = $this->makeForm($converted);
return $converted;
}
protected function filterText($text)
{
preg_match_all('/[x{4e00}-x{9fa5}A-Za-z0-9.]*/iu', $text, $results);
$title = '';
if ($results) {
foreach ($results[0] as $result) {
if (!empty($result)) {
$title .= $result;
}
}
}
return $title;
}
}
}
在线案例演示地址:http://all.zkdygl.com/course/20
1、edusoho新版后台的界面,配置PayPal支付参数信息,主要还是商户邮箱
2、配置完之后,在前台测试课程购买的支付过程
3、选择PayPal支付订单,直接跳转到PayPal支付的页面!!!!!
4、支付完之后,就会进行回调,系统显示支付成功
最后
以上就是高高心情为你收集整理的edusoho二次开发paypal支付功能-附源代码的全部内容,希望文章能够帮你解决edusoho二次开发paypal支付功能-附源代码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复