最近项目中需要开发澳洲那边的微信支付宝支付,所以去研究了一下微信境外支付,发现境外只支持服务商模式,即客户需要去与澳洲本地服务商合作,由客户提供材料,服务商帮客户申请支付相关账号,然后调用服务商提供的接口去获得调起支付所需的参数。客户选择了RoyalPay这家微信支付服务商,于是我照着服务商的提供的接口文档进行对接,实现了下单、查询订单状态、退款、查询退款状态接口。接口文档地址
<?phpnamespace App\Components\AustraliaPay;use App\Components\Common\DateTool;
use App\Components\Common\Utils;class AustraliaPay
{/*** 支付接口** by xh** 2020/4/8 13:28* @param array $data trade_no:订单号 price:订单金额 description:订单描述 currency:支付币种(只支持人民币与澳元) channel:支付通道(Wechat微信支付 Alipay支付宝支付) notify_url:回调url* @return mixed*/public static function payOrder(array $data){$data["appid"] = env('WECHAT_PAYMENT_APPID_HW');$sign_data = self::getSign();$url = "https://mpay.royalpay.com.au/api/v1.0/gateway/partners/".$sign_data['partner_code']."/app_orders/".$data['trade_no']."?time=".$sign_data['now_millisecond']."&nonce_str=".$sign_data['nonce_str']."&sign=".$sign_data['sign'];$response = self::put_curl($url,json_encode($data));Utils::processLog(__METHOD__,'测试支付----------------',json_encode($response));return json_decode($response);}/*** 查询订单状态** by xh** 2020/4/8 10:29* @param $trade_no //商户订单号* @return mixed*/public static function queryOrderStatus($trade_no){$sign_data = self::getSign();$url = "https://mpay.royalpay.com.au/api/v1.0/gateway/partners/".$sign_data['partner_code']."/orders/".$trade_no."?time=".$sign_data['now_millisecond']."&nonce_str=".$sign_data['nonce_str']."&sign=".$sign_data['sign'];$result = Utils::curl($url,false,0,1);return json_decode($result);}/*** 查询退款状态** by xh** 2020/4/8 10:29* @param $trade_no //商户订单号* @return mixed*/public static function queryRefundStatus($trade_no,$refund_trade_no){$sign_data = self::getSign();$url = "https://mpay.royalpay.com.au/api/v1.0/gateway/partners/".$sign_data['partner_code']."/orders/".$trade_no."/refunds/".$refund_trade_no."?time=".$sign_data['now_millisecond']."&nonce_str=".$sign_data['nonce_str']."&sign=".$sign_data['sign'];$result = Utils::curl($url,false,0,1);return json_decode($result);}/*** 申请退款** by xh** 2020/4/8 11:54* @param $trade_no //订单单号* @param $refund_trade_no //退款单号* @param $refund_fee //退款金额* @return mixed*/public static function refund($trade_no,$refund_trade_no,$refund_fee){$sign_data = self::getSign();$url = "https://mpay.royalpay.com.au/api/v1.0/gateway/partners/".$sign_data['partner_code']."/orders/".$trade_no."/refunds/".$refund_trade_no."?time=".$sign_data['now_millisecond']."&nonce_str=".$sign_data['nonce_str']."&sign=".$sign_data['sign'];$result = self::put_curl($url,json_encode(['fee'=>$refund_fee]));return json_decode($result);}/*** 获取签名** by xh** 2020/4/7 18:01* @return array*/public static function getSign(){$nonce_str = Utils::getRandomString(10); //:随机字符串,无长度限制,请使用URL安全字符(避开&,=等符号),请注意HTTP URL的最大长度限制,建议长度在10到30位$partner_code = env('PARTNER_CODE'); //商户编码,由4位大写字母或数字构成$now_millisecond = DateTool::getMillisecond(); //当前毫秒,UTC毫秒时间戳,取当前UTC时间的毫秒数时间戳,Long类型,5分钟内有效$credential_code = env('CREDENTIAL_CODE'); //系统为商户分配的开发校验码,请妥善保管,不要在公开场合泄露//验证字符串$valid_string = $partner_code."&".$now_millisecond."&".$nonce_str."&".$credential_code;//签名算法 sha256加密验证字符串然后转成十六进制最后再转小写$sign = hash('sha256',$valid_string,false);$sign_data = ['nonce_str' => $nonce_str,'partner_code' => $partner_code,'now_millisecond' => $now_millisecond,'sign' => $sign];return $sign_data;}/*** 发送put请求,传json参数** by xh** 2020/4/8 9:04* @param $url* @param $json_data* @return bool|string*/public static function put_curl($url, $json_data){$ch = curl_init();$header = array('Content-Type: application/json; charset=utf-8','Content-Length: ' . strlen($json_data));//定义header,可以加多个curl_setopt($ch, CURLOPT_URL, $url); //定义请求地址curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); //定义请求类型,当然那个提交类型那一句就不需要了curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//定义headercurl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//定义是否直接输出返回流?curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); //定义提交的数据curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在$res = curl_exec($ch);curl_close($ch);//关闭return $res;}}
curl方法内容。日志类替换成你们自己用的日志类就行了
/*** 调取外部接口方法* @param $url 请求网址* @param bool $params 请求参数* @param int $ispost 请求方式* @param int $https https协议* @return bool|mixed*/public static function curl($url, $params = false, $ispost = 0, $https = 0, $header = null){Utils::processLog(__METHOD__, '', " " . "url:" . $url);$httpInfo = array();$ch = curl_init();//2019-10-07进行优化,可以设置header信息if ($header == null) {$header = array('Content-Type: application/json; charset=utf-8');}curl_setopt($ch, CURLOPT_HTTPHEADER, $header);curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);curl_setopt($ch, CURLOPT_TIMEOUT, 30);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);if ($https) {curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在}if ($ispost) {Utils::processLog(__METHOD__, "", "POST请求的params:" . json_encode($params));curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));curl_setopt($ch, CURLOPT_URL, $url);} else {if ($params) {if (is_array($params)) {$params = http_build_query($params);}curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);} else {curl_setopt($ch, CURLOPT_URL, $url);}}$response = curl_exec($ch);if ($response === FALSE) {//echo "cURL Error: " . curl_error($ch);return false;}$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);$httpInfo = array_merge($httpInfo, curl_getinfo($ch));curl_close($ch);return $response;}
调用下单接口
//进行业务测试
public function test()
{$pay_data = ['trade_no' => $trade_no = Utils::generateTradeNo(), //订单号'price' => 100, //支付金额,单位为分'description'=>'1澳元支付成功', //订单描述'currency' => 'AUD', //货币种类'notify_url' => ''http://xxx.com/payNotify'', //支付回调'channel' => 'Wechat' //支付通道 Wechat:微信支付 Alipay:支付宝支付];$result = AustraliaPay::payOrder($pay_data)->sdk_params;return ApiResponse::makeResponse(true,bcmul(1.11,100),ApiResponse::SUCCESS_CODE);
}
下单接口返回内容
微信

支付宝



















