ÔõÑùʹÓÃThinkPHP6¾ÙÐÐÖ§¸¶±¦ºÍ΢ÐÅÖ§¸¶²Ù×÷£¿
Ëæ×ÅÒƶ¯»¥ÁªÍøµÄ¿ìËÙÉú³¤£¬µç×ÓÖ§¸¶ÔÚÏÖ´ú»¯ÉúÑÄÖÐÊÎÑÝ×ÅÔ½À´Ô½Ö÷ÒªµÄ½ÇÉ«¡£Ö§¸¶±¦ºÍ΢ÐÅÖ§¸¶ÒѾ³ÉΪÏÖ´úÉç»áµç×ÓÖ§¸¶µÄÖ÷ÒªÊÖ¶ÎÖ®Ò»¡£Òò´Ë£¬ÎªÁËÈÃwebÓ¦ÓóÌÐò˳³©´¦Öóͷ£Ö§¸¶±¦ºÍ΢ÐÅÖ§¸¶£¬±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃthinkphp 6¾ÙÐÐÖ§¸¶±¦ºÍ΢ÐÅÖ§¸¶²Ù×÷¡£
Ò»¡¢ÒýÈëÏà¹Ø¿âÎļþ
ÔÚʹÓÃThinkPHP6¾ÙÐÐÖ§¸¶±¦ºÍ΢ÐÅÖ§¸¶Ö®Ç°£¬Ê×ÏÈÐèÒªÒýÈëÏà¹Ø¿âÎļþ¡£ÎÒÕâÀï¼ÙÉèÄúÒѾװÖÃÁËComposer£¬ÄÇôÔÚ¿ØÖÆ̨ÖÐʹÓÃÒÔÏÂÏÂÁî¼´¿É×°ÖÃÏà¹Ø¿âÎļþ£º
composer require alipay/easysdk
composer require wechatpay/wechatpay
composer require guzzlehttp/guzzle
ÆäÖУ¬alipay/easysdkÊÇÖ§¸¶±¦¿ª·¢°ü£¬wechatpay/wechatpayÊÇ΢ÐÅ¿ª·Åƽ̨SDK£¬guzzlehttp/guzzleÊÇÓÃÓÚÏòAPI·¢³öHTTPÇëÇóµÄPHP¿â¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
¶þ¡¢Ö§¸¶±¦Ö§¸¶²Ù×÷
Ö§¸¶±¦Ö§¸¶Àú³ÌµÄÖ÷ÒªÁ÷³ÌÊÇ£º
½á¹¹ÐèÒªÖ§¸¶µÄ¶©µ¥ÐÅÏ¢£»
ŲÓÃÖ§¸¶±¦APIÌᳫ֧¸¶ÇëÇó£»
Óû§Í¨¹ýÖ§¸¶±¦¾ÙÐÐÖ§¸¶£»
Ö§¸¶±¦Ö§¸¶Ð§¹û֪ͨÉÌ»§Ð§ÀÍÆ÷¡£
ÏÂÃæÊÇÒ»¸öʹÓÃThinkPHP6¾ÙÐÐÖ§¸¶±¦Ö§¸¶µÄÀý×Ó£º
use AlipayEasySDKFactory; class AlipayController extends Controller { public function index() { $config = [ 'app_id' => 'your-app-id', 'private_key' => 'your-private-key', 'public_key' => 'your-public-key', 'log' => [ 'file' => './alipay/easy.log', 'level' => 'debug', ], 'notify_url' => 'http://yourwebsite.com/notify', 'return_url' => 'http://yourwebsite.com/return' ]; $app = Factory::create('payment', $config); $order = [ 'out_trade_no' => date('YmdHis'), 'total_amount' => 0.01, 'subject' => 'test', ]; $url = $app->order->page($order, 'http://yourwebsite.com/return'); return $url; } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬Ê×ÏÈÎÒÃÇÒýÓÃÁËÖ§¸¶±¦µÄEasySDK¹¤³§À࣬¸ÃÀཨÉèÁËÒ»¸ö¾ßÓиø¶¨ÉèÖõÄÉúÒâʵÀý¡£È»ºó£¬ÎÒÃǽṹÁËÒ»¸ö°üÀ¨¶©µ¥ÐÅÏ¢µÄorderÊý×é¡£ÔÚÕâÀÎÒÃÇÉèÖÃÁ˶©µ¥ºÅ(out_trade_no)¡¢¶©µ¥½ð¶î(total_amount)ºÍ¶©µ¥Ö÷Ìâ(subject)¡£½ÓÏÂÀ´£¬ÎÒÃÇʹÓÃorderÒªÁìÌᳫ֧¸¶ÇëÇó£¬×îºó½«Ö§¸¶URL·µ»Ø¸øÓû§¼´¿É¡£
ÔÚÖ§¸¶Íê³Éºó£¬Ö§¸¶±¦½«»áÏòÉÌ»§Ð§ÀÍÆ÷·¢ËÍÒ»¸öPOSTÇëÇ󣬸ÃÇëÇó°üÀ¨Ò»Ð©Ö§¸¶ÐÅÏ¢£¬²¢Å²ÓÃÉÌ»§µÄµÄnotify_url¡£ÔÚ´úÂëÖУ¬notify_urlÖ¸ÏòÉÌ»§Ð§ÀÍÆ÷µÄÒ»¸öµØµã£¬ÌṩÉÌ»§´¦Öóͷ£Ö§¸¶Ð§¹ûµÄÄÜÁ¦¡£
Èý¡¢Î¢ÐÅÖ§¸¶²Ù×÷
΢ÐÅÖ§¸¶Àú³ÌµÄÖ÷ÒªÁ÷³ÌÊÇ£º
Ïò΢ÐÅЧÀÍÆ÷ÇëÇóÔ¤Ö§¸¶¶©µ¥ÐÅÏ¢£»
»ñµÃ΢ÐÅЧÀÍÆ÷·µ»ØµÄprepay_id£¬²¢ÌìÉú¶©µ¥µÄÊðÃû(Çë×¢ÖØ£¬ÊðÃû˳ÐòÒÀ´ÎΪappid¡¢mch_id¡¢nonce_str¡¢prepay_id¡¢trade_type¡¢key)£»
¿Í»§¶ËÌᳫ֧¸¶ÇëÇó£»
΢ÐÅÖ§¸¶Ð§¹û֪ͨÉÌ»§Ð§ÀÍÆ÷¡£
ÏÂÃæÊÇÒ»¸öʹÓÃThinkPHP6¾ÙÐÐ΢ÐÅÖ§¸¶µÄÀý×Ó:
use WechatPayGuzzleMiddlewareUtilPemUtil; use WechatPayNotifyPaidNotify; use WechatPayOpenAPIV3PayAppPayClient; use WechatPayOpenAPIV3PayJsPayClient; class WechatController extends Controller { public function index() { $merchantId = 'your-mchid'; $merchantSerialNumber = 'your-serial'; $merchantPrivateKey = PemUtil::loadPrivateKey('./cert/apiclient_key.pem'); $wechatpayCertificate = PemUtil::loadCertificate('./cert/wechatpay_certificate.pem'); $apiV3Key = 'your-key'; $client = new JsPayClient( $merchantId, $merchantSerialNumber, $merchantPrivateKey, $wechatpayCertificate, $apiV3Key ); $params = [ 'body' => 'testbody', 'out_trade_no' => date('YmdHis'), 'app_id' => 'your-app-id', 'notify_url' => 'http://yourwebsite.com/wechat_notify', 'amount' => [ 'total' => 1, ], 'description' => 'test_description', ]; $result = $client->prepare($params); $prepayId = $result['prepay_id']; $appClient = new AppPayClient( $merchantId, $merchantSerialNumber, $merchantPrivateKey, $wechatpayCertificate, $apiV3Key ); $packageParams = [ 'prepay_id' => $prepayId, 'trade_type' => 'JSAPI', 'timeStamp' => strval(time()), 'nonceStr' => md5(bin2hex(openssl_random_pseudo_bytes(16))), ]; $packageParams['sign'] = $appClient->sign($packageParams); return json_encode($packageParams); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÒýÈëÁË΢ÐÅÖ§¸¶µÄGuzzleMiddleware¿âºÍ΢ÐÅÖ§¸¶¿ª·Åƽ̨µÄSDK¡£È»ºó£¬ÎÒÃÇÉèÖÃÁËÉÌ»§ID¡¢ÉÌ»§Á÷Ë®ºÅ¡¢ÉÌ»§Ë½Ô¿(mchid¡¢serialºÍkey)¡£½ÓÏÂÀ´£¬ÎÒÃǽṹÁËÖ§¸¶Ïà¹ØµÄ²ÎÊý£¬²¢Ê¹ÓÃJsPayClientµÄprepareÒªÁì»ñÈ¡prepay_id¡£×¢ÖØ£¬ÌìÉú¶©µ¥ÊðÃûµÄ˳ÐòÒ»¶¨ÒªÆ¾Ö¤appid¡¢mch_id¡¢nonce_str¡¢prepay_id¡¢trade_type¡¢key¾ÙÐС£×îºó£¬ÎÒÃÇʹÓÃAppPayClientµÄsignÒªÁìÌìÉúÊðÃû£¬²¢½«ÆäËùÓвÎÊýJSONÐòÁл¯ºó·µ»Ø¸øÓû§¼´¿É¡£
ÔÚÖ§¸¶Íê³Éºó£¬Î¢ÐŽ«»áÏòÉÌ»§Ð§ÀÍÆ÷·¢ËÍÒ»¸öPOSTÇëÇ󣬸ÃÇëÇó°üÀ¨Ò»Ð©Ö§¸¶ÐÅÏ¢£¬²¢Å²ÓÃÉÌ»§µÄµÄnotify_url¡£ÔÚ´úÂëÖУ¬notify_urlÖ¸ÏòÉÌ»§Ð§ÀÍÆ÷µÄÒ»¸öµØµã£¬ÌṩÉÌ»§´¦Öóͷ£Ö§¸¶Ð§¹ûµÄÄÜÁ¦¡£
×ÛÉÏËùÊö£¬±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃThinkPHP6¾ÙÐÐÖ§¸¶±¦ºÍ΢ÐÅÖ§¸¶²Ù×÷¡£Çë×¢ÖØ£¬±¾ÎĽöÌṩÁËÒ»¸ö»ù±¾µÄʾÀý£¬ÄúÓ¦¸ÃÔ½·¢Ï꾡µØ´¦Öóͷ£Ö§¸¶Ð§¹ûºÍÒì³£¡£ÈôÊÇÄúÓöµ½ÈκÎÎÊÌ⣬Çë²Î¿¼Ö§¸¶±¦ºÍ΢ÐÅÖ§¸¶µÄAPIÎĵµ»òÕßGitHubµÈƽ̨µÄ×ÊÁÏ¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃThinkPHP6¾ÙÐÐÖ§¸¶±¦ºÍ΢ÐÅÖ§¸¶²Ù×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡